Member Avatar for aexi

Calculating Average and high/lowest test score
Hello, I am getting frustrated because I am currently trying to figure out how to solve this program:

Write a program that prompts the user for test scores (doubles). The user enters -1 to stop the entry. After all of the test scores have been entered, calculate the average, the highest and the lowest test score.

So far this is what I have:

#include <iostream>

using namespace std;

int main()
{
double score, average, highest, lowest;
int counter = -1;
do
{
counter++;
cout << "Please enter a score (enter -1 to stop): ";
cin >> score[counter];
} while (scores[counter] >= 0);


cout << "Highest is ";
cin >> highest;
cout << "Lowest is ";
cin >> lowest;

// I am stuck at this point which is to CALCULATE AND DISPLAY THE AVERAGE,
// THE HIGHEST AND LOWEST TEST SCORE


}

Hint: within your loop, keep track of the highest and lowest values as they are coming in (by comparison with the existing highest and lowest values). Think about what you need to keep track of to take the average and keep track of that in your loop as well.

Trace the code out by hand for what you need to do and then build a loop out of it using the above information.

Member Avatar for aexi

I'm pretty new to c++ and this class I am taking is going really fast for me which is why I'm having a hard time with these problems. I have tried to solve it again, and got what is listed below. However, it keeps giving me errors and I still do not understand how to get the highest/lowest scores that the user enters.

#include <iostream>

using namespace std;

int main()
{
	double scores[75];
	int counter = -1;
	do
	{
		counter++;
		cout << "Please enter a score (enter -1 to stop): ";
		cin >> scores[counter];
	} while (scores[counter] >= 0);

	double average, highest, lowest;
	int sum;
	sum = 0;
	sum = sum + scores;
	average = sum / counter;

	cout << "Average is ";
	cin >> average;
	cout << "Highest is ";
	cin >> highest;
	cout << "Lowest is ";
	cin >> lowest;
	
}

You actually don't even need arrays for this. It's simpler if you don't use one.
Look at line 19. It's not really doing anything (especially since scores is an array). What about if you moved that up within your loop and kept track of the sum as you added in each new element.

For the smallest and largest, consider what you would do if I told you that you could hold two pieces of paper, one with the largest number so far and one with the smallest. Now, walk along a list of papers and compare what you have in your hand to the current value. If it's greater than the maximum, pick it up, if it's less than the minimum pick it up. Now translate that into variables and do it in your loop.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.