Thread: Finding the two largest numbers

  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
    5,108
    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
    28,413
    It is a hint, not merely a problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,318
    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, 01:08 PM
  3. Finding largest number in array
    By el_chupacabra in forum C Programming
    Replies: 2
    Last Post: 01-22-2009, 02:31 PM
  4. finding the largest ascending subsequence..
    By transgalactic2 in forum C Programming
    Replies: 92
    Last Post: 01-21-2009, 08:57 AM
  5. Replies: 26
    Last Post: 09-06-2007, 02:35 AM