I have mid term test today!

there is sample test but professor does not give out the answer for the sample test :/. Help me!!

a. Declare an array named values that can store at most 100 values like .5, 1.3, ...
my answer: double values[100]

b. Write one statement that assign the value 2.3 to the very first element in the array declared in a.

my answer: double values[0] = {2.3}

c. Write one statement that resets the value of the very last element in the array declared in a to twice its original value.

my answer: do not know QQ

help me people, thanks
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?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;
int main () {
	int number;
	int sumeven=0;
	int sumodd=0;
	
	cin >> number;
	while (number>0) {
	    cin >> number;
	    if (number % 2 == 0) {
	        sumeven = sumeven + number;
	    }
	    else
	        sumodd = sumodd + number;
	}
	cout << sumeven << "\n";
	cout << sumodd << "\n";
	return 0;
}

Above is my code for the second question b.
I am not sure what I am missing.
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;
Your code snippet is really close; remove line 8 and consider using a do-while loop instead of a plain while loop.
1
2
3
4
5
6
//...
do
{
    //... your logic
} while (number > 0);
//... 
@booradley60
I haven't learned do-while loop
do i need to use do-while loop for this?
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
Last edited on
@booradley60
I solved it, it was really simple.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;
int main () {
	int number=1;
	int sumeven=0;
	int sumodd=0;
	
	while (number>0) {
	    cin >> number;
	    if (number % 2 == 0) {
	        sumeven = sumeven + number;
	    }
	    else
	        sumodd = sumodd + number;
	}
	sumeven = sumeven -number;
	cout << sumeven << "\n";
	cout << sumodd << "\n";
	return 0;
}
Topic archived. No new replies allowed.