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.
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?
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?
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.
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.
#include <iostream>
usingnamespace 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.