Thursday 4 February 2016

Bronze medallist (Programming Assignment )

Your college is organising a sporting event. The events are all done and now the task is to prepare the list of medal winners. The sports secretary for some reason assumed that the events will only have Gold and Silver medals but no Bronze medals. The contestants, especially the 3rd place winners were furious (understandably so!). To get things in control the secretary gives you the list of participants in an event and their standing in the event. Your task is to output the list of Bronze medalists in each of the events.


Each event has 10 participants.
Input:  The number of test cases T followed by T lines. Each line has 10 numbers that indicate the time taken by each contestant to complete the task. Each task takes less than or equal to 30 seconds. If there are two bronze winners, print the one with the lower id.
Output: The id of the contestant who secured the Bronze medal.


Example:
Input
1
16 12 34 11 10 5 3 1 15 21
Output
6

Explanation: The 1 in first line indicates that there is only one testcase. The third position is for the player who took 5 sec to finish the task. The player is in the 6th person in the input list. Note that the ids start at 1 and not at 0.


Solution-:

#include<stdio.h> int main() {int n,l,i,j,temp,a[10],b[10]; scanf("%d",&n); for(l=0;l<n;l++) { //as event has 10 participant for(i=0;i<10;i++) {scanf("%d",&a[i]);b[i]=i;} for(i=0;i<3;i++) { for(j=i;j<10;j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; temp=b[i]; b[i]=b[j]; b[j]=temp; } } } if(l==n-1) printf("%d",b[2]+1); else printf("%d\n",b[2]+1); } return 0; }

21 comments:

  1. i tried with selection sort.it didn't help.

    ReplyDelete
    Replies
    1. why are you sorting.?
      simple find the third smallest element and print its position.

      Delete
    2. but we have to print smallest id if there are two third rankers...

      Delete
    3. ok if you are sorting .then do one thing.

      take a different array ,equal to the size of the number of players.
      now put a[1]=1;a[2]=2......a[n]=n;
      now look ..when you sort the array also swap this position...with this you will have a sorted array and a list parallel showing the position of that element in the original list

      Delete
    4. But now i tried that method of finding the third smallest number.I'm confused how to compare indices of that number if it occurs twice.I tried with for loop having two different variables.

      Delete
    5. look
      first clear your head.
      delete your code.
      now start.

      use any sorting technique , in this technique when you swap the elements for sorting also swap their position indices.
      so

      \a[0]=10 b[0]=1
      a[1]=8 b[1]=2
      a[2]=9 b[2]=3
      a[3]=10 b[3]=4
      a[4]=4 b[4]=5

      you have this..in this b[] only specifies their position or you can say a parallel array.
      now when you sort a[]. suppose you swap a[2]<-->a[3]
      so swap b[2]<-->b[3] with this when you have sorted array in a[] you have their initial position in b[].
      so just print b[2]

      Delete
    6. thanks for explanation. i've to take a 2D array as it does sorting for last added list in case of more than 2 cases.

      Delete
    7. It's done. Thanks a lot Killer.

      Delete
  2. Replies
    1. I have solved it using the concept of selection sort.....

      u may try it.......

      Delete
    2. plzzz send me the program

      Delete
    3. please try yourself first and if you have problem please ask.

      Delete
  3. #include

    void medal(int);
    int main()
    {
    int n;
    scanf("%d",&n);
    medal(n);
    return 0;
    }
    void medal(int m)
    {
    int b[15][10],a[15][10],i,j,k,l=m*10,small=0,position;
    for(i=0;ia[i][k])
    {
    small=a[i][k];
    position=k;

    }
    }
    a[i][position]=a[i][j];
    a[i][j]=small;
    }

    }
    for(i=0;i<m;i++)
    {
    for(j=0;j<10;j++)
    {
    if((a[i][2])==(b[i][j]))
    {
    printf("%d",(j+1));
    break;

    }
    }
    if(i<m-1)
    printf("\n");
    }
    }

    ReplyDelete
  4. #include
    int main()
    {
    int t,i,j,k,tp,temp;
    scanf("%d",&t);
    int T[t][10],b[t][10];

    for(i=0;iT[i][k])
    {
    temp=T[i][j];
    T[i][j]=T[i][k];
    T[i][k]=temp;
    tp=b[i][j];
    b[i][j]=b[i][k];
    b[i][k]=tp;
    }
    }
    }
    }

    for(i=0;i<t;i++)
    {
    if(i==(t-1))
    printf("%d",b[i][2]);
    else
    printf("%d\n",b[i][2]);
    }
    return 0;
    }

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete