#include <iostream>
#include <cstdio>
using std::cout;
using std::cin;
int main()
{
int num;
intconst 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.
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
intconst 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.
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 . . .
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.