Problem Solving: Finding Second Highest Number

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

In this lesson, we will write a program that takes k numbers and finds the second maximum (highest) number using functions.

So let’s start!

Second highest number

Take k numbers as input and find the second highest number.

Sample input

k = 5
10 -20 30 -40 50

Sample output

30

We need to find the second highest number out of k numbers. How do we do that? To find the second highest number, we can modify the function kNumbersMaximum() that we wrote in the previous lesson.

We can modify the kNumbersMaximum() as kNumbersSecondMaximum() with few changes.

We will declare the secondMax variable in the function. Before assigning num to max, we will store the max number in secondMax. After completion of the for loop, we will return the second_max.

Let’s write the complete code below:

#include <iostream>
using namespace std;
// Printing the maximum number
int kNumbersSecondMaximum(int k)
{   
    int num;
    cout << "numbers: "; 
    cin >> num;
    
    // max variable initialized with the first number
    int max = num;
    int secondMax;

    for(int i = 2; i<=k; i++)
    {
        // taking the k-1 numbers from the user
        cin >> num;
        if (num > max)
        {
            secondMax = max; // store previous maximum before assigning num to max
            max = num;
        }
    } 
    return secondMax;
}
int main()
{
   int k, result;
   cout << "How many numbers would you like to enter (k): " << endl;
   cin >> k; // How many numbers you want as input
   result = kNumbersSecondMaximum(k);
   cout << "The second maximum number is " << result << endl; 

    return 0;
}









kNumbersSecondMaximum function

  • Line 19: We are assigning max number to secondMax before updating max variable with num in line 20. In each iteration secondMax will store the previous max number.
  • Line 23: We return the secondMax.

Instruction: Execute the above program with the sample input k = 3 and numbers 1 2 3.

Can you see an issue with the second run?

Point out the logical error

There is at least one logical mistake in the above second highest code. Can you guess what it is without running it? After thinking, click “Run” and see step-by-step execution in the debugger.

Instruction: Run the above code and step by step execute it and see what is the logical mistake for the input of k=3 and numbers 3, 2, 1.

Second maximum number

Q

On the below sample input, what second maximum will your kNumbersSecondMaximum() return?

Sample input

k = 3
20 -5 15
A)

-5

B)

20

C)

15

D)

Garbage

Fixing logical error 1

The idea to fix the above code is that instead of not initializing the secondMax, before entering the loop, we input two numbers and store the first in max and second in secondMax. If the value in secondMax is bigger, we swap the two values and ensure that before entering the loop, the max contains the maximum and the secondMax contains the second maximum. Now, the rest of the code should work.

Let’s run the below code to see how we can change it:

#include <iostream>
using namespace std;
// Printing the maximum number
int kNumbersSecondMaximum(int k)
{
    int num;
    cin >> num;
    // max variable initialized with the first number
    int max = num;
    
    int secondMax;
    // take second number for initialization of secondMax variable
    cin >> num;

    //secondMax should not be greater than max
    if (num > max)
    {
        secondMax = max;
        max = num;
    }
    else
    {
        secondMax = num;
    }
    
    for(int i = 3; i<=k; i++)
    {
        // taking the k-2 numbers from the user
        cin >> num;
        if (num > max)
        {
            secondMax = num; // store previous maximum before assigning num to max
            max = num;
        }
    } 
    return secondMax;
}
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 = kNumbersSecondMaximum(k);
   cout << "The 2nd maximum number is " << result << endl; 

    return 0;
}




kNumbersSecondMaximum() implementation: First logical error fixed
  • In lines 7–9, we take the first number num from the user and assign it to max.
  • In lines 11–13, the second number is taken from the user.
  • In lines 16–24, the second number is checked. If num> max, both are swapped. Otherwise, the second number is assigned to secondMax.
  • In line 26, our for loop will start from 3 instead of 2 for k-2 numbers because we have already taken two numbers from the user.

This will resolve the garbage value issue. Run the above code with 3, 2, 1 and see the output.

However, there’s still one issue left. Look at the below example:

Sample Input

3 1 2 

Sample Output

2

Our kNumbersSecondMaximum() will fail on above sample input and will return 1 as secMax. Why?

Fixing logical error 2

The issue is that we were assuming in the above code that whenever the max is updated, surely, the second maximum will also get replaced by the previous maximum. But what if the new number is not greater than the maximum but greater than the second maximum only? In that case, we will only update the second maximum number.

So look at the following fix (Look at the following fix (from line 35–38):):

#include <iostream>
using namespace std;
// Printing the maximum number
int kNumbersSecondMaximum(int k)
{
    int num;
    cout << "number: ";
    cin >> num;
    // max variable initialized with the first number
    int max = num;
    
    int secondMax;
    // take second number for initialization of secondMax variable
    cin >> num;

    //secondMax should not be greater than max
    if (num > max)
    {
        secondMax = max;
        max = num;
    }
    else
    {
        secondMax = num;
    }
    
    for(int i = 3; i<=k; i++)
    {
        // taking the k-2 numbers from the user
        cin >> num;
        if (num > max)
        {
            secondMax = max; // store previous maximum before assigning num to max
            max = num;
        }
        else if (num > secondMax)
        {
            secondMax = num;
        }
    } 
    return secondMax;
}
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 = kNumbersSecondMaximum(k);
   cout << "The second maximum number is " << result << endl; 

    return 0;
}














kNumbersSecondMaximum() implementation: Second logical error fixed
  • In lines 30–38, if num > secondMax, both max and secMax will update. And if num > secondMax, only secondMax will be updated.

Practice exercise

How can we modify the above program to report the third maximum from k numbers (where k>=3)?