Advertisement
coffeebeforecode

CSE2012 PP1 Q1

Aug 12th, 2021
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.73 KB | None | 0 0
  1. /*
  2. 1.  Some players play a game and their scores (positive integers) are recorded in the form of an array. A player who scores more is the winner. Design an algorithm to find out the winner, next winner, worst player, next worst player by scanning the score sheet only once.    
  3.  
  4. Example: Consider the following 5 players whose score sheet is given by 7,3,5,8,4. Winner: Player 4. Next winner: Player 1. Worst player: Player 2, Next worst player: Player 5.  
  5.  
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10.  
  11. void solve(){
  12.     unsigned int size;
  13.     int *arr;
  14.    
  15.     // Input
  16.     printf("Enter number of players: ");
  17.     scanf("%u", &size);
  18.     arr = (int *)malloc(sizeof(int)*size);
  19.     printf("\nEnter scores: ");
  20.     int i;
  21.     for(i = 0; i < size; i++){
  22.         scanf("%d", &arr[i]);
  23.     }
  24.  
  25.     //solving
  26.  
  27.     int best, secBest, worst, secWorst;
  28.  
  29.     best = secBest = worst = secWorst = 0;
  30.  
  31.     for(i = 0; i < size; i++){
  32.         //printf("\narr[i]=%d\n",arr[i]);
  33.         if (arr[i] >= arr[secBest] || arr[secBest] == arr[best]){
  34.             if (arr[i] > arr[best]){
  35.                 best = i;
  36.             }
  37.             else{
  38.                 secBest = i;
  39.             }
  40.         }
  41.  
  42.         if (arr[i] <= arr[secWorst] || arr[secWorst] == arr[worst]){
  43.             if (arr[i] < arr[worst]){
  44.                 worst = i;
  45.             }
  46.             else{
  47.                 secWorst = i;
  48.             }
  49.         }
  50.         //printf("\nBest: %d\nSecond Best: %d\nWorst: %d\nSecond Worst: %d\n", best+1, secBest+1, worst+1, secWorst+1);
  51.     }
  52.  
  53.     printf("\nBest: Player %d\nSecond Best: Player %d\nWorst: Player %d\nSecond Worst: Player %d\n", best+1, secBest+1, worst+1, secWorst+1);
  54.    
  55. }
  56.  
  57. int main(){
  58.     solve();
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement