Promi_38

Structure sorting

Dec 6th, 2020
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. /*Write a code to sort the elements firstly according to num of problems solved. If number of
  2. problems solved is equal then according to cgpa, if cgpa is equal then according to their
  3. registration number.
  4. Input:
  5. The number in the first line means number of students. Then each line of input has 3 numbers. The first
  6. number represents the registration number, the second number represents the number of solved
  7. problems and the third number represents the cgpa of the students.
  8. 4
  9. 1 200 3.99
  10. 4 400 3.77
  11. 2 1000 3.55
  12. 33 1000 3.62
  13. Output:
  14. 33 1000 3.62
  15. 2 1000 3.55
  16. 4 400 3.77
  17. 1 200 3.99 */
  18.  
  19. #include<stdio.h>
  20.  
  21. typedef struct {
  22.     int regNo;
  23.     int solvedPrb;
  24.     double cgpa;
  25. } info;
  26.  
  27. int main()
  28. {
  29.     int n, i, j, k;
  30.     scanf("%d", &n);
  31.     info student[n];
  32.  
  33.     for(i = 0; i < n; i++) scanf("%d %d %lf", &student[i].regNo, &student[i].solvedPrb, &student[i].cgpa);
  34.  
  35.     for(i = 0; i < n; i++)
  36.     {
  37.         for(j = i + 1; j < n; j++)
  38.         {
  39.             if(student[i].solvedPrb < student[j].solvedPrb )
  40.             {
  41.                 info temp = student[i];
  42.                 student[i] = student[j];
  43.                 student[j] = temp;
  44.             }
  45.         }
  46.     }
  47.  
  48.     for(i = 0; i < n; i++)
  49.     {
  50.         for(j = i + 1; j < n; j++)
  51.         {
  52.             if(student[i].solvedPrb == student[j].solvedPrb)
  53.             {
  54.                 if(student[i].cgpa < student[j].cgpa)
  55.                 {
  56.                     info temp = student[i];
  57.                     student[i] = student[j];
  58.                     student[j] = temp;
  59.                 }
  60.             }
  61.         }
  62.     }
  63.    
  64.     for(i = 0; i < n; i++)
  65.     {
  66.         for(j = i + 1; j < n; j++)
  67.         {
  68.             if(student[i].cgpa == student[j].cgpa && student[i].solvedPrb < student[j].solvedPrb)
  69.             {
  70.                 if(student[i].regNo > student[j].regNo)
  71.                 {
  72.                     info temp = student[i];
  73.                     student[i] = student[j];
  74.                     student[j] = temp;
  75.                 }
  76.             }
  77.         }
  78.     }
  79.     printf("\n");
  80.     for(int i = 0; i < n; i++) printf("%d %d %.2lf\n", student[i].regNo, student[i].solvedPrb, student[i].cgpa);
  81. }
Advertisement
Add Comment
Please, Sign In to add comment