0

i want to know how does this work the logic behind it

I have this program that calculate the total of the even and the odd: #include <iostream> #include <vector> using namespace std; int main() { int even_total = 0; int odd_product = 1; std::vector<int> vector = {2, 4, 3, 6, 1, 9}; for (int i = 0; i < vector.size(); i++) { //what is the i < vector.size() for if (vector[i] % 2 == 0) { even_total = even_total + vector[i]; //how does this work } else { odd_product = odd_product * vector[i]; //how does this work } } std::cout << "sum of even: " << even_total << "\n"; std::cout << "product of odd: " << odd_product << "\n"; return 0; } and the when i hit run i get: sum of even: 12 product of odd: 27 //can some one tell me why is this the answer i know that i am so stupid but i would really appreciate your help :)

9th May 2020, 12:07 AM
lion warrior
lion warrior - avatar
3 odpowiedzi
+ 2
the total even = 2 + 4 + 6 = 12 the odd_product = 3 * 1 * 9 = 27
9th May 2020, 12:15 AM
rodwynnejones
rodwynnejones - avatar
0
but why are we multiplying for the the odd and can you please tell me what does the i in the [] stand for and it's function
9th May 2020, 12:21 AM
lion warrior
lion warrior - avatar
0
There no special reason for multipyling the odd numbers. The "I" in [] is your basic array notation to access the vector elements in the for loop.
9th May 2020, 7:04 AM
rodwynnejones
rodwynnejones - avatar