Two Highest Numbers

In this program I'm supposed to find the two highest numbers of a series of numbers entered by the user. I have the program to collect the series from the user and get the highest number. What I don't have is the second highest number. The section we're working on are pass-by-reference and loops. How would you use these things for this program? Right now I have it output just the highest number with no defining or anything just for test purposes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
using namespace std;

int main()
{
	int amount, inumber, high1, high2;
	cout << "How many numbers? ";
	cin >> amount;

	cin >> inumber;
	high1 = inumber;

//Finds the highest number.
	for(int i = 0; i < (amount-1); i++)
	{
		int nnumber;
		cin >> nnumber;
		
		if(high1 > nnumber)
			high1 = high1;
		if(high1 < nnumber)
			high1 = nnumber;	
	}
	


	cout << "         " << high1 << endl;
}
1
2
3
4
5
6
7
8
9
10
11
void highest(int &h1, int &h2) {
    int val;
    h1 = h2 = (1 << 31); // -2147483648
    while (std::cin >> val) {
        if (val > h1) {
            h2 = h1;
            h1 = val;
        }
        else if (val > h2) h2 = val;            
    }
}
This doesn't make any sense to me. Based on what I've have made with my code so far, what do I need to add to get the second highest number? Is there a way to take out the highest number from the numbers entered and then run another loop that finds the highest number of the remaining numbers?
This doesn't make any sense to me.


What doesn't make sense? Step through smac89's code. As each val is entered, it is compared against the previous highest number (h1). If greater, the previous high number (h1) is moved to h2 and the new number stored in h1. If not greater, then it's compared to h2. If greater than h2, the new number replaces h2. h1 and h2 are returned to the caller by reference.

BTW, line 20 in your original code doesn't do anything. It assigns high1 to itself.

An alternative approach would be to store the numbers in an array, then after all the numbers are entered, sort the array. array[n-1] and array[n-2] will contain the highest and second highest numbers respectively, where n is the number of entries in the array.
@Smac89: Are you sure that signed left shift is using defined behavior? Even if it is, that's obfuscated even with the comment and is non-portable as it depends on the number of bits in an integer. You should use std::numeric_limits<int>::min() instead.
Ok. You guys are way beyond me on this because I've only been coding for a few weeks and my class is flipped so really I have to teach myself most of it. I don't understand what this means: while (std::cin >> val) or what this means: h1 = h2 = (1 << 31); // -2147483648 . What do they mean?
RobGillespie wrote:
I don't understand what this means: while (std::cin >> val)
It continually takes input into val and run the loop body until the input operation fails. This works because the input operation returns the stream that was input from (so you can chain input) and also because streams can be converted implicitly to bool to see if they are in a good state.
RobGillespie wrote:
or what this means: h1 = h2 = (1 << 31); // -2147483648
I complained to Smac89 about it - it means entirely different things depending on your compiler, and shouldn't be used. But, in a nutshell, it takes the binary bit pattern of the number 1, shifts that to the left 31 times, and then assigns that value to both h1 and h2. The only problem is that different compilers give the int type different sizes, so the code is not portable.
Last edited on
Ok so would using this for loop:
1
2
3
4
for(int i = 1; i <= (amount-1); i++)
	{
		int nnumber;
		cin >> nnumber;

be the same as using while (std::cin >> val)?
No, it would not. Your for loop executes a fixed number of times, regardless of whether there is less or more input than expected. The while loop will run as long as there is input and it can be input into val successfully. It only ends with end of input or invalid input.
Ok, like I said, I know hardly anything about what he did with that function. This is what I have for my entire code...with a bunch of things missing:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
using namespace std;

int highest(int &h1, int &h2)
{ 
		
}

int main()
{
	int amount, number1, number2;
	cout << "How many numbers? ";
	cin >> amount;

	cin << number1; 

	for(int i = 1, i < (amount - 1); i++)
	{
		int number2;
		cin >> number2;

		highest(number1, number2);
	}

	cout << endl;
	cout << "Highest: " << ??? << endl;
	cout << "Second: " << ???;
}


Now, the program is supposed to ask for an amount of numbers the user wants to enter. Then the user enters random numbers up to the amount that was entered. Then the program is supposed to find the highest number and the second highest number. Right now this is a lesson over "Pass-by-reference" and for loops.
So using those two methods, how would I need to go about changing my code? I don't know what to put in the function "highest" and I don't know where to call it. Like I said before, the course is flipped so the "professor" is supposed to post a lesson but it is one section and doesn't say much about anything. Then he has another section that is a broken link so that's great...I'm very new to programming so using the things that smac89 used is way beyond me.
You don't want to use smac89 highest function as is inside your for loop, since his highest function also includes a loop.

We could modify his function as follows:
1
2
3
4
5
6
7
8
9
void highest(int val, int &h1, int &h2) 
{   if (val > h1) 
    {   h2 = h1;
         h1 = val;
    }
    else 
        if (val > h2) 
          h2 = val;            
} 

This eliminates the while input loop and compares the current input (val) to the h1 and h2 as I explained before.

You would need to change your main as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{    int amount, number1, number2, val;
  
    cout << "How many numbers? ";
    cin >> amount;
    //  Set number1 and number2 to smallest possible number
    number1 = std::numeric_limits<int>::min();
    number2 = std::numeric_limits<int>::min() ;

    for (int i = 0, i< amount; i++)
    {    cin >> val;
          highest (val, number1, number2);
    }

    cout << endl;
    cout << "Highest: " << number1 << endl;
    cout << "Second: " << number2 << endl;
} 






1
2
3
4
for(int i = 1, i < (amount - 1); i++)
{
	highest(number1, number2);
}


1
2
3
4
5
6
7
8
9
10
void highest(int &h1, int &h2) {
    int val;
    if (std::cin >> val) {
        if (val > h1) {
            h2 = h1;
            h1 = val;
        }
        else if (val > h2) h2 = val;            
    }
}
Topic archived. No new replies allowed.