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 = {1, -8, -8, -4, -8, -4, 9, 9};
auto it1 = std::ranges::find(v, 3);
auto it2 = std::ranges::find_if(v, [](int x){
return x > 4;
});
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?
*/
//
//equal_range; includes; set_union; set_intersection; set_difference
#include <iostream>
#include <vector>
#include <algorithm>
int main1() {
std::vector<int> a = {-3, -1, 1, 6, 8, 1, 6, -3, -3, -2};
std::vector<int> b = {3, 6, 0, 1, 6, 8, 7, 3, 8};
std::ranges::sort(a);
std::ranges::sort(b);
auto sub1 = std::ranges::unique(a);
a.erase(sub1.begin(), sub1.end());
auto sub2 = std::ranges::unique(b);
b.erase(sub2.begin(), sub2.end());
auto eq = std::ranges::equal_range(a, 1);
int posL = static_cast<int>(eq.begin() - a.begin());
int posU = static_cast<int>(eq.end() - a.begin());
int found = (eq.begin() != eq.end());
bool inc = std::ranges::includes(a, b);
std::vector<int> u(a.size() + b.size());
auto itU = std::ranges::set_union(a, b, u.begin());
u.erase(itU.out, u.end());
std::vector<int> inter(std::min(a.size(), b.size()));
auto itI = std::ranges::set_intersection(a, b, inter.begin());
inter.erase(itI.out, inter.end());
std::vector<int> diff(a.size());
auto itD = std::ranges::set_difference(a, b, diff.begin());
diff.erase(itD.out, diff.end());
int su = 0, si = 0, sd = 0;
for (int x : u) su += x;
for (int x : inter) si += x;
for (int x : diff) sd += x;
std::cout << found << "|" << posL << "|" << posU << "|" << inc << "|"
<< static_cast<int>(u.size()) << "|" << su << "|"
<< static_cast<int>(inter.size()) << "|" << si << "|"
<< static_cast<int>(diff.size()) << "|" << sd;
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 = {-5, -6, -5, 8, -2, 0, -2, 4, -1, -8};
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, 3);
int cur = 1;
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?
*/
//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 = {5, 1, -4, -5, 2, -4, -1, 0, -2, 4};
int c1 = std::ranges::count(v, 0);
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;
}
#define main1 main
/** Question C1
What is output of the program?
*/
#include <iostream>
int apply_twice(int v, auto fn){
return fn(fn(v));
}
int main1(int argc, char** argv){
int a = 6;
int b = 16;
auto op = [a, &b](int x){
b -= a - x;
return x + b;
};
std::cout << apply_twice(2, op) + op(3);
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 = {4, -3, -7, -3, -7, -8, 0, 3, 5, -9, 3};
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(3, static_cast<int>(c.size()));
std::ranges::partial_sort(c, c.begin() + k1);
int k2 = std::min(5, static_cast<int>(d.size()) - 1);
std::ranges::nth_element(d, d.begin() + k2);
auto it = std::ranges::partition(e, [](int x){
return x < -2;
});
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?
*/
//
//equal_range; includes; set_union; set_intersection; set_difference
#include <iostream>
#include <vector>
#include <algorithm>
int main1() {
std::vector<int> a = {0, 5, 5, -2, 7, 6, 6, 2, 0, 5};
std::vector<int> b = {8, -3, 7, 7, 5, 4, -4, 5, 2};
std::ranges::sort(a);
std::ranges::sort(b);
auto sub1 = std::ranges::unique(a);
a.erase(sub1.begin(), sub1.end());
auto sub2 = std::ranges::unique(b);
b.erase(sub2.begin(), sub2.end());
auto eq = std::ranges::equal_range(a, -1);
int posL = static_cast<int>(eq.begin() - a.begin());
int posU = static_cast<int>(eq.end() - a.begin());
int found = (eq.begin() != eq.end());
bool inc = std::ranges::includes(a, b);
std::vector<int> u(a.size() + b.size());
auto itU = std::ranges::set_union(a, b, u.begin());
u.erase(itU.out, u.end());
std::vector<int> inter(std::min(a.size(), b.size()));
auto itI = std::ranges::set_intersection(a, b, inter.begin());
inter.erase(itI.out, inter.end());
std::vector<int> diff(a.size());
auto itD = std::ranges::set_difference(a, b, diff.begin());
diff.erase(itD.out, diff.end());
int su = 0, si = 0, sd = 0;
for (int x : u) su += x;
for (int x : inter) si += x;
for (int x : diff) sd += x;
std::cout << found << "|" << posL << "|" << posU << "|" << inc << "|"
<< static_cast<int>(u.size()) << "|" << su << "|"
<< static_cast<int>(inter.size()) << "|" << si << "|"
<< static_cast<int>(diff.size()) << "|" << sd;
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 = {-1, 8, -6, -2, -7, 8, 0, 5};
auto it1 = std::ranges::find(v, 1);
auto it2 = std::ranges::find_if(v, [](int x){
return x > 0;
});
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?
*/
//
//replace, replace_if, std::erase, std::erase_if, reverse, rotate
#include <iostream>
#include <vector>
#include <algorithm>
int main1() {
std::vector<int> v = {-4, 2, -3, -5, 1, -1, -5, 7, 4, -4};
std::ranges::replace(v, -1, 7);
std::ranges::replace_if(v, [](int x){
return x > 2;
}, -6);
int removed1 = std::erase(v, 1);
int removed2 = std::erase_if(v, [](int const& x){
return x % 3 == 0;
});
std::ranges::reverse(v);
if (!v.empty()) {
int k = 1 % 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?
*/
//
//binary_search, lower_bound, upper_bound
#include <iostream>
#include <vector>
#include <algorithm>
int main1() {
std::vector<int> v = {9, 8, -4, 3, 8, 6, 1, -4, 7, 6, 2, 2};
std::ranges::sort(v);
bool found = std::ranges::binary_search(v, 5);
auto itL = std::ranges::lower_bound(v, 8);
auto itU = std::ranges::upper_bound(v, 0);
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;
}