trouble with getting max and min and difference

stumped once again, something is wrong here. the math problems always seem to get me.



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
#include <iostream>
#include <cstdio>
using std::cout;
using std::cin;

int main()
{     
	int num;
    int const SENTINAL=0;
    int min=SENTINAL;
    int max=SENTINAL;
    int difference=max-min;
    int sum = 0, count = 0; // to figure out the average of the num.
   
    
    /* ask user for series of integers*/
    
    
    do { 
         
    cout <<"Enter a series of integers (Press 0 to quit.) :" ;
    cin >> num; 
	cout <<"\n";
    
    if( min == SENTINAL || num < min )
			 
				min = num;
				
    if( max == SENTINAL || num > max )
			 
				max = num;
				
				sum += num;
			    count++;
}
while(num != SENTINAL);
    
    
    
    /*when user presses zero display the information*/
     cout<<"You've entered " << count << " integers\n";                               
     cout<<"The greatest integer is, " <<max<<"\n";
     cout<<"The least integer is," <<min<<"\n";
     cout<<"The average of the integers is, "<<(float)sum/count<<"\n";
     cout<<"The difference of the least and greatest integer is, "<<difference<<"\n";

cin.get();   
return 0;
}


I have tried to set the min to 200,000 but then my difference was 200,000. I need something that will make it not count 0 as lowest number of the integers, if anyone could suggest anything I would be very grateful, and also feel free to judge me on how I write code. I need to learn to write my code neater and clearer. Also any tips would be awesome too.
Last edited on
The way you write code looks perfectly fine to me. The only thing I would change is that lines 31 and 27 are indented way too far over. Not that it really matters.

Also, I ran the program, and it counts zero as one of the integers I've input. You might want to change that.

As for your problem, it's right here
1
2
3
4
    int const SENTINAL=0;
    int min=SENTINAL;
    int max=SENTINAL;
    int difference=max-min;


When you declare max here, it sets it to 0 - 0 (min - max)

Move the declaration of difference below the line
cout<<"The average of the integers is, "<<(float)sum/count<<"\n"; and it will fix your problem.

EDIT: Now that I read through your whole post instead of just skimming...

change
if( min == SENTINAL || num < min ) to if( num < min && num != SENTINAL )

Do the same thing with the other if. That should fix it.
Last edited on
hmm for some reason it didn't fix it. i changed everything that you've said to and it fixed the max but then after i kept messing with the program it just didn't work anymore.
Enter a series of integers (Press 0 to quit.) :25

Enter a series of integers (Press 0 to quit.) :65

Enter a series of integers (Press 0 to quit.) :89

Enter a series of integers (Press 0 to quit.) :5

Enter a series of integers (Press 0 to quit.) :2

Enter a series of integers (Press 0 to quit.) :0

You've entered 6 integers
The least integer is,2
The greatest integer is, 0
The average of the integers is, 31
The difference of the least and greatest integer is, -2
Press any key to continue . . .


that is the output i get.
okay i have fixed it now. but i have one more problem i forgot to mention that i need to make count not count 0 as an integer. This is not an assignment by the way. I'm trying to keep up my C++ skills. This is from my C++ book.
I figured it out now I put another if statement.

if(num==0)
count--;

had a brain fart. thanks paulthepenguin. you've been very good at helping me. I shall return the favor sometime.
You're very welcome.
Topic archived. No new replies allowed.