Looking for Object Oriented Programming C++ test answers and solutions? Browse our comprehensive collection of verified answers for Object Oriented Programming C++ at emokymai.vu.lt.
Get instant access to accurate answers and detailed explanations for your course questions. Our community-driven platform helps students succeed!
#define main1 main
/** Question C1
What is output of the program?
*/
//Q-stl-1: std::ranges::for_each; std::ranges::count; std::ranges::count_if;
//std::ranges::all_of; std::ranges::none_of
#include <iostream>
#include <vector>
#include <algorithm>
int main1() {
std::vector<int> v = {0, 3, -1, 1, -6, 8, -2, 6};
auto it1 = std::ranges::find(v, 4);
auto it2 = std::ranges::find_if(v, [](int x){
return x > 2;
});
auto itMin = std::ranges::min_element(v);
auto itMax = std::ranges::max_element(v);
int p1 = (it1 == v.end()) ? -1 : static_cast<int>(it1 - v.begin());
int p2 = (it2 == v.end()) ? -1 : static_cast<int>(it2 - v.begin());
std::cout << p1 << "|" << p2 << "|" << *itMin << "|" << *itMax;
return 0;
}
#define main1 main
/** Question C1
What is output of the program?
*/
//
//binary_search, lower_bound, upper_bound
#include <iostream>
#include <vector>
#include <algorithm>
int main1() {
std::vector<int> v = {8, -3, 7, 3, -5, 3, 8, 5, 7, 7, 1, 9};
std::ranges::sort(v);
bool found = std::ranges::binary_search(v, 5);
auto itL = std::ranges::lower_bound(v, -4);
auto itU = std::ranges::upper_bound(v, -3);
int posL = static_cast<int>(itL - v.begin());
int posU = static_cast<int>(itU - v.begin());
int count = posU - posL;
int first = (itL == v.end()) ? 999 : *itL;
int last = (itU == v.begin()) ? 999 : *(itU - 1);
std::cout << found << "|" << posL << "|" << posU << "|" << count << "|" << first << "|" << last;
return 0;
}
#define main1 main
/** Question C1
What is output of the program?
*/
#include <iostream>
int main1(int argc, char** argv){
int a = 2;
int b = 8;
auto f = [&a, &b](int x){
a += x;
b += a;
return a + b;
};
std::cout << f(1) << "|" << a << "|" << b << "|" << f(4);
return 0;
}
#define main1 main
/** Question C1
What is output of the program?
*/
//
//sort, stable_sort, partial_sort, nth_element, partition
#include <iostream>
#include <vector>
#include <ranges>
#include <algorithm>
int main1() {
std::vector<int> v = {-3, -7, -1, 3, 6, 2, 4, 2, -2, -3, 6};
std::vector<int> a = v;
std::vector<int> b = v;
std::vector<int> c = v;
std::vector<int> d = v;
std::vector<int> e = v;
std::ranges::sort(a);
std::ranges::stable_sort(b, [](int x, int y){
return abs(x) < abs;
});
int k1 = std::min(2, static_cast<int>(c.size()));
std::ranges::partial_sort(c, c.begin() + k1);
int k2 = std::min(6, static_cast<int>(d.size()) - 1);
std::ranges::nth_element(d, d.begin() + k2);
auto it = std::ranges::partition(e, [](int x){
return x < -1;
});
int s1 = 0, s2 = 0, s3 = 0;
for (int i = 0; i < 3 && i < static_cast<int>(a.size()); ++i) s1 += a[i];
for (int i = 0; i < 3 && i < static_cast<int>(b.size()); ++i) s2 += b[i];
for (int i = 0; i < k1; ++i) s3 += c[i];
int nthv = d[k2];
int left = static_cast<int>(it.begin() - e.begin());
std::cout << s1 << "|" << s2 << "|" << s3 << "|" << nthv << "|" << left;
return 0;
}
#define main1 main
/** Question C1
What is output of the program?
*/
//
//binary_search, lower_bound, upper_bound
#include <iostream>
#include <vector>
#include <algorithm>
int main1() {
std::vector<int> v = {-5, 2, 9, 2, 6, 0, 0, -1, 9, 4, -4, -2};
std::ranges::sort(v);
bool found = std::ranges::binary_search(v, 3);
auto itL = std::ranges::lower_bound(v, 5);
auto itU = std::ranges::upper_bound(v, 1);
int posL = static_cast<int>(itL - v.begin());
int posU = static_cast<int>(itU - v.begin());
int count = posU - posL;
int first = (itL == v.end()) ? 999 : *itL;
int last = (itU == v.begin()) ? 999 : *(itU - 1);
std::cout << found << "|" << posL << "|" << posU << "|" << count << "|" << first << "|" << last;
return 0;
}
#define main1 main
/** Question C1
What is output of the program?
*/
//
//copy, copy_if, transform, fill, generate
#include <iostream>
#include <vector>
#include <algorithm>
int main1() {
std::vector<int> v = {-3, 2, -5, 1, -8, 1, 5, -3, -1, 4};
std::vector<int> a(v.size(),0);
std::vector<int> b(v.size(),0);
std::vector<int> c(v.size(),0);
std::ranges::copy(v, a.begin());
auto r1 = std::ranges::copy_if(v, b.begin(), [](int x){
return x > 1;
});
int copied = static_cast<int>(r1.out - b.begin());
std::ranges::transform(v, c.begin(), [](int x){
return x + 3;
});
std::ranges::fill(a.begin() + 1, a.begin() + 4, -2);
int cur = 3;
std::ranges::generate(b.begin() + copied, b.end(), [&cur](){
int old = cur;
cur += 2;
return old;
});
int s1 = 0, s2 = 0, s3 = 0;
for (int x : a) s1 += x;
for (int x : b) s2 += x;
for (int x : c) s3 += x;
std::cout << copied << "|" << s1 << "|" << s2 << "|" << s3;
return 0;
}
#define main1 main
/** Question C1
What is output of the program?
*/
//
//replace, replace_if, std::erase, std::erase_if, reverse, rotate
#include <iostream>
#include <vector>
#include <algorithm>
int main1() {
std::vector<int> v = {5, 5, -5, 3, 0, 7, 7, 4, 0, -3};
std::ranges::replace(v, -2, 9);
std::ranges::replace_if(v, [](int x){
return x > 0;
}, -7);
int removed1 = std::erase(v, -2);
int removed2 = std::erase_if(v, [](int const& x){
return x % 3 == 0;
});
std::ranges::reverse(v);
if (!v.empty()) {
int k = 2 % static_cast<int>(v.size());
std::ranges::rotate(v, v.begin() + k);
}
int sz = static_cast<int>(v.size());
int sum = 0;
for (int x : v) sum += x;
std::cout << removed1 << "|" << removed2 << "|" << sz << "|" << sum;
return 0;
}
#define main1 main
/** Question C1
What is output of the program?
*/
#include <iostream>
int main1(int argc, char** argv){
int a = 9;
int b = 6;
auto f = [a, b](int x) mutable {
a += x;
b += a;
return 3*a + -3*b;
};
std::cout << f(2) << "|" << f(2) << "|" << a << "|" << b;
return 0;
}
#define main1 main
/** Question C1
What is output of the program?
*/
//
//sort, stable_sort, partial_sort, nth_element, partition
#include <iostream>
#include <vector>
#include <ranges>
#include <algorithm>
int main1() {
std::vector<int> v = {-3, -8, 8, 5, 7, -5, -7, -4, 4, 4, 1};
std::vector<int> a = v;
std::vector<int> b = v;
std::vector<int> c = v;
std::vector<int> d = v;
std::vector<int> e = v;
std::ranges::sort(a);
std::ranges::stable_sort(b, [](int x, int y){
return abs(x) < abs;
});
int k1 = std::min(4, static_cast<int>(c.size()));
std::ranges::partial_sort(c, c.begin() + k1);
int k2 = std::min(4, static_cast<int>(d.size()) - 1);
std::ranges::nth_element(d, d.begin() + k2);
auto it = std::ranges::partition(e, [](int x){
return x < -1;
});
int s1 = 0, s2 = 0, s3 = 0;
for (int i = 0; i < 3 && i < static_cast<int>(a.size()); ++i) s1 += a[i];
for (int i = 0; i < 3 && i < static_cast<int>(b.size()); ++i) s2 += b[i];
for (int i = 0; i < k1; ++i) s3 += c[i];
int nthv = d[k2];
int left = static_cast<int>(it.begin() - e.begin());
std::cout << s1 << "|" << s2 << "|" << s3 << "|" << nthv << "|" << left;
return 0;
}
#define main1 main
/** Question C1
What is output of the program?
*/
//Q-stl-1: std::ranges::for_each; std::ranges::count; std::ranges::count_if;
//std::ranges::all_of; std::ranges::none_of
#include <iostream>
#include <vector>
#include <algorithm>
int main1() {
std::vector<int> v = {3, 2, -5, -2, -3, 2, -2, -1, -2, 3};
int c1 = std::ranges::count(v, -3);
int c2 = std::ranges::count_if(v, [](int x){
return x % 2 == 0;
});
bool b1 = std::ranges::all_of(v, [](int x){
return x >= -5;
});
bool b2 = std::ranges::none_of(v, [](int x){
return x > 4;
});
std::ranges::for_each(v, [](int &x){
x = x + 1;
});
int sum = 0;
for (auto x : v) sum += x;
std::cout << c1 << "|" << c2 << "|" << b1 << "|" << b2 << "|" << sum;
return 0;
}