Finding Maximum and minimum values in a set of integers?

I have an assignment where I have to write a program that accepts a series of integers from the keyboard from 1 to 100 using a single sentinel controlled loop, meaning I will need to also assign a sentinel number that when entered, will stop the loop and display the results.

I have no problem creating sentinel loops but this assignment stumps me. Once the user has entered their series of numbers and used sentinel, the output needs to display the average of the numbers, as well as the maximum and minimum numbers.

We have not covered arrays yet so I'm not allowed to use those. I really don't have much problem created a sentinel controlled loop and finding an average, it's just I have no idea how to find the maximum and minimum in the set of numbers. It is not covered in our book and my notes from my teacher only mention using something called climits int_min and int_max?

Any help on how to obtain the maximum and minimum integers in a set would be greatly appreciated
You should add a check at the end ,for the case if user just enters -1,without giving any numbers.In this case, we have no average(or min or max or count)


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
#include <iostream>
using namespace std;

int main()
{
int maximum=0,minimum=1,number,sum=0;
int count=0;
float average;


while(1)
{
cout<<"Enter a number(-1 to quit): "; cin>>number;    

if(number==-1)
    break;  // enough asking for numbers
    
else if(number<1 || number>100)
    {cout<<"\nOut of range.Enter between 1-100.\n";continue;} //invalid input,do again


maximum=(number>maximum)?number:maximum; //conditional statement,
                                        //if entered number is greater than our max,its new max

minimum=(number<minimum)?number:minimum;//condiitonal statement,
                                        //if entered number is smaller than our min,its new min
sum+=number; //add to sum    
count++;     // increment count
}

average=float(sum)/float(count);

cout<<"\nYou entered " << count << " numbers."<<endl
    <<"Their average is : "<<average<<endl
    <<"Max number is : "<<maximum<<endl
    <<"Min number is : "<<minimum;  
return 0;
}

Last edited on
Wow, you didn't have to do all that, I really appreciate it though. Definitely looks more advanced than what we're doing so I'll study it and see if I can make my own version out of it. When I run the program everything works except the minimum which always outputs 1.
Oops.I didnt notice it,at the beginning initialize minimum to 101.
Since it was initialized to 1,none of the entered numbers can be smaller than it :)

Alternatively, you can ask for one number before entering the loop,and write
1
2
minimum=number;
maximum=number;

Since this is our first number,its min and max at the same time.
Last edited on
using something called climits int_min and int_max?

"climits" is a header file from the Standard C Library, you use it lie you use "iostream" i.e
#include <climits> //or #include <limits.h> (which is deprecated/not used anymore in C++

it contains macros INT_MAX and INT_MIN, which are the largest and smallest values the type int can store for your computer, try to run this:
1
2
3
4
5
6
7
8
9
int main()
{
    int x = INT_MAX;
    int y = INT_MIN;
    std::cout << x << " " << y << std::endl;
    //or you can forget the three preceding lines and just type ;)
    //std::cout << INT_MAX << " " << INT_MIN << std::endl;
    return 0;
} 


so, what your instructor hints at:
- define one smallest integer you can think of, which we'll call 'largest number'
- get input from user
- compare that to the smallest integer (INT_MIN), teh larger number will be your 'largest number'
- get the next input, compare it to the previous result until the user got bored and end his session
- print out the result of your comparisons

remember: try all these separately, no need do it all at the same time ;)
Last edited on
I took a break from this for a while. I tried coding it without any break or continue statements but it still isn't coming out right.

Here's what I have

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
int main()
{
int maxnum = 0,minnum = 101,number,sum = 0;
int count = 0;
float average;
int SENTINEL = -1;



while(number != SENTINEL)
{
cout <<"Enter a number(-1 to quit): "; 
cin >> number;    

if (number > maxnum && number < 101)
   maxnum = number; 
                                       

else if (number <= maxnum && number !=SENTINEL)
   minnum = number;
                         
                                        
sum = sum+number; 
count++;     
}

count--;


average=(float)sum/(float)count;


cout<<"\nYou entered " << count << " numbers."<<endl
    <<"Their average is : " <<setw(3) << fixed << setprecision(2) << showpoint<<average<<endl
    <<"Max number is : "<<maxnum<<endl
    <<"Min number is : "<<minnum << endl;
       
        return 0;
        }        


It seems that if you enter a number such as 80 and THEN a bigger number like 90, it just takes the bigger number as the max and ignores the 80, outputting 101 and the minimum.

However, enter 90 and THEN 80, the program will almost work correctly, the average seems to be off by .5

so close to having this done, my mind is fried from stress so any help completing this would be appreciated.
Last edited on
Try this:
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
#include <iostream>

using namespace std;

int main() {
	const int SENTINEL = -1;
	int num; //each entree stores here
	int min = SENTINEL, max = SENTINEL; //for minimum, and maximum entries
	int sum = 0, count = 0; //for figuring out the average of all the entries

	// do-while loops must run at least once
	do {
		cout << "Enter a number (-1 to quit): ";
		cin >> num;
		if( 1 <= num && num <= 100 ) {
		 //num is in range
			if( min == SENTINEL || num < min )
			 //min has not been initialized yet, or the entree is a lower value
				min = num;
			if( max == SENTINEL || num > max )
			 //max has not been initialized yet, or the entree is a higher value
				max = num;
			//keep track of sum and count
			sum += num;
			count++;
		} else if( num != SENTINEL )
		 //num is out of range but not the sentinel
			cout << num << " is out of range" << endl;
	} while( num != SENTINEL );
	
	//display min, max, and average
	cout << "Minimum: " << min << endl
		<< "Maximum: " << max << endl
		<< "Average: " << (double)sum / count << endl;
	
	return 0;
}


I tried to make the comments self explanatory, but please ask a question if you don't understand something. (Please use this code as a guideline only. I'm not trying to do your homework for you :)
Last edited on
I appreciate it. I don't quite understand lines 17-22. Especially line 17 where it says if num < min then assign that number to min. Was min assigned the value of -1 when it was given a SENTINEL value in the declarations?
@Mathhead: Try not to give solutions; just give hints that will help the OP get to the solution themselves.

@Lkafilta: Yes, however in the if statement he checks to if that is true OR if it is equal to SENTINEL (i.e. it hasn't been assigned yet.
Yes, min and max start at -1 but that gets overriden to what ever the first value entered is.
1
2
if( num == SENTINEL )
//meens if this is the first run 


However think about how you would find a minimum...
If I said "tell me the lowest number, the numbers are: 5, 2, 10".
What do you do?
Well I say "ok 5" that's the lowest so far... min = num; // num is 5
Then move to the next number, Wait! 2 is less then 5... if( num < min ) //it is (true)
so now the lowest so far is 2... min = num //num is 2
Then keep moving. 10 is not less then 2, so do nothing to the current min and forget 10.
Finally try to keep moving, but we are out of numbers so 2 is the lowest.

This is know as the "high water mark algorithum" http://en.wikipedia.org/wiki/Ordinary_high_water_mark (or I guess low water mark in this case.)

(The maximum works the same way, but check to see if num is greater then the current max)
Last edited on
you are a genius, thanks for helping me understand
Well I don't know about that Lkafilta, but your very welcome. Let me know if you have any more questions.

firedraco
@Mathhead: Try not to give solutions; just give hints that will help the OP get to the solution themselves.

I understand firedraco, but hey... I put my disclaimer :)
I understand firedraco, but hey... I put my disclaimer :)


Yes, however I'd rather not have people being able to get an answer to their homework without trying...and besides, if they are able to solve it themselves it helps them feel better about it than just looking at the solution and thinking "Ok, so that's how it works".
I don't give answers to people who don't try. Look at Lkafilta's post right before mine. They almost had it... I just wanted to show the same thing in a cleaner (and clearer) way...
I agree with you!
Last edited on
Topic archived. No new replies allowed.