Jul 27, 2012 at 12:40pm UTC
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.
Jul 27, 2012 at 12:46pm UTC
Sort the numbers..
Third number is your answer...
Jul 27, 2012 at 1:09pm UTC
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 Jul 27, 2012 at 1:10pm UTC
Jul 28, 2012 at 12:38am UTC
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 Jul 28, 2012 at 12:56am UTC
Jul 28, 2012 at 12:41am UTC
I ahve not understood you. The most highest is 6. Then 5 follows. And at last the third highest is 4.