b. Write a complete c++ program that will keep reading positive integer values (>0) entered by user and calculate the sum of all even numbers entered and the sum of all odd numbers entered. Notice that the sentinel value (-100 in the example) is not counted towards the sum.
Sample output
v:\midterm2>3b.exe
3
5
7
9
1
100
-100
sum of even = 100
sum of odd = 25
I have no clue how to do this part can you guys help me with this too?
In your first post:
a. is wrong. You must put a semicolon at the end. double values[100];
b. is wrong. You must put a semicolon at the end, remove the braces, and exclude the type, since you've already declared it in part a. values[0] = 2.3;
c. Arrays of size n may be indexed with values 0 through n-1. Therefore, the last element in a 100 element array would be index 99. You may fetch the value, multiply it by two, and store it back in the same place. values[99] = values[99] * 2; or values[99] *= 2;
If you want to keep your while loop as is, then do two things.
1. Delete line 8.
2. On line 4, initialize number to something > 0. int number = 1;
Step 2 isn't intuitive when you read it, because you end up throwing that value out anyway, but in your case it would be necessary to initialize it > 0 to be 100% sure that your while loop would execute at least once. If you used a do-while, you'd be guaranteed that your loop would execute at least once.
EDIT: Also,
I haven't learned do-while loop
Then learn it. If you understand while loops then you could understand a do-while loop in seconds. Take a look at this page. You'll pick it up really fast, I guarantee. http://www.cplusplus.com/doc/tutorial/control/#dowhile