# Thread: Finding the two largest numbers

1. ## Finding the two largest numbers

Hi, i'm trying to find the two largest numbers out of ten. I've re-done this problem several times and it still doesn't work. I'm not sure if my logic is wrong or if i'm missing something. Any help would be greatly appreciated.

Code:
```#include <stdio.h>

int main(void) {

float number;
float largest;
float secondLargest;
float counter;
float temp;

scanf("%f",&number);

number = largest;

scanf("%f",&number);

number = secondLargest;

counter = 2;

while(counter < 10){

++counter;

scanf("%f",&number);

if (number > largest){
largest = number;

}/*end if*/

if (number > secondLargest){

secondLargest = number;

}/*end if */

}/* end while */

if(secondLargest > largest){

temp = largest;
largest = secondLargest;
secondLargest = temp;

}/* end if */

printf("The largest number entered was: %.2f",largest);
printf("\nThe second largest number entered was: %.2f",secondLargest);

return 0;

}/* end function main */```

2. O_o

2 1 3 4 5 6 7 8 9 -> 9 1

One strikes me as being particularly less than three AND less than two.

Soma

3. I don't know how to fix that problem.

4. It is a hint, not merely a problem.

5. Code:
```if (number > largest){
largest = number;
}/*end if*/

if (number > secondLargest){
secondLargest = number;
}```
There is an issue with the logic here. If the input is 1 2 3 4 5 6 7 8 9, your code will tell the user that the largest value is 9 and the second largest value is 9. I'm guessing this isn't the behaviour you're looking for. What happens is that both evaluations turn out true when the input is 9, and thus both largest and secondlargest is set to 9. What you want to do is if either if is true, then set the corresponding variable, and end execution of the current iteration of the while loop. This can be accomplished with the 'continue' keyword.

Also, i would do the check/swap of the largest and secondlargest variables before the while-loop, but after the initial 2 inputs.

Actually, scratch that. What i would do is gather all 10 variables, store them in an array, sort the array, and return the last to entries after sorting. But that's just me.

6. Originally Posted by Neo1
What i would do is gather all 10 variables, store them in an array, sort the array, and return the last to entries after sorting. But that's just me.
Well, that would be space inefficient, and possibly inefficient in time if you sort the whole array.

7. Originally Posted by laserlight
Well, that would be space inefficient, and possibly inefficient in time if you sort the whole array.
Ah, but less bug prone and faster to accomplish since i wouldn't really have to write the code myself, just sort the array with whatever sort you have lying around, and the rest is 1 or 2 lines of code. Besides, when the size of the input is fixed to 9, the difference in execution time would probably be in nanoseconds.

8. Originally Posted by Neo1
Ah, but less bug prone and faster to accomplish since i wouldn't really have to write the code myself, just sort the array with whatever sort you have lying around, and the rest is 1 or 2 lines of code.
Maybe we should use C++ so we have easy access to std::partial_sort and std::nth_element

Originally Posted by Neo1
Besides, when the size of the input is fixed to 9, the difference in execution time would probably be in nanoseconds.
I agree, but then if it were me, I wouldn't even solve this problem because I don't need to, i.e., consider that it is an academic problem to teach students, hence in practice the student may have to apply whatever knowledge acquired here to a problem that involves much larger input.

9. What does "two largest" actually mean? Say I had 2, 3, 1, 3.

Are the "two largest" 3 and 3, or are they 2 and 3?

What if all the numbers are equal?

10. Originally Posted by laserlight
I agree, but then if it were me, I wouldn't even solve this problem because I don't need to, i.e., consider that it is an academic problem to teach students, hence in practice the student may have to apply whatever knowledge acquired here to a problem that involves much larger input.
I see what you're getting at, this is a contrived example. However oftentimes, all user input is stored in an array or similar anyways, i very rarely find myself polling the user for a lot of input and then immediately disregarding 80% of it. So in a real world situation, even if the input was much larger than 9 elements, it would probably still have to be loaded into memory, and since sorting can be done in-place, the memory overhead would be zero.