Problem Solving: Finding Maximum Number

Learn to write a program using functions to find the maximum number.

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:

Press + to interact
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;
}
  • The kNumbersMaximum() function takes k numbers in the num variable as input and compares each num with max number in each iteration of the for loop.
  • If num is greater than max, assign num to max. After completing the for loop, we will return the max number.

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;
}






Maximum number using function
  • In line 22, we take k numbers from which we want to find out the maximum number.
  • In line 23, we call kNumbersMaximum() and pass it k as an argument.
  • In line 9, we have a for loop starting from 1 till k.
  • In lines 12–14, we take a num as input and compare it with the max number. If input num is greater than max, we assign that num to max.
  • In line 16, our function will return the max number.
  • In line 24, the kNumbersMaximum() returns the maximum number from the result.

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

Q

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
A)

-5

B)

-20

C)

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;
}







Maximum number using Functions
  • In line 7, instead of assigning num= 0, we are taking the first number from the user and assigning it to max.
  • In Line 10, our for loop will now start from 2 instead of 1 for 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 if statement in line 14 will compare all numbers with each other instead of 0 and return the correct maximum number.

Our code is correct now to find the maximum of all positive and negative numbers.