Problem Solving: Finding Maximum Number
Learn to write a program using functions to find the maximum number.
We'll cover the following...
In this lesson, we will write a program that takes k numbers and finds the maximum number using functions.
Maximum number
Take k numbers as input and find the maximum number.
Sample input
k = 5
20 19 30 14 50
Sample output
50
We need to find the maximum number out of k numbers. This means we will have to compare all the numbers to see the maximum number.
We have solved this problem in this lesson with an efficient solution.
Let’s take that solution and implement it using the kNumbersMaximum() function:
int kNumbersMaximum(int k){int num = 0;// max variable initialized with the first numberint max = num;for(int i = 1; i<=k; i++){// taking the k numbers from the usercin >> num;if (num > max)max = num;}return max;}
- The
kNumbersMaximum()function takesknumbers in thenumvariable as input and compares eachnumwithmaxnumber in each iteration of theforloop. - If
numis greater thanmax, assignnumtomax. After completing theforloop, we will return themaxnumber.
Let’s look at the interactive visualization of the function kNumbersMaximum() below:
Let’s write the complete code below and run it:
#include <iostream>
using namespace std;
// Printing the maximum number
int kNumbersMaximum(int k)
{
int num = 0;
// max variable initialized with the first number
int max = num;
for(int i = 1; i<=k; i++)
{
// taking the k numbers from the user
cin >> num;
if (num > max)
max = num;
}
return max;
}
int main()
{
int k, result;
cout << "How many numbers would you like to enter: " << endl;
cin >> k; // How many numbers you want as input
result = kNumbersMaximum(k);
cout << "The maximum number is " << result << endl;
return 0;
}
- In line 22, we take
knumbers from which we want to find out the maximum number. - In line 23, we call
kNumbersMaximum()and pass itkas an argument. - In line 9, we have a
forloop starting from1tillk. - In lines 12–14, we take a
numas input and compare it with themaxnumber. If inputnumis greater thanmax, we assign thatnumtomax. - In line 16, our function will return the
maxnumber. - In line 24, the
kNumbersMaximum()returns the maximum number from theresult.
There is a small bug in the above program. Can you find that logical error? Hint: What will be the output of the program if all the k numbers are negative?
Fixing the logical error
Before fixing the error, look at the following quiz.
Maximum number
If all numbers entered by the user are negative numbers, what maximum number will the kNumbersMaximum() function return?
Instruction: Execute the above program and check with the following inputs.
Sample input
k = 3
-10 -20 -5
-5
-20
0
So how can we change our code to work with negative numbers also?
Let’s modify kNumbersMaximum() and run below code:
#include <iostream>
using namespace std;
// Printing the maximum number
int kNumbersMaximum(int k)
{
int num;
cin >> num;
// max variable initialized with the first number
int max = num;
for(int i = 2; i<=k; i++)
{
// taking the k-1 numbers from the user
cin >> num;
if (num > max)
{
max = num; // store previous maximum before assigning num to max
}
}
return max;
}
int main()
{
int k, result;
cout << "How many numbers would you like to enter: " << endl;
cin >> k; // How many numbers you want as input
result = kNumbersMaximum(k);
cout << "The maximum number is " << result << endl;
return 0;
}
- In line 7, instead of assigning
num= 0, we are taking the first number from the user and assigning it tomax. - In Line 10, our
forloop will now start from2instead of1for k-1 numbers as we have already stored one number. - Rest of the code will remain the same. Now if the user enters negative numbers, the
ifstatement in line 14 will compare all numbers with each other instead of0and return the correct maximum number.
Our code is correct now to find the maximum of all positive and negative numbers.