Stats on number series

I am working on this so that a sample number can be entered. Then the user can input a series of number up to the sample. It would then display the max, min, sum and mean values of those numbers. I am not allowed to use arrays. Everything works except the min value. I have tried many different things and was looking for some help with this. Here is my 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70


#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;
 
int main()
{
 
int sample = 0;
int counter = 0;  
double number = 1;
double max = 0;
double min = 0;
double sum = 0;
double mean = 0;

for (counter = 1; counter <= sample; counter++)

cout << "This program will display statistical data on a series of numbers \n";
  
cout << "\nPlease enter a sample size between 1 - 20 \n";
cin >> sample;  


	if (sample > 20 || sample < 1)
	
		cout << "Invalid sample size. \n";
		//break;
	else
		while (counter <= sample)
		{
			cout << "Please enter number " << counter++ << ": \n";
			cin >> number;
			


			if (max <= number)
{
				max = number;
}
			else if (min >= number)
{
				min = number;
}
				
			sum = sum + number;

			number + number++;
		}
		
cout << "The highest (MAX) number in the series is \n" << max << endl;
cout << "The lowest (MIN) number in the series is \n" << min << endl;
cout << "The sum (SUM) of the numbers is \n" << sum << endl;
cout << "The average (MEAN) of the numbers is \n" << sum/sample << endl;
  
 		
		
		
 system("pause"); //holds the command window open
    return 0; //ends the program

}




Last edited on
closed account (iAk3T05o)
How does the program even work.
In the loop, counter = 1 and should only loop while counter <= sample but sample = 0, which means run while 1 <= 0 (impossible).
You use if and else statements but don't enclose it in {} and then put another loop under.
How does it not crash?
I for got to type the brackets in but they are in the original code .It doesn't crash it returns the min as a zero. That what I am trying to figure out. How to get a value for min.
Last edited on
Where are the brackets in the original code? The way this is it does no iterations therefore returns the value min was initialized to.
I went back and edited it and put them in already. I do not understand how to get the min.
The line
for (counter = 1; counter <= sample; counter++)
in the beginning will never run as the variable sample has not been read yet and has value 0.
If you then move on to the while loop, counter is never incremented and that should lead to an infinite loop. Lastly, the average should be
sum/double(sample) as sample is an int.
Last edited on
So I need to move this line after the sample size has been inputted?
Well it is unnecessary. all you should need is a line with:
counter++;
Inside the while loop. Also I'm not sure what:
number + number++;
is supposed to be doing.
*Edit: I did not see you had a counter++;
The problem is that you intialize min at 0 thus it is always less than anything else you input. either start it at something very large or set the first number to min.
Last edited on
i tried getting rid of the -for statement and now the
cout << "Please enter number" starts at 0 and goes to 4 for a sample size of three. When I add the counter++ in the -while statement it goes from 0 to 2 skipping 1 then quits with a sample of 3.
Yes, I tried to edit the comment before but i might have been too late. I did not see you already had a counter++ in place so the extra one is not needed. Initializing counter to 1 instead of 0 should fix the other issue.
Using a sample of 3 then entering 2, 4, 6
changing,
int counter = 1;

got rid of the for statement
for (counter = 1; counter <= sample; counter++)

I run and still get a zero for min. It is still counting from 1 to 3, good, it gives the max, good, the sum and mean are correct. all problem are with the min.
Am I calculating my min correctly?
Agian, since you initialize min to 0, and then proceed to input only positive numbers, the statement min >= number is never true. The solution would be to either start min to be very large, or initialize it to the first number you input.
if (counter==1) min=number;
ok, I see what you mean about giving min a large number.
are you saying replace the
if (min >= number){
with
if (counter==1) min=number;
or adding anoth if statement?
adding another if statement when the while begins.
Ok, I put the
while (counter <= sample)
{
if (counter==1){
min=number;
}
within the while brackets, the min goes to zero again.
Did I put in the right place?
no. it needs to read the number in first.
Ok, like this.
while (counter <= sample)
{
cout << "Please enter number " << counter++ << ": \n";
cin >> number;

if (counter==1){
min = number;
}
Yes that should do it.
Ok, Thanks for the help.
Topic archived. No new replies allowed.