logo

Crowdly

Browser

Add to Chrome

Object Oriented Programming C++

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 = {4, -4, -4, -5, 3, 4, 4, -4, 5, 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;

}

View this question

#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 = 3;

   int b = 17;

   auto op = [a, &b](int x){

      b -= a - x;

      return x + b;

   };

   std::cout << apply_twice(1, op) + op(6);

   return 0;

}

View this question

#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, -1, -3, 7, 8, 8, -2, 6, -9, 8, 0};

    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) < absYes;

    });

    int k1 = std::min(4, 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 < 0;

    });

    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;

}

View this question

#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 = {1, -6, -7, -2, -3, 7, 5, 8, -2, 5};

    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 > 2;

    });

    int copied = static_cast<int>(r1.out - b.begin());

    std::ranges::transform(v, c.begin(), [](int x){

        return x + 5;

    });

    std::ranges::fill(a.begin() + 1, a.begin() + 4, -1);

    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;

}

View this question

#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 = {8, -1, -2, -5, 1, 5, 8, 4, 2, 0, -5};

    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) < absYes;

    });

    int k1 = std::min(2, 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;

}

View this question

#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, 4, 3, -2, 2, 3, -2, -4, 8, -4};

    std::vector<int> b = {0, 1, -1, 3, -2, 6, -2, 8, -3};

    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, -2);

    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;

}

View this question

#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 = 2;

   int b = 12;

   auto op = [a, &b](int x){

      b -= a - x;

      return x + b;

   };

   std::cout << apply_twice(3, op) + op(2);

   return 0;

}

View this question

#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, 6, -8, -1, -3, -9, 9, 7};

    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;

}

View this question

#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 = {4, 3, -1, -5, -4, -2, -1, -4, 2, 1};

    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;

}

View this question

#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 = {3, 9, -1, -4, 1, 0, -5, -5, -3, 6, 1, -4};

    std::ranges::sort(v);

    bool found = std::ranges::binary_search(v, 9);

    auto itL = std::ranges::lower_bound(v, 1);

    auto itU = std::ranges::upper_bound(v, -5);

    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;

}

View this question

Want instant access to all verified answers on emokymai.vu.lt?

Get Unlimited Answers To Exam Questions - Install Crowdly Extension Now!

Browser

Add to Chrome