Hey all, I have an array in which the user inputs a series of numbers and when "-1" is entered the program is stopped and the average, max, and min of the numbers entered is displayed. I am able to get the average and max but for some reason the min is displaying "0" all the time. I'm not sure if its a stupid mistake that I am missing or not. Can anyone please point me in the right direction?

#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 sum = 0;
	double average = 0;
	double max = 0;
	double min = 0;

	for (int x = 0; x < counter; x++)
	{
		 sum += scores[x];

	}
	average = sum / counter;
	cout << "Average is " << average << endl;

	for (int x = 0; x < counter; x++)
	{
		if (scores[x] > max)
			max = scores[x];
	}
	cout << "Highest is " << max << endl;

	for (int x = 0; x < counter; x++)
	{
			if (scores[x] < min)
			min = scores[x];	
	}
	cout << "Lowest is " << min << endl;

}

You set the min to 0. Will any score be less than that?

For any min/max kind of problem, the best approach is to set min and max to the first data value, then start comparisons at the second element.

Consider a set with just one value. Isn't that value the min, and the max?

If you are initializing min to zero then it's always going to be lower than any (positive) number that you encounter in the list. Set minimum initially to the first element of the list (works when you're finding max too).

EDIT: Beaten by vmanes. Curses, foiled again!

If you are initializing min to zero then it's always going to be lower than any (positive) number that you encounter in the list. Set minimum initially to the first element of the list (works when you're finding max too).

EDIT: Beaten by vmanes. Curses, foiled again!

thanks for the quick reply. Is there any way you can give me an example? I feel like this is an easy fix but for some reason I am not processing it...

If you start out with min = 0, and say your list of scores was 90,93,100. Is 0 < 90, yes, minimum is still 0. Is 0 < 93, yes, minimum is still 0. Is 0<100, yes, minimum at the end is 0.

If you start out with min = first on the list when the list is 85 70 94, so min = 85. Is 85 < 70, No, so min = 70. Is 70 < 94, Yes, so minimum at the end = 70.

If you start out with min = 0, and say your list of scores was 90,93,100. Is 0 < 90, yes, minimum is still 0. Is 0 < 93, yes, minimum is still 0. Is 0<100, yes, minimum at the end is 0.

If you start out with min = first on the list when the list is 85 70 94, so min = 85. Is 85 < 70, No, so min = 70. Is 70 < 94, Yes, so minimum at the end = 70.

I understand what your saying but I'm having trouble putting it to my code. I thought I was setting it to first on the list when i coded min = scores[x]. Should i do the same with double min?

I understand what your saying but I'm having trouble putting it to my code. I thought I was setting it to first on the list when i coded min = scores[x]. Should i do the same with double min?

How is the min = scores[x]; code on line 42 ever reached if 0 is less than every element? scores[x] < min will always be false in that case when scores are all positive. When you initialize min, set it to the first element (i.e., scores[0]).

you should add this code "min=scores[0]"

min=scores[0]

for (int x = 1; x < counter; x++)
{
        if (scores[x] < min)
        min = scores[x];    
}
cout << "Lowest is " << min << endl;

Thanks for all the help. I finally figured it out. One last question I have is when -1 (which is used to terminate the program) is the first number entered by the user it still displays the average, highest, and lowest. Is there any way to stop it from displaying this and simply end the program when -1 is the first number entered?

Ummmm, test your counter variable? If it's still 0, do nothing?

how would I go about doing that? Is it possible to use a break statement to terminated the program when -1 is entered first?

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.