Trouble with some Loop Examples

I have some questions in regards to the loop examples, and it's two of them. The first one is using accumulator in while loops, and I wrote down my question in the code. Basically for the first program, the "total = total + points" has to go on top of the code inside the while loop, but I really wanted to put it at the bottom inside the loop; under the "cin >> points;". I don't understand the logic on why it's supposed to go on top.


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
  	#include <iostream>
	using namespace std;
	int main()
	{

	int game = 1;
	int points;
	int total = 0;

	cout << "Enter the number of points your team has entered, thank you!\n";
	cout << "Enter negative 1 when you are done!\n";
	cout << "Enter game points for game 1!\n";
	cin >> points;
	
	while (points != -1) // Everything other than 1 will pass.
	{
		total = total + points; // Why does this go here?
		game++;
		cout << "Enter the points for game " << game << endl;
		cin >> points;	
		// And not here? (total = total + points;)
	}
	
	cout << "Total points are " << total << endl;
	
	return 0;
	}


My other question is regarding For Loops, after reading my school book, I stumbled upon a example program I am very confused on why the "average = total / numTests;
cout << "The average score for student " << student;
cout << " is " << average << ".\n\n";" I just don't understand the logic on why it's supposed to go there.

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
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	int numStudents; // Number of students
	int numTests; // Number of tests per student
	double total; // Accummulator for total scores
	double average; // Average test score

	cout << fixed << showpoint << setprecision(1);

	// Get the number of students.
	cout << "This program average test scores.\n";
	cout << "For how many students do you have scores? ";
	cin >> numStudents;
	
	// Get the number of test scores per student.
	cout << "How many test score does each student have? ";
	cin >> numTests;

	// Determine each student's average score.
	for (int student = 1; student <= numStudents; student++)
	{
		total = 0; // Initialize the accumulator.
		for (int test = 1; test <= numTests; test++)
		{
			double score;
			cout << "Enter score " << test << " for ";
			cout << "student " << student << ": ";
			cin >> score;
			total += score;
		}
		average = total / numTests; // Why does this go here?
		cout << "The average score for student " << student; // Why does this go here?
		cout << " is " << average << ".\n\n"; // Why does this go here?
	}



	return 0;
}


Thank you! :)
Last edited on

Basically for the first program, the "total = total + points" has to go on top of the code inside the while loop, but I really wanted to put it at the bottom inside the loop; under the "cin >> points;". I don't understand the logic on why it's supposed to go on top.
1
2
3
4
5
6
7
8
9
10
11
	cout << "Enter game points for game 1!\n";
	cin >> points;
	
	while (points != -1) // Everything other than 1 will pass.
	{
		total = total + points; // Why does this go here?
		game++;
		cout << "Enter the points for game " << game << endl;
		cin >> points;	
		// And not here? (total = total + points;)
	}

Because you've entered a value into points outside the while loop. If you had it at the end of the while loop, your input for game 1 will be overwritten and ignored.
If you really want it at the end of the while loop, you can write it like this:
1
2
3
4
5
6
7
8
9
int points = 0; // initialise
while (points != -1)
{
    cout << "Enter the points for game " << game << endl;
    cin >> points;  
    //total = total + points;
    total += points; // prefer the shorthand form
    game++;
}



My other question is regarding For Loops, after reading my school book, I stumbled upon a example program I am very confused on why the "average = total / numTests;
cout << "The average score for student " << student;
cout << " is " << average << ".\n\n";" I just don't understand the logic on why it's supposed to go there.
1
2
3
4
5
6
7
8
for (int test = 1; test <= numTests; test++)
{
    double score;
    cout << "Enter score " << test << " for ";
    cout << "student " << student << ": ";
    cin >> score;
    total += score;
}

This loop calculates the total score for each test.
average = total / numTests; // Why does this go here?
This just gets the average.
1
2
cout << "The average score for student " << student; // Why does this go here?
cout << " is " << average << ".\n\n"; // Why does this go here? 

This prints out the calculated average.
1
2
3
4
5
6
7
	cin >> points;
	while (points != -1) // Everything other than 1 will pass.
	{
		total = total + points; // Why does this go here?
		cin >> points;	
		// And not here? (total = total + points;)
	}

As said, the first input has already happened before the loop.

Furthermore, (and the version by integralfx has the issue too) you want to:
1. get input
2. check the input
3. use valid input

The order is important; you do not want to add the -1 to total. You do not want to count the -1 as a game.

You have inputs on lines 1 and 5.
Line 2 has the check.
Line 4 uses input and it does it after the check on line 2 (and only if input is valid).
Line 6 would use the input whether it is valid or not.



for each student:
1
2
3
4
5
6
7
8
9
double total = 0; // Initialize the accumulator.
for (int test = 1; test <= numTests; ++test)
{
	double score;
	cin >> score;
	total += score;
}
average = total / numTests; // Why does this go here?
cout  << average << ".\n\n"; // Why does this go here? 

Why does one compute and show data about a student after all input of that student has been received?
You cannot show earlier, for the average needs all the input.
You cannot show later, for you don't store data for every student separately.
Thanks for the replies, I understand the first program. The second program I am still a bit confused. The part on " average = total / numTests; cout << "The average score for student " << student;
cout << " is " << average << ".\n\n"; "

Is still confusing to be a bit. I don't understand why it's going in the first for loop, and it has to be in that specific place to work. I could be over thinking it, but I'll try to play with it some more. Thanks for the help so far!

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
34
35
36
37
38
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	int numStudents; // Number of students
	int numTests; // Number of tests per student
	double total; // Accummulator for total scores
	double average; // Average test score

	cout << fixed << showpoint << setprecision(1);

	cout << "This program average test scores.\n";
	cout << "For how many students do you have scores? ";
	cin >> numStudents;

	cout << "How many test score does each student have? ";
	cin >> numTests;

	for (int student = 1; student <= numStudents; student++) // First loop allows to see how many students there are.
	{
		total = 0; // Initialize the accumulator.
		for (int test = 1; test <= numTests; test++) 
		{
			double score;
			cout << "Enter score " << test << " for ";
			cout << "student " << student << ": ";
			cin >> score;
			total += score; // This has to go with the score, because you want to find the average score so we must set it an accumulator.
		}
		average = total / numTests; // Why does this go here? // Still confused on why these specific code go under the first loop statement, even after trying to switch it around.
		cout << "The average score for student " << student; 
		cout << " is " << average << ".\n\n"; 
	}

	return 0;
}
The point of the program is to generate the average for each student. You generate the first student's average, then the second student's average, and so on. Does that make it clear why those lines are where they are?

The lines that are in the wrong place are 9 and 10. Since we're generating a total score and average score for each student, it would be better practise to declare those variables inside the same loop.
Last edited on
I don't understand why it's going in the first for loop, and it has to be in that specific place to work.

What other places do you had in mind?

Explain, why each of them would (not) work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// A
for (int student = 1; student <= numStudents; student++)
{
	// B
	total = 0; // Initialize the accumulator.
	for (int test = 1; test <= numTests; test++) 
	{
		// C
		double score;
		cout << "Enter score " << test << " for ";
		cout << "student " << student << ": ";
		cin >> score;
		// D
		total += score;
		// E
	}
	// F
}
// G 

There are 7 places (A,B,C,D,E,F,G) suggested for your lines 32-34 (the calculate and show average). Place F is actually used. Were these among the places that you did consider?
Hello, sorry for the late reply. After reading what you have given me, I now know why it can't go in A, B, C, and D. I included comments in the program on why it can't get in. The only real letters I have concern with is E, F, and G. I just don't know why I am not getting it really, I just can't understand why we can't just put it outside of the loop and why it must stay in side.

I think maybe because we put it in the first loop because we are using the "student" variable that it goes up by 1? Thanks.

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	int numStudents; // Number of students
	int numTests; // Number of tests per student
	double total; // Accummulator for total scores
	double average; // Average test score

	cout << fixed << showpoint << setprecision(1);

	cout << "This program average test scores.\n";
	cout << "For how many students do you have scores? ";
	cin >> numStudents;

	cout << "How many test score does each student have? ";
	cin >> numTests;

	// 'A' It can't go in A because the variable "student" is undefined, hence, an error comes up.
	
	for (int student = 1; student <= numStudents; student++) // First loop allows to see how many students there are.
	{
		// 'B' Average not being used here, it will give an error.
		
		total = 0; // Initialize the accumulator.
		for (int test = 1; test <= numTests; test++)
		{
			// 'C' Because it goes from top to bottom, the total is not actually being used here and hence 
			// even though it will run, it will give weird and wrong statements, something not the program was designed for.
			
			double score;
			cout << "Enter score " << test << " for ";
			cout << "student " << student << ": ";
			cin >> score;
			// 'D' You will get the simimilar issue to "'C'".
			total += score; // This has to go with the score, because you want to find the average score so we must set it an accumulator.
			// 'E' Confused on why it can't go here, even with testing it.
		}
		// 'F' Even though this works, I don't understand the logic behind it.
		average = total / numTests;
		cout << "The average score for student " << student;
		cout << " is " << average << ".\n\n";
	}
	// 'G' Same thing here, I don't understand the logic behind it.


	return 0;
}
Lets try this:
* How many times the line E is executed during the program?
* How many times the line F is executed during the program?
* How many times the line G is executed during the program?

* How many averages does one student have?
Hello there, firstly I greatly appreciate your help with this. I finally understand everything now on why it's supposed to go there. My final question is regards to the location of "average = total / numTests;". I decided to play around with it and put it inside the inner for loop and hence, it still worked? Doesn't this get confused with other loops, or it does not matter? Sorry for the late response again, been really busy! :)

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
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	int numStudents; 
	int numTests; 
	double total; 
	double average; 

	cout << fixed << showpoint << setprecision(1);

	// Get the number of students.
	cout << "This program average test scores.\n";
	cout << "For how many students do you have scores? ";
	cin >> numStudents;

	// Get the number of test scores per student.
	cout << "How many test score does each student have? ";
	cin >> numTests;

	// Determine each student's average score.
	for (int student = 1; student <= numStudents; student++)
	{
		total = 0; // Initialize the accumulator.
		for (int test = 1; test <= numTests; test++)
		{
			double score;
			cout << "Enter score " << test << " for ";
			cout << "student " << student << ": ";
			cin >> score;
			total += score;
			// 'E' // Line E is executed only 1 times per student, when I enter 2 and 3.
			average = total / numTests;  // If I put just this line in the "inner" for loop, it will still work? Don't know why.
		}
		// 'F' Only choice is this.
		cout << "The average score for student " << student;
		cout << " is " << average << ".\n\n";
	}
	// 'G' // This is not even executed because student is undefined.
	

	return 0;
}
Last edited on
closed account (48T7M4Gy)
average is actually changing in value as each value of test is incremented along with the input value of score.

If you add a cout at line 35 ( 35a) you can see it happen. The average variable value changes.

The reason you get the same answer at the end is variable total has been updated to its final value, keeping in mind numTests has remained the same.
closed account (48T7M4Gy)
However, this removes the apparent strangeness:
1
2
    average = total / test; // RUNNING AVERAGE
    cout << "Total: " << total  << " Average: " << average << '\n';
Last edited on
Cool, got it. Thanks for the help! :) Will mark as resolved.
Topic archived. No new replies allowed.