how do u find 2nd largest number??

This is a discussion on how do u find 2nd largest number?? within the C Programming forums, part of the General Programming Boards category; i need to find the two largest vaules of 10 numbers using a while loop. so far i can only ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    6

    how do u find 2nd largest number??

    i need to find the two largest vaules of 10 numbers using a while loop.

    so far i can only find the largest number but can't fingure out how to get the 2nd largest number.

    here is how i find the largest number

    Code:
    int counter = 1, number, largest = 0, second_largest = 0;
    
    
    while ( counter <= 10 ) {
         printf("enter number: ");
         scanf("%d", &number );
    
           if ( number > largest )
                    number = largest;
         
            counter++;
     }
    how do i find the second largest number???
    Last edited by juancardenas; 02-12-2003 at 10:56 PM.

  2. #2
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    First of all, when you paste a code, add code tags!
    Second, look here:

    2nd_largest = 0;
    Variables names cannot start with numbers, so change it.
    First, you make largest = second_largest. then in the loop if the number isn't bigger than the largest you put it on second largest and check again.

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Don't forget to change the second_largest in case the largest changes.

    Code:
    if number > largest then
        second_largest = largest
        largest = number
    else if number > second_largest then
        second_largest = number

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    6
    ok!

    i got this and it seems to work also

    Code:
    if ( second_largest < number )
    	second_largest = largest;
    
    if ( largest < number )
    	largest = number;
    Last edited by juancardenas; 02-13-2003 at 02:29 PM.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by juancardenas
    ok!

    i got this and it seems to work also

    Code:
    if ( second_largest < number )
    	second_largest = largest;
    
    if ( largest < number )
    	largest = number;
    No. that's wrong. Consider the following three numbers being read consecuivly:

    1
    2
    3
    Code:
    if( second number (0) < 1 )
        second number = 1
    if( largest number (0) < 1 )
        second number = 1
    if( second number (1) < 2 )
        second number = 2
    if( largest number (1) < 2 )
        second number = 2
    if( second number (2) < 3 )
        second number = 3
    if( largest number (2) < 3 )
        second number = 3
    In your sample code, both number and second number end up being the same

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    11
    I would suggest doing a descending sort array and then calling the function with some pointer arithmetic. Example,


    printf("%d", *iptr);
    iptr++;
    printf("%d", *iptr);

  7. #7
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Talking

    Code:
    I would suggest doing a descending sort array and then calling the function with some pointer arithmetic. Example,
    
    
    printf("%d", *iptr);
    iptr++;
    printf("%d", *iptr);

    He could also do the same without using pointers! Keep it simple if at all possible.
    Mr. C: Author and Instructor

  8. #8
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Something like that I think it should work:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        int nFirst, nSecond, nNumber;
        nFirst = nSecond = 0;
        
        printf("Enter numbers, -1 to exit\n");
        do {
            scanf("%d",&nNumber);
            if (nNumber > nFirst)
            {
                nSecond = nFirst;
                nFirst = nNumber;
            }
            
            else if (nNumber > nSecond)
                    nSecond = nNumber;
        } while (nNumber != -1);
        
        printf("The biggest number: %d and the second biggest: %d\n",nFirst,nSecond);
        system("PAUSE");
        return 0;
    }

  9. #9
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    everyone here seems to be assuming that the numbers are positive and therefore at the beginning setting the largest and second largest to 0.

    you should take into acount that some or all of the numbers may be negative.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code to find th square root of a number
    By CODY21 in forum C++ Programming
    Replies: 34
    Last Post: 10-29-2010, 09:27 AM
  2. finding the largest number in a binary search
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 07-31-2008, 03:19 AM
  3. Find largest and second largest number (help)
    By Arkon in forum C++ Programming
    Replies: 6
    Last Post: 01-20-2006, 10:21 PM
  4. Determining the Largest number??
    By gqchynaboy in forum C++ Programming
    Replies: 4
    Last Post: 08-28-2003, 11:27 PM
  5. Replies: 2
    Last Post: 08-03-2003, 10:01 AM

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