Sequences


Here is the question:

Write a program to compute, and later output to the screen, the average of an arbitrary sequence of integers entered, via the keyboard, by the user. To indicate the end of the sequence, the user inputs a value of -1, a value that is not computed as part of the average.

This is what I have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

#include <iostream>

using namespace std;

int main() {
	const int SENTINEL = -1;
	int num, count, sum;
	double avg;

	do {
		cout << "Enter a sequence of numbers (-1 to end the sequence): ";
		cin >> num;
		
		// equation for finding sum of the integers in the sequence, not including the -1 (for finding sum)
		// equation for finding the number of integers in the sequence (for finding count)

	} while( num != SENTINEL );

	avg = sum / count;
	
	cout << "Average: " << avg << endl;
	
	return 0;
}


The comments highlight where I'm struggling

This is what I've tried:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

int main() {
	const int SENTINEL = -1;
	int num, count, sum;
	double avg;

	do {
		cout << "Enter a sequence of numbers (-1 to end the sequence): ";
		cin >> num;
		
		// equation for finding sum of the integers in the sequence, not including the -1 (for finding sum)
		sum += num;
		// equation for finding the number of integers in the sequence (for finding count)
		count ++;

	} while( num != SENTINEL );

	avg = sum / count;
	
	cout << "Average: " << avg << endl;
	
	return 0;
}


But it's not working.

Any help would be appreciated.
Your idea is almost exactly what you need. However:
- You do not initialize either of count or sum.
- You will be including the -1 in the sum as well as counting it; you will want to rework your portion inside the loop to ensure that doesn't happen.
Ok,

I think I'd initialize sum = 0, right?

I don't know what I'd initialize count too, though, I suppose 0 as well? Considering it is at zero before I anything is entered via the keyboard.

This:

- You will be including the -1 in the sum as well as counting it; you will want to rework your portion inside the loop to ensure that doesn't happen.

I do not know how to achieve.
Last edited on
Ok,

I've done this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

#include <iostream>
#include <iomanip>

using namespace std;

int main() {
	const int sValue = -1;
	int num, count = 0, sum = 0;
	double avg;

	do {
		cout << "Enter a sequence of numbers (-1 to end the sequence): ";
		cin >> num;
		
		// equation for finding sum of the integers in the sequence, not including the -1 (for finding sum)
		sum += num;
		sum ++;
		// equation for finding the number of integers in the sequence (for finding count)
		count ++;

	} while( num != sValue );

	avg = sum / count;
	
	cout << "Average: " << fixed << setprecision(2) << avg << endl;

	return 0;
}


I'm making progress. However, the output is still not quite right:

Enter a sequence of numbers (-1 to end the sequence): 1 2 3 -1
Enter a sequence of numbers (-1 to end the sequence): Enter a sequence of number
s (-1 to end the sequence): Enter a sequence of numbers (-1 to end the sequence)
: Average: 2.00
Press any key to continue . . .


My average for a sequence like this is correct. But for a sequence like this:

Enter a sequence of numbers (-1 to end the sequence): 5 10 0 0 -1
Enter a sequence of numbers (-1 to end the sequence): Enter a sequence of number
s (-1 to end the sequence): Enter a sequence of numbers (-1 to end the sequence)
: Enter a sequence of numbers (-1 to end the sequence): Average: 3.00
Press any key to continue . . .


The average of this sequence should be 3.75, but the program does not recognizing this and I don't know why not.

Also, I don't understand why the program runs:

cout << "Enter a sequence of numbers (-1 to end the sequence): "

a number of times apparently equivalent to the value of count

Help?
Last edited on
1
2
3
4
5
6
//Put cout here outside do/while
cout << "Enter a sequence of numbers (-1 to end the sequence): ";

do {
		
		cin >> num;


Why are you incrementing sum?

All you should need is the sum+=num; part
Last edited on
@thomaselder84: I suspect TC is incrementing sum in order to "cancel out" the -1 that they are adding in the loop, though if so it should be done only once.

TC: Note that you are using only integers, so when you divide, you're result will be an integer. Make one of the division operands a floating point type (either by casting or simply changing the type directly) to get a floating point result.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <iomanip>

using namespace std;

int main() {
	const int sValue = -1;
	int num, count = 0, sum = 0;
	double avg;

        cout << "Enter a sequence of numbers (-1 to end the sequence): ";

	do {
		
		cin >> num;
		
                if(num!=sValue){
		      sum += num;
                      count++;
                      }
                else
                      ;

             

		} while( num != sValue );

	avg = sum / count;
	
	cout << "Average: " << fixed << setprecision(2) << avg << endl;

	return 0;
}
Last edited on
@Zhuge Your suspicion is correct, the increment to sum is to "cancel out" the -1. Although, I'm not sure what you mean by "though it should only be done once". Does that mean I should move it outside of the loop? Perhaps, like this:

1
2
3
4
5
6
7
8
9
10
11
12
13

do {
		cin >> num;
		
		// equation for finding sum of the integers in the sequence, not including the -1 (for finding sum)
		sum += num;
		// equation for finding the number of integers in the sequence (for finding count)
		count ++;

	} while( num != sValue );

sum ++


Because that is throwing off the calculation of the average:

Enter a sequence of numbers (-1 to end the sequence): 1 2 3 -1

Average: 1.50

Press any key to continue . . .
Last edited on
It's the setprecision that's rounding it. The number was probably something like 3.895 or something. setprecision would round that to 3.80

A double and a float will behave in the same way. Only difference is that double can hold larger values.
Last edited on
As for the precision of the average, I thought that declaring avg as a double would allow it to have decimal points.

Obviously that's not the case.

Is there a way to do it using type double, though? My proff has stated he prefers the use of doubles over floats because they're essentially the same, but doubles are more versatile.

If I write the code like this:

1
2
3
4
5

avg = (double)sum / (double)count;
	
	cout << "Average: " << fixed << setprecision(2) << avg << endl;


My output is still rounding off:


Enter a sequence of numbers (-1 to end the sequence): 5 10 0 0 -1

Average: 3.80

Press any key to continue . . .
Last edited on
Is there a way to stop setprecision from rounding? Basically, is there a way for me to get:


Enter a sequence of numbers (-1 to end the sequence): 5 10 0 0 -1

Average: 3.75

Press any key to continue . . .

Instead of:


Enter a sequence of numbers (-1 to end the sequence): 5 10 0 0 -1

Average: 3.80

Press any key to continue . . .


Last edited on
Can you show an actual test case with inputs and outputs .... with setprecision at 2... it shouldn't round 3.75 to 3.80 ... it would take something like 3.749 and round it to 3.75
Last edited on
Or just try changing the set precision to 3 instead of 2 and see what that does
Topic archived. No new replies allowed.