Finding the 3rd highest value

Can somebody teach me a code on how to find the 3rd highest value.

for example.
At start, the program ask how many loops(or numbers the user wants to add)
User inputs 10 different numbers (0,positive,negative)
Program will try to find out what is the third highest of all.
Sort the numbers..
Third number is your answer...
The function to do that in C++ is called nth_element()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>

int main()
{
    std::cout << "Enter 10 different numbers (0, positive, negative) ";
    std::vector<int> numbers(10);
    for(size_t n = 0; n < numbers.size(); ++n)
       std::cin >> numbers[n];

    nth_element(numbers.begin(), numbers.begin() + 2, numbers.end(),
                std::greater<int>());

    std::cout << "The 3rd largest number is " << numbers[2] << '\n';
}


(Note: it's important that your task specifies "10 different numbers"
The algorithm is simple. Let assume that you have an array of n elements int a[n] fiiled with integer numbers.
In the very beginning you set

1
2
int max1, max2, max3;
max1 = max2 = max3 = a[o];

Then in a loop until all elements of the array will be passed you do the following comparisions


1
2
3
4
5
6
7
8
9
10
11
12
if ( max1 < a[i] )
{
   max1 = a[i]; max2 = max1; max3 = max2;
}
else if ( max2 < a[i] )
{
   max2 = a[i]; max3 = max2;
}
else if ( max3 < a[i] )
{
   max3 = a[i];
}  
Last edited on
vlad's code won't work. You need to do the assignments in the opposite order. Otherwise you are setting all of the max values to a[i];

1
2
3
4
5
6
7
8
9
10
11
12
if ( max1 < a[i] )
{
   max3 = max2; max2 = max1; max1 = a[i];
}
else if ( max2 < a[i] )
{
   max3 = max2; max2 = a[i];
}
else if ( max3 < a[i] )
{
   max3 = a[i];
}  
@doug4

vlad's code won't work. You need to do the assignments in the opposite order


Thanks that you pointed out my typo.
Got it working! Thanks vlad!

My bad
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
for(i=0;i<loop;i++)
     {
        cout<<"Enter a value: ";
        cin>>array[i];

        if ( max1 < array[i] )
        {
        max3 = max2; max2 = max1; max1 = array[i];
        }
        else if ( max2 < array[i] )
        {
         max3 = max2; max2 = array[i];
        }
        else if ( max3 < array[i] )
        {
        max3 = array[i];
        }  
     }

First I did this, and it does not show the correct 3rd highest but then when I use
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
for(i=0;i<loop;i++)
     {
        cout<<"Enter a value: ";
        cin>>array[i];
     }
     for(i=0;i<loop;i++)
     {
        if ( max1 < array[i] )
        {
        max3 = max2; max2 = max1; max1 = array[i];
        }
        else if ( max2 < array[i] )
        {
         max3 = max2; max2 = array[i];
        }
        else if ( max3 < array[i] )
        {
        max3 = array[i];
        }  
     }

It's working fine now, thanks man.
Last edited on
I ahve not understood you. The most highest is 6. Then 5 follows. And at last the third highest is 4.
Topic archived. No new replies allowed.