127k views
3 votes
Write a program that:

Takes the list lotsOfNumbers and uses a loop to find the sum of all of the odd numbers in the list (hint: use Mod).
Displays the sum.
Situation B
Write a procedure that takes a positive integer as a parameter. If the number given to the procedure is no more than 30, the procedure should return the absolute difference between that number and 30. If the number is greater than 30, the procedure should return the number doubled.

1 Answer

5 votes

Situation A:

#include <iostream>

#include <vector>

#include <string>

int main(int argc, char* argv[]) {

//Fill it by own.

std::vector<int> lotsOfNumbers = {0,1,2,3,4,5,6,7,8,9};

//Loop to find odd numbers.

for(int i=0;i<lotsOfNumbers.size(); i++) {

if(lotsOfNumbers[i]%2!=0) std::cout << lotsOfNumbers[i] << std::endl;

else continue;

}

return 0;

}

Situation B:

#include <iostream>

#define check(x) ((x>30) ? 2*x : 30-x)

int main(int argc, char* argv[]) {

std::cout << check(15) << std::endl;

std::cout << check(-2) << std::endl;

std::cout << check(30) << std::endl;

std::cout << check(78) << std::endl;

return 0;

}

Write a program that: Takes the list lotsOfNumbers and uses a loop to find the sum-example-1
Write a program that: Takes the list lotsOfNumbers and uses a loop to find the sum-example-2
User Kinjelom
by
7.9k points