Q:

Write a program to use a conditional operator to find the elements in a vector<int> that have odd value and double the value of each such element

0

Write a program to use a conditional operator to find the elements in a vector<int> that have odd value and double the value of each such element.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <iostream>
#include <vector>

int main() {
  std::vector<int> iv;
  int i;
  while (std::cin >> i)
    iv.push_back(i);
  for (auto &elem : iv)
    elem = elem % 2 ? elem + elem : elem;
  for (const auto &elem : iv)
    std::cout << elem << ' ';
  std::cout << std::endl;

  return 0;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now