Assignment Finding Average of Test Scores

For my code I have to have the user enter in 4 test scores from 5 different students and print the average, min, and max of each test. The problem I am having in my code is that I cant get it to print the average of exam 1 correctly. I have no idea what I am doing wrong here.

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
51
52
53
54
55
56
#include <stdio.h>
#include <iostream>
using namespace std;

int main()
{
	int i, j;
	double average;
	int min = -1;
	int maximum;
	int exams[5][4];

	for (int i = 0; i < 5; i++)
	{
		printf("Enter the scores for Student #%d\n", i + 1);
		for (int j = 0; j < 4; j++)
		{
			cin >> exams[i][j];
		
		}
	}



	for (int j = 0; j < 4; j++)
	{
		for (int i = 0; i < 5; i++)
		{
			average = average + exams[i][j];

			if (min < 0)
			{
				min = exams[i][j];
				maximum = min;
		
			}
			if (min >= exams[i][j])
			{
				min = exams[i][j];
			}
			if (maximum < exams[i][j])
			{
				maximum = exams[i][j];
			}
		}
		average = average / 5;
		cout << " Exam: " << j + 1 << endl;
		cout << " Average: " << average << endl;
		cout << " Min: " << min << endl;
		cout << " Max: " << maximum << endl;
		min = -1;
		average = 0;
	}
	system("pause");
	return 0;
}
Last edited on
Hello Qaz20177,

Welcome to the forum.

Some comments while I check out the program.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

It is ALWAYS a good practice and programming to initialize your variables. If your compiler is using the C++11 standards or after the easiest way to initialize variables is with empty {}s, e.g., int num{};. This will initialize the variable to 0 (zero) or 0.0 for a double. A "char" will be initialized to "\0". "std::string"s are empty to start with an do not need initialized. Should you need to you can put a number between the {}s.You can also Initialize an array, e.g., int aNumbers[10]{};. This will initialize all elements of the array to 0 (zero). A use of
int aNumbers[10]{ 1 }; will initial the first element to "1" and the rest of the array to "0".. Following the "1" with ", 2 ..." will initialize the array with the numbers that you have used up to the entire array.

Something to work on while look into the program.

Andy
Hello Qaz20177,

I believe what you need to understand first is a 2D array. You define the array as: int exams[5][4]; which is correct. Now when you think about look at it as 5 rows of 4 columns. I set this up for testing, so I would not have to enter number each time:
1
2
3
4
5
6
7
8
int exams[5][4]
{
	{ 50, 60, 70, 80 },
	{ 51, 61, 71, 81 },
	{ 52, 62, 72, 82 },
	{ 53, 63, 73, 83 },
	{ 54, 64, 74, 84 }
};

This helps to visualize how to look at the array.

Your for loops for dealing with the array are the right concept, but the wrong way to go about it. The for loops need to deal with the array as row first and then column. what you have is backwards.

Using loop counters like "i" and "j" may be easy, but can be confusing if you are not use to it. When working with a 2D array I fine this easier to under and keep things straight.

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
for (int row = 0; row < 5; row++)
{
	for (int col = 0; col < 4; col++)
	{
		average = average + exams[row][col];

		if (min < 0)
		{
			min = exams[row][col];
			maximum = min;
		}
		if (min >= exams[row][col])
		{
			min = exams[row][col];
		}
		if (maximum < exams[row][col])
		{
			maximum = exams[row][col];
		}
	}

	average = average / 4;  // <--- There are only 4 scores to each row.

	cout << "\n Exam: " << row + 1 << endl;
	cout << " Average: " << average << endl;
	cout << " Min: " << min << endl;
	cout << " Max: " << maximum << endl;
	min = 200;
	average = 0;
}


Fixing the for loops allowed the output to be correct.

Lines 5 and 22 does work, but most people would write line 5 as total += exams[row][col] and line 22 as average = total / MAXSCORES where MAXSCORES would be defined above main as:

1
2
3
constexpr std::size_t MAXSCORES{ 4 };
constexpr std::size_t MAXROW{ 5 };
constexpr std::size_t MAXCOL{ 4 };

This gives you one place to make changes if or when needed and you do not have to hunt through your program to find everything. MAXROW and MAXCOL can be used when you define the array and can be used in the for loops in place of the 5 and 4 you now have.

Hope that helps,

Andy
Hello Qaz20177,

Sorry an after thoughts.

Try to avoid using using namespace std; in your programs it may seem easy now, but WILL get you in trouble some day.

It is better to learn to qualify what is in the standard name space with "std::" and then to learn what is in the standard name space now while it is easy. And not all at once to try to keep a job.

What you are most likely to use for now is "std::cout", "std::cin" and "std::endl". About a week or so of typing this and you will not even notice that you are doing it.

And try to stay away from using "system" anything as this can be a potential problem.

This is something you can use in its place:
1
2
3
4
5
// <--- The next line may not be needed. Sometimes it is. If you have to press Enter to see what the "cout"
// prints then comment it out.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires heder file <limits>.
std::cout << "\n\n Press Enter to continue";
std::cin.get();

For other ideas you can read http://www.cplusplus.com/forum/beginner/1988/

One last note it is not good form or programming to mix C and C++ code. What you did for inputting the scores works, but it can be done with C++ syntax.


Hope that helps,

Andy
Thank you for the tips, I suppose I forgot to mention I am averaging each column and not row, my apologies.

This the array I am using:

1
2
3
4
5
int exams[5][4] = { { 50, 86, 75, 78 },
			   { 68, 76, 82, 0 },
			   { 74, 65, 80, 90 },
			   { 80, 79, 86, 92 },
			   { 45, 50, 77, 82 } };


Here is my updated code:

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
#include <stdio.h>
#include <iostream>
using namespace std;

int main()
{
	double average;
	int min = -1;
	int maximum;
	int exams[5][4] = { { 50, 86, 75, 78 },
						{ 68, 76, 82, 0 },
						{ 74, 65, 80, 90 },
						{ 80, 79, 86, 92 },
						{ 45, 50, 77, 82 } };

	

	for (int col = 0; col < 4; col++)
	{
		for (int row = 0; row < 5; row++)
		{
			average = average + exams[row][col];

			if (min < 0)
			{
				min = exams[row][col];
				maximum = min;
		
			}
			if (min >= exams[row][col])
			{
				min = exams[row][col];
			}
			if (maximum < exams[row][col])
			{
				maximum = exams[row][col];
			}
		}
		average = average / 5;
		cout << " \n Exam:" << col + 1 << endl;
		cout << " Average: " << average << endl;
		cout << " Min: " << min << endl;
		cout << " Max: " << maximum << endl;
		min = 200;
		average = 0;
	}
	system("pause");
	return 0;
}


and my output for the first exam still is:
Exam: 1
Average: -1.85119e+61
Min: 45
Max: 80

The other three exam outputs for average, min, and max are all correct.

Last edited on
Hello Qaz20177,

Averaging the columns that makes a big difference. In the nested for loop you should be able to just switch the "row" and "col" in everything. I will have to test it, bur I believe it should work.

Andy
Hello Qaz20177,

My bad I was being rushed when I thought everything needed to be changed. Only the for loops need to reverse with "col" in the outer loop and "row" in the inner loop. You will still have to access the the array as exams[row][col], but this time "row" will change before "col".

Hope that helps,

Andy
So I did that but I still get

Exam: 1
Average: -1.85119e+61
Min: 45
Max: 80

I am using visual studios right now to run it, the average should be 68.4. When you run the code are you getting 68.4 and not -1.85119e+61 for the first exam?
Hello Qaz20177,

In my first message I mentioned to ALWAYS initialize you variables. All I had to change was:
1
2
3
double average{};  // <--- Initializes to 0.0.
int min = -1;
int maximum{};  // <--- Initializes to 0. 

and it worked. It is the "average" variable having some garbage value that through everything off.

Initialize you variables and you will see a big difference.

Hope that helps,

Andy
Last edited on
Thank you it worked.
Any time.
Topic archived. No new replies allowed.