Advertisement
fahimkamal63

Player Ranking

Jun 20th, 2021
752
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.53 KB | None | 0 0
  1. #include<stdio.h>
  2. #define N 5
  3.  
  4. struct player
  5. {
  6.     char name[30];
  7.     int seq;
  8.     int point;
  9. };
  10. struct player input(struct player st[],int n);
  11. struct player output(struct player st[],int n);
  12. void swap(struct player *xp, struct player *yp);
  13. void selectionSort(struct player st[], int n);
  14.  
  15. void main()
  16. {
  17.  
  18.     struct player s[N];
  19.     printf("Enter name and points of 5 players\n");
  20.     input(s,N);
  21.     selectionSort(s, N);
  22.     output(s,N);
  23. }
  24.  
  25.  
  26.  
  27. struct player input(struct player st[],int n)
  28. {
  29.     for(int i=0; i<n; i++)
  30.     {
  31.         st[i].seq=i+1;
  32.         printf("information of player number %d\n",st[i].seq);
  33.         printf("enter name : ");
  34.         scanf("%s",st[i].name);
  35.         printf("enter points : ");
  36.         scanf("%d",&st[i].point);
  37.     }
  38. }
  39.  
  40. void swap(struct player *xp, struct player *yp)
  41. {
  42.     struct player temp = *xp;
  43.     *xp = *yp;
  44.     *yp = temp;
  45. }
  46.  
  47. void selectionSort(struct player st[], int n)
  48. {
  49.     int i, j, min_idx;
  50.     for (i = 0; i < n-1; i++)
  51.     {
  52.         min_idx = i;
  53.         for (j = i+1; j < n; j++)
  54.           if (st[j].point > st[min_idx].point)
  55.             min_idx = j;
  56.         swap(&st[min_idx], &st[i]);
  57.     }
  58. }
  59.  
  60.  
  61. struct player output(struct player st[],int n)
  62. {
  63.     printf("\nplayers info in terms of points\n");
  64.     printf("\n\tRANK\t\t\tNAME\t\t\tPOINT\n");
  65.     printf("-----------------------------------------------------------------------\n");
  66.     for(int i=0; i<n; i++)
  67.     {
  68.         printf("\t%d\t\t\t%s\t\t\t%d\n",i+1,st[i].name,st[i].point);
  69.     }
  70. }
  71.  
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement