Thread: help on find largest and second largest number in array with their position

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    1

    help on find largest and second largest number in array with their position

    Hello..
    Can anybody help me to find inserted numbers, let say 10 numbers and find the largest and second largest number with their position in an array?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Ainon Mohd View Post
    Hello..
    Can anybody help me to find inserted numbers, let say 10 numbers and find the largest and second largest number with their position in an array?
    First, Welcome to the forum, Ainon!


    If I asked you to record say, 10 numbers, and then find the largest and the second largest number, and what their position was in the list you wrote down, could you do it?

    Of course you could.

    So try it. If you need a C tutorial, click on the tab for a good one, right at the top of the forum.

    You have to start it -- that's how we roll here. If you get stuck on something, post up your code, and we'll try and help out - it's pretty quick, and pretty correct - if I do say so.

    But you have to get this program started.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    54
    Hints:

    if arr[x]>arr[y], then arr[x]=arr[y] /// use temporary variables // it derives smaller number, its opposite makes larger number program. you may use loop.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    You could use a data structure which is automatically sorted. BSD tree.h provides some macros which do this for you, but they might be a bit tricky to use at first. For example, if you define a list called IntList il and a pointer to a node IntListNode *iln which contains `index' and `integer' members, then the following would print the largest N items

    Code:
    iln = RB_MAX(IntList, &il);
    for (int i = 1; i <= N; i++) {
        if (iln == NULL) {
            fatal("can't find an ith largest integer (i: %d)\n", i);
        }
        printf("[%d]: %d\n", iln->index, iln->integer);
        iln = RB_PREV(IntList, &il, iln);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Find last largest integer in array?
    By schmidtc in forum C Programming
    Replies: 2
    Last Post: 07-05-2010, 01:00 PM
  2. Find the Largest and Smallest Number
    By Nightsky in forum C Programming
    Replies: 27
    Last Post: 09-04-2006, 03:40 PM
  3. Find largest and second largest number (help)
    By Arkon in forum C++ Programming
    Replies: 6
    Last Post: 01-20-2006, 11:21 PM
  4. find largest in array with recrusion
    By ronenk in forum C Programming
    Replies: 14
    Last Post: 11-15-2004, 02:36 PM
  5. how do u find 2nd largest number??
    By juancardenas in forum C Programming
    Replies: 8
    Last Post: 02-14-2003, 08:28 AM