...
/Problem Solving: Finding the Maximum and Minimum Number
Problem Solving: Finding the Maximum and Minimum Number
Learn to write a program that finds the maximum and minimum number out of the given numbers.
We'll cover the following...
In this lesson, we will write a program that takes some positive numbers as input and displays the maximum and minimum number. We will, once again, make several attempts to refine our algorithm with each attempt.
So let’s get to it!
Maximum number
Take five numbers and find the maximum number.
Sample input
45 20 12 60 33
Sample output
Maximum = 60
Now, we need to find the maximum number out of five numbers. This means we will have to compare all the numbers with each other to see which number is the maximum.
So how do we do that? There are multiple ways to solve this. Let’s start with the most obvious one.
Implementation 1: The if statement
So, let’s say we have five numbers stored in five variables.
We could start simply by taking the first number and comparing it with all the other numbers using the greater than operator (>) and the AND operator (&&). If it is greater than all the other numbers, print that number as maximum. We’ll repeat these steps for all the numbers as shown below.
...if ( n1 > n2 && n1 > n3 && n1 > n4 && n1 > n5)cout << n1 << " is the maximum number. " << endl;// now repeating the above steps for second numberif ( n2 > n1 && n2 > n3 && n2 > n4 && n2 > n5)cout << n2 << " is the maximum number. " << endl;// and so on.......
Now, let’s write the complete code below and run it to see the output.
#include <iostream>
using namespace std;
int main()
{
int n1, n2, n3, n4, n5;
cout << "The 5 positive integer numbers are: ";
// taking the 5 numbers from the user
cin >> n1 >> n2 >> n3 >> n4 >> n5;
if ( n1 > n2 && n1 > n3 && n1 > n4 && n1 > n5)
cout << n1 << " is the maximum number. " << endl;
// now repeating the above steps for n2
if ( n2 > n1 && n2 > n3 && n2 > n4 && n2 > n5)
cout << n2 << " is the maximum number. " << endl;
// now repeating the above steps for n3
if ( n3 > n1 && n3 > n2 && n3 > n4 && n3 > n5)
cout << n3 << " is the maximum number. " << endl;
// now repeating the above steps for n4
if ( n4 > n1 && n4 > n2 && n4 > n3 && n4 > n5)
cout << n4 << " is the maximum number. " << endl;
// now repeating the above steps for n5
if ( n5 > n1 && n5 > n2 && n5 > n3 && n5 > n4)
cout << n5 << " is the maximum number. " << endl;
return 0;
}
Now, as you may have noticed, this is not a convenient way to solve this problem. What if we had more than five numbers? If we had to find the maximum of fifty or a hundred numbers, it would be too many conditions!
One of the most evident flaws of the above code is that, regardless of being able to find the maximum number, the program will not end until it has executed all the if conditions. That is, if n1 was the maximum number, the other if statements would still be checked.
In the code above (the one with the if statements), how many conditions are being checked?
4
20
5
Implementation 2: The if-else statement
To solve the above issue we could use the if-else statements instead! This would reduce the number of conditions and optimize our code.
So, the code would be as follows:
......if ( n1 > n2 && n1 > n3 && n1 > n4 && n1 > n5)cout << n1 << " is the maximum number. " << endl;// no need to add the n1 conditionelse if (n2 > n3 && n2 > n4 && n2 > n5)cout << n2 << " is the maximum number. " << endl;// no need to add the n1 and n2 conditionelse if (n3 > n4 && n3 > n5)cout << n3 << " is the maximum number. " << endl;// and so on.......
Now, let’s write the complete code below and run it to see the output.
#include <iostream>
using namespace std;
int main()
{
int n1, n2, n3, n4, n5;
cout << "Enter 5 integer numbers: ";
// taking the 5 numbers from the user
cin >> n1 >> n2 >> n3 >> n4 >> n5;
if ( n1 > n2 && n1 > n3 && n1 > n4 && n1 > n5)
cout << n1 << " is the maximum number. " << endl;
// now repeating the above for n2
else if (n2 > n3 && n2 > n4 && n2 > n5)
cout << n2 << " is the maximum number. " << endl;
// now repeating the above steps for n3
else if (n3 > n4 && n3 > n5)
cout << n3 << " is the maximum number. " << endl;
// now repeating the above steps for n4
else if (n4 > n5)
cout << n4 << " is the maximum number. " << endl;
// now repeating the above steps for n5
else
cout << n5 << " is the maximum number. " << endl;
return 0;
}
One thing to note in the code above is that, besides using the if-else statements, the number of comparison conditions has decreased considerably as compared to the previous code.
In line 12, we compare n1 with all the numbers. If n1 is maximum, the program ends and no other condition is checked. If not, we move to line 16.
In line 16, we do not need to check whether n2 is greater than n1 since it has already been checked in the first statement. If n2 is the maximum, the program ends. If not, we move to line 20.
In line 20, we do not need to check whether n3 is greater than n1 and n2 since they have already been checked in the previous two statements.
In line 24, we do not need to check whether n4 is greater than n1, n2, and n3.
In line 28, we do not need to check whether n4 is greater than n1, n2, and n3. So, if it gets this far, n5 is the maximum number and we print it.
Number of conditions
In the code above (the one with the if-else statements), how many conditions are being checked in the worst case?
4
10
20
Implementation 3: Maintaining the maximum number
So far, we’ve seen two methods to solve the problem. However, there is an even more efficient solution to solving this problem.
Here, we introduce a new variable called max. We store the first number in max and compare it with each number. If the number is greater than max, we update max with that number. After all the if conditions, we print the value of max.
Have a look at the code below and run it step by step to see how the value of
maxis updated.
#include <iostream>
using namespace std;
// Printing the maximum number
int main()
{
int n1, n2, n3, n4, n5;
cout << "The 5 integer numbers are: ";
// taking the 5 numbers from the user
cin >> n1 >> n2 >> n3 >> n4 >> n5;
// max variable initialized with the first number
int max = n1;
// max has the first value as maximum
if (n2 > max)
max = n2;
// max has the the maximum of the first 2 numbers
if (n3 > max)
max = n3;
// max has the the maximum of the first 3 numbers
if (n4 > max)
max = n4;
// max has the the maximum of the first 4 numbers
if (n5 > max)
max = n5;
// max has the the maximum of the all 5 numbers
cout << "The maximum number is " << max << endl;
return 0;
}
Now, in the code above, we simply assume the first number is max. We then compare it with the second number in line 14.
If the second number is greater than the value stored in max, we update max with the second number (line 15).
Then we compare max with the third number and if the third number is greater than max, we update max. We do this till max has been compared with all numbers. Whichever number is greater, gets stored inside max.
We can see how the if conditions have considerably been reduced now.
In the code above, only four conditions are checked now!
We saw the conditions being reduced from 20 (using the if statements) to 10 (using if-else statements) to only 4 (using the max variable)!
If we had to find the max between 10 numbers in the code above (the one that uses the max variable), how many conditions would then be required?
9
10
Can we modify the above solution to make it even more efficient?
Yes, we can, by using a loop!
Implementation 4: An efficient solution using the for loop
Instead of going directly to the loop, let’s ask ourselves if we can redesign our algorithm such that it should be able to perform the above tasks with a minimum number of input variables.
Discovering the loop
The idea is that, instead of taking all the inputs at once, we will take inputs one by one in a single variable. We will also keep maintaining the max and take an input again in the same variable, as shown in the following code:
int num = 0;int max = num;// 1.cin >> num; // enter the 1st numberif (num > max)max = num;// 2.cin >> num; // enter the 2nd numberif (num > max)max = num;// 3.cin >> num; // enter the 3rd numberif (num > max)max = num;// 4.cin >> num; // enter the 4th numberif (num > max)max = num;// 5.cin >> num; // enter the 5th numberif (num > max)max = num;cout << "The maximum number is " << max << endl;// should print 1000return 0;}
The good thing about the code above is that we can repeat the three steps of taking input and updating the maximum in a for loop five times to make it a complete solution. So, here we have the updated, efficient solution.
Assume that the numbers entered by the user are all positive.
#include <iostream>
using namespace std;
// Printing the maximum number
int main()
{
int num = 0;
// max variable initialized with the first number
int max = num;
for(int i = 1; i<=5; i++)
{
cout << "n"<<i<<": ";
// taking the 5 numbers from the user
cin >> num; // e.g. 10 5 20 1000 200
if (num > max)
max = num;
}
cout << "The maximum number is " << max << endl;
// should print 1000
return 0;
}
Instruction: Execute the above program. The cin>>num in the loop will take input five times (since the loop is executing five times).
Exercise 1: Finding the minimum number using a loop
Write a program to find the minimum number out of five integer numbers in the playground given below.
Instruction: Ideally, you should solve using all the above implementations by yourself and execute them step-by-step to see their working. At the very least, you should solve using the loop strategy shown in Implementation 4.
#include <iostream>
using namespace std;
// Printing the minimum number
int main()
{
int num = 0;
// max variable initialized with the first number
int min = num; // this will not work? Why???
// write your code here
cout << "The minimum number is " << min << endl;
// should print 1000
return 0;
}
Exercise 2: Finding the minimum number (using a loop) with k numbers
Extend your program to find the minimum number out of k integer numbers in the playgrounds above (for finding the minimum, refer to Exercise 1; for the maximum refer to Implementation 4), where k is the input your program should take.
Instruction: You only need to change the code such that it should take input k before the loop and run the loop k times instead of 5. Modify the above code of Exercise 1 or Implementation 4.
Now let’s modify our problem a bit to print both the maximum and minimum numbers simultaneously.
Finding the maximum and minimum
Take five numbers and find both the maximum and minimum number in the same program.
Sample input
45 20 12 60 33
Sample output
Maximum = 60
Minimum = 12
Let’s see the code below:
#include <iostream>
using namespace std;
int main()
{
int n1, n2, n3, n4, n5;
cout << "The 5 integer numbers are: ";
// taking the 5 numbers from the user
cin >> n1 >> n2 >> n3 >> n4 >> n5;
// max and min variable initialised with the first number
int max = n1, min = n1;
if (n2 > max)
max = n2;
else if (n2 < min)
min = n2;
if (n3 > max)
max = n3;
else if (n3 < min)
min = n3;
if (n4 > max)
max = n4;
else if (n4 < min)
min = n4;
if (n5 > max)
max = n5;
else if (n5 < min)
min = n5;
cout << "The maximum number is " << max << endl;
cout << "The minimum number is " << min << endl;
return 0;
}
We now have two variables called min and max and use the same logic as when finding the maximum number. We used if-else in our code to first check if each number was greater than max and then updated it. In else, we compared the same number with min only if that number was not greater than max.
Again, note how we did not use any curly braces
{}after theifandelseconditions and the code still executed fine.
Exercise 3: Finding the minimum and maximum number simultaneously
First refine the above program to work in the loop (for five numbers), and then extend your program to find the minimum and maximum number out of k integer numbers.
#include <iostream>
using namespace std;
int main()
{
int k, max, min, num;
// Write your code here.
cout << "The maximum number is " << max << endl;
cout << "The minimum number is " << min << endl;
return 0;
}
We hope you enjoyed this problem solving walk-through. We could have come up with the optimized solution at the start, but we wanted you to understand why and how we discovered the natural process of the final solution with loops, which is not a trivial journey and requires a leap of mental progress. We will keep discovering this in the coming lessons.