Advertisement
silentkiler029

Structure-sort-swe-19

Aug 30th, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.40 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. typedef struct {
  5.     char name[80];
  6.     double cgpa;
  7.     int regNo;
  8. } information;
  9.  
  10. int main()
  11. {
  12.     information var_students[5];    ///0.1.2.3.4
  13.  
  14.     strcpy(var_students[0].name, "Mehraj Ul Islam");
  15.     var_students[0].cgpa = 3.85;
  16.     var_students[0].regNo = 1001;
  17.  
  18.     strcpy(var_students[1].name, "Shawon Majid");
  19.     var_students[1].cgpa = 3.75;
  20.     var_students[1].regNo = 1002;
  21.  
  22.     strcpy(var_students[2].name, "Samiul Islam Mugdha");
  23.     var_students[2].cgpa = 3.88;
  24.     var_students[2].regNo = 1003;
  25.  
  26.     strcpy(var_students[3].name, "Promi Mojumder");
  27.     var_students[3].cgpa = 3.98;
  28.     var_students[3].regNo = 1004;
  29.  
  30.     strcpy(var_students[4].name, "Mohammad Ibrahim");
  31.     var_students[4].cgpa = 4.00;
  32.     var_students[4].regNo = 1005;
  33.  
  34.  
  35.     for(int i = 0; i < 5; i++) {
  36.         for(int j = i + 1; j < 5; j++) {
  37.             if(var_students[i].cgpa < var_students[j].cgpa) {
  38.                 ///swap
  39.                 information temp;
  40.                 temp = var_students[i];
  41.                 var_students[i] = var_students[j];
  42.                 var_students[j] = temp;
  43.             }
  44.         }
  45.     }
  46.  
  47.  
  48.     for(int i = 0; i < 5; i++) {
  49.         printf("Name: %s\n", var_students[i].name);
  50.         printf("Registration Number: %d\n", var_students[i].regNo);
  51.         printf("CGPA: %0.2lf\n", var_students[i].cgpa);
  52.     }
  53.  
  54.     return 0;
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement