Arrays

This is for a class assignment.

"Write a C++ program that accepts test scores. The program should have a loop that allows the user to continue entering numbers until an end sentinel of -100 is entered. Once the user has input all test scores the program must display them back to the user and display the class average for that test.

You will also be calculating the Highest and Lowest grades as well as how many test scores were below the average."

We are just starting arrays, and I am beyond confused on this. I can get the very basics of setting up a for loop array, taking in values and then displaying them. He said to use a while loop so that if a user entered a -100 it close the loop (sentinel). Every time however, I keep getting error numbers on the average. No matter what I use. If anyone can help on this I would really appreciate it. Thank you.

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

int main()
{
	const int TESTS = 20;
	int scores[TESTS];
	int numValues = 0;
	int grade;
	double total = 0;
	double average;

	cout << "Enter a test grade or -100 to quit : " << endl;
	cin >> grade;
	while (grade != -100 && numValues < TESTS)
	{
		scores[numValues] = grade;
		numValues++;
		cout << "Enter a test grade or - 100 to quit : " << endl;
		cin >> grade;
	}
	for (int count = 0; count < numValues; count++)
	{
		cout << scores[count] << endl;
		total += scores[count];
	}

	average = total / scores[numValues];

	cout << "The average is " << average << "." << endl;


	system("pause");
	return 0;
}
On line 28, you don't need to use the array at all. Calculating the average only requires knowing the sum and the size.
Line 28:
average = total / scores[numValues];

Should be:

average = total / numValues;

I hope it helps.
Last edited on
Okay, thank you for that. Now I have run into my last 2 problems.
First I need to edit the code to not accept any negative numbers, while keeping the sentinel at -100.
Second, whenever I run the check for lowest, I always get -858993460, which I think is a location in memory, since the number doesn't change. Any ideas?

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
57
58
59
#include <iostream>
using namespace std;

int main()
{
	const int TESTS = 20; //Accepts 20 test scores.
	int scores[TESTS];
	int numValues = 0;
	int grade;
	double total = 0;
	double average;
	int high;
	int low;

	//Get grades.
	cout << "Enter a test grade or -100 to quit : " << endl;
	cin >> grade;
	while (grade != -100 && numValues < TESTS)
	{
		scores[numValues] = grade;
		numValues++;
		cout << "Enter a test grade or - 100 to quit : " << endl;
		cin >> grade;
	}

	//Totals test scores.
	for (int count = 0; count < numValues; count++)
	{
		cout << "Test " << (count+1) << " is " << scores[count] << endl;
		total += scores[count];
	}

	//Average of scores.
	average = total / numValues;

	cout << "The average is " << average << "." << endl;

	//Find highest and lowest.
	high  = low = scores[0];
	for (int count = 1; count < TESTS; count++)
	{
		if (scores[count] > high)
			high = scores[count];
		else if (scores[count] < low)
			low = scores[count];
	}
	//Display the lower than average scores.
	for (int count = 0; count < numValues; count++)
	{
		if (scores[count] < average)
			cout << "Test " << (count + 1) << " lower than average." << endl;
	}
	
	cout << "Highest: " << high << "." << endl;
	cout << "Lowest: " << low << "." << endl;

	system("pause");
	return 0;
}
I would use the sentinel if I don't know how many items are in the list (i.e. while (grade != -100). However, it is possible to perform the operations requirements for your assignment.

For the first situation, I would use an if/else statement inside the while loop on line 18.

Like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	while (grade != -100 && numValues < TESTS)
	{
		if (/*grade is negative number*/)
		{
                     //Notify the user the input is invalid.
                    // Ask the user to enter a new grade or -100 to quit.
		}
		else
		{
			scores[numValues] = grade;
			numValues++;
			cout << "Enter a test grade or - 100 to quit : " << endl;
			cin >> grade;
		}
	}


Now for your second situation, on line 40:
for (int count = 1; count < TESTS; count++)

Two things:
First, your int count = 1 means that is starting to count your second score, not your first score. Remember, the 0 is the first element, 1 the second and so on.
Second, I crossed a line of the wrong info I gave you.
You are running the loop until count is less but not equal to TESTS. This may work if you are inputting all 20 scores (see line 6), but what if the user does not have all 20 scores? We use the sentinel to stop the list (i.e. -100). The problem with that is the loop will continue until it reaches 20 scores, which in turn will display undesired results.

One way to fix it is to change the for loop from for (int count = 1; count < TESTS; count++) to for (int count = 1; count < numValues; count++) It will run the loop until the last count in which the user inputted the sentinel value.

I hope this helps.
Last edited on
Topic archived. No new replies allowed.