Calculating Minimum Values

Hello. I am writing a program which collects data from the user and once all the data is entered, calculates the mean, and the highest and lowest numbers entered.

I got the program to calculate the mean and figure out the highest number but I cannot figure out the lowest number. Help, please?

Also, the program stops running when the user enters anything less than 0 however the mean/highest and lowest numbers COUTs still appear. How can I make the program completely break if the user enters anything less than 0?

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
*/ 

/* After the user enters scores (entering -1 will stop the loop),
calculate and display the highest and lowest test scores and the 
average of all the test scores.
*/ 

#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);
	//The above section must be included in the program, left untouched.

	
	//This section finding the mean of the entered numbers works.
	double total=0;
	for (int x = 0; x < counter; x++)
	{
		total += scores[x];
	}
	cout << "Average is " << (total/(counter)) << endl;
	

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

	// The minimum value is constantly being returned as 0.
	double minval=0;
	for (int x=0; x>counter; x--)
		{
			minval = scores[x];
		}
	cout << "Lowest is " << minval <<endl;
}
I'd be tempted to initialise minval with maxval:

 
double minval=maxval;


1
2
3
4
for (int x = 0; x < counter; x++)
{
	maxval = scores[x];
}


I would think you should only set maxval to the score if the score is greater than the current maxval?
I gave that a shot but it returned the maxval equal to the minval...
And as for your setting maxval to the score if the score is greater than the current maxval, I have to have the program figure out the max/min values after all the numbers have been entered, not as they are being entered.
Currently all you do is set the maxval equal to each score in turn. So it obviously ends up with the last score as its value.

What you need to do is check IF the score is larger than the current maxval. ONLY IF that is true then set the maxval to the score.
I changed it around to if statements but now the cout for min and max values doesn't display.
What am I 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
#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 total=0;
	for (int x = 0; x < counter; x++)
	{
		total += scores[x];
	}
	cout << "Average is " << (total/(counter)) << endl;
	
	double maxval=0;
	double minval=0;
	do
	{
		if (scores[counter]>maxval)
		{
			maxval = scores[counter];
		}
		if (scores[counter]<minval)
		{
			minval = scores[counter];
		}
	} while(cin>>scores[counter]);
	cout << "Highest is " << maxval << endl;
	cout << "Lowest is " << minval <<endl;
}
The if() statements are the right thing to do. But why have you put them in a do{}while() loop? What's that for? Your original for(;;) loop was correct.
Okay, I changed the loops back for the for loops. However, they still aren't displaying anything when I run the program.


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
#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 total=0;
	for (int x = 0; x < counter; x++)
	{
		total += scores[x];
	}
	cout << "Average is " << (total/(counter)) << endl;
	
	double maxval=0;
	double minval=0;
	while(cin>>scores[counter])
	{
		if (scores[counter]>maxval)
		{
			for (int x = 0; x < counter; x++)
			{
				maxval = scores[counter];
			}
		}
		
		if (scores[counter]<minval)
		{
			for (int x=0; x>counter; x--)
			{
				minval = scores[counter];
			}
		}
	} 
	cout << "Highest is " << maxval << endl;
	cout << "Lowest is " << minval <<endl;
	
}
It worked very well, but perhaps you do not like this solution.

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
/* After the user enters scores (entering -1 will stop the loop),
calculate and display the highest and lowest test scores and the 
average of all the test scores.
*/ 

#include <iostream>
using namespace std;
int main()
{
	double scores[75];
	int counter;
	double total = 0;
	for(counter = 0 ; counter < 75 ; counter++) {
		cout << "Please enter a score (enter -1 to stop): ";
		cin >> scores[counter];
		if (scores[counter] < 0) {
            break;
        } else {
            total += scores[counter];
        }
	}
	//The above section must be included in the program, left untouched.

	
	//This section finding the mean of the entered numbers works.
	cout << "Average is " << (total/counter) << endl;
	
	double maxval=0;
	for (int x = 0; x < counter; x++)
		{
			if (scores[x] >= maxval) {
                maxval = scores[x];
            }
		}
	cout << "Highest is " << maxval << endl;

	// The minimum value is constantly being returned as 0.
	double minval = scores[0];
	for (int x=0; x < counter; x++)
		{
			if (scores[x] <= minval) {
                minval = scores[x];
            }
		}
	cout << "Lowest is " << minval <<endl;
}

That does work but for my assignment, I'm not allowed to change this code segment:
Any other ideas?
1
2
3
4
5
6
7
8
9
10
11
12
#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);


This is what you want?

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
/* After the user enters scores (entering -1 will stop the loop),
calculate and display the highest and lowest test scores and the 
average of all the test scores.
*/ 

#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);
	//The above section must be included in the program, left untouched.

	
	//This section finding the mean of the entered numbers works.
	double total=0;
	for (int x = 0; x < counter; x++)
	{
		total += scores[x];
	}
	cout << "Average is " << (total/(counter)) << endl;
	

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

	// The minimum value is constantly being returned as 0.
	double minval=0;
	for (int x=counter; x>=0; x--)
		{
			minval = scores[x];
		}
	cout << "Lowest is " << minval <<endl;
}
I personally prefer this one.

Your functions for minimum and maximum return, in fact, the first and last value instead of the minimum and maximum values.


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
/* After the user enters scores (entering -1 will stop the loop),
calculate and display the highest and lowest test scores and the 
average of all the test scores.
*/ 

#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);
	//The above section must be included in the program, left untouched.

	
	//This section finding the mean of the entered numbers works.
	double total=0;
	for (int x = 0; x < counter; x++)
	{
		total += scores[x];
	}
	cout << "Average is " << (total/(counter)) << endl;
	

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

	// The minimum value is constantly being returned as 0.
	double minval=scores[0];
	for (int x=counter-1; x>=0; x--)
		{
			if (scores[x] <= minval) {
                minval = scores[x];
            }
		}
	cout << "Lowest is " << minval <<endl;
}



Thanks so much! The main program works now. There's just one thing that isn't working. When the only thing entered is a negative number (for example, a -1 to stop the loop), the loop still attempts to figure out the mean, lowest and highest number. When a -1 is entered, I'd like the program to immediately print "Press any key...." and exit.
Any tips for that?
Would that change the structure of your program. The simplest solution I could think, without much changing its program was:

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
/* After the user enters scores (entering -1 will stop the loop),
calculate and display the highest and lowest test scores and the 
average of all the test scores.
*/ 

#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);
	//The above section must be included in the program, left untouched.

	if (counter > 0) {
    	//This section finding the mean of the entered numbers works.
    	double total=0;
    	for (int x = 0; x < counter; x++)
    	{
    		total += scores[x];
    	}
    	cout << "Average is " << (total/(counter)) << endl;
    	
    
    	double maxval=scores[0];
    	for (int x = 0; x < counter; x++)
    		{
    			if (scores[x] >= maxval) {
                    maxval = scores[x];
                }
    		}
    	cout << "Highest is " << maxval << endl;
    
    	// The minimum value is constantly being returned as 0.
    	double minval=scores[0];
    	for (int x=counter-1; x>=0; x--)
    		{
    			if (scores[x] <= minval) {
                    minval = scores[x];
                }
    		}
    	cout << "Lowest is " << minval <<endl;
    }
}

Simple add if (counter > 0) { on line 19 and close in the end.
Thanks for all your help! It's working just right now.
Sarah
Topic archived. No new replies allowed.