Finding the two largest numbers

This is a discussion on Finding the two largest numbers within the C Programming forums, part of the General Programming Boards category; Hi, i'm trying to find the two largest numbers out of ten. I've re-done this problem several times and it ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    4

    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;
    
    
        printf("Please enter a number:\t");
        scanf("%f",&number);
    
    
        number = largest;
    
    
        printf("\nPlease enter a number:\t");
        scanf("%f",&number);
    
    
        number = secondLargest;
    
    
        counter = 2;
    
    
        while(counter < 10){
    
            ++counter;
    
            printf("\nPlease enter a number:\t");
            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. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,214
    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. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    4
    I don't know how to fix that problem.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    It is a hint, not merely a problem.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    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.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by laserlight View Post
    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.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    Quote 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

    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    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?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by laserlight View Post
    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.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding next largest integer multiple
    By trinitrotoluene in forum C Programming
    Replies: 2
    Last Post: 06-16-2010, 05:48 PM
  2. Finding the 3 largest numbers of the five entered
    By Bkgrant in forum C Programming
    Replies: 11
    Last Post: 02-13-2009, 12:08 PM
  3. Finding largest number in array
    By el_chupacabra in forum C Programming
    Replies: 2
    Last Post: 01-22-2009, 01:31 PM
  4. finding the largest ascending subsequence..
    By transgalactic2 in forum C Programming
    Replies: 92
    Last Post: 01-21-2009, 07:57 AM
  5. Replies: 26
    Last Post: 09-06-2007, 02:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21