fork download
  1. #include<stdio.h>
  2.  
  3. #define STARHEAD "*******************************************************\n"
  4. #define SHOWHEAD " Show information for all students \n"
  5. #define SORTHEAD " Output from high to low according to grades \n"
  6. #define WRAPHEAD "\n"
  7.  
  8. struct Student {
  9. int number;
  10. char name[15];
  11. int score;
  12. };
  13.  
  14. struct Student student[10] = {
  15. {8, "imz", 84},
  16. {30, "pzh", 30},
  17. {28, "xhx", 88},
  18. {51, "vbi", 81},
  19. {35, "spe", 91},
  20. {14, "apm", 77},
  21. {7, "lyv", 71},
  22. {9, "fey", 87},
  23. {16, "uvv", 100},
  24. {42, "aap", 67}
  25. };
  26.  
  27. void show();
  28. void sort();
  29.  
  30. void show()
  31. {
  32. int n;
  33. printf(STARHEAD);
  34. printf(SHOWHEAD);
  35. printf(STARHEAD);
  36. for (n = 0; n < 10; n++) {
  37. printf("number:%2d name:%s score:%d\n", student[n].number, student[n].name, student[n].score);
  38. }
  39. printf(WRAPHEAD);
  40. }
  41.  
  42. void sort()
  43. {
  44. int i, j, h, k;
  45. struct Student temp;
  46. printf(STARHEAD);
  47. printf(SORTHEAD);
  48. printf(STARHEAD);
  49. for (i = 0; i < 9; i++) {
  50. k = i;
  51. for (j = i + 1; j < 10; j++) {
  52. if (student[i].score < student[j].score) {
  53. k = j;
  54. temp = student[i];
  55. student[i] = student[k];
  56. student[k] = temp;
  57. }
  58. }
  59. }
  60. for (h = 9; h >= 0; h--) {
  61. printf("number:%d name:%s score:%d\n", student[h].number, student[h].name, student[h].score);
  62. }
  63. }
  64.  
  65. int main()
  66. {
  67. show();
  68. sort();
  69. return 0;
  70. }
Success #stdin #stdout 0s 5612KB
stdin
Standard input is empty
stdout
*******************************************************
           Show information for all students           
*******************************************************
number: 8 name:imz score:84
number:30 name:pzh score:30
number:28 name:xhx score:88
number:51 name:vbi score:81
number:35 name:spe score:91
number:14 name:apm score:77
number: 7 name:lyv score:71
number: 9 name:fey score:87
number:16 name:uvv score:100
number:42 name:aap score:67

*******************************************************
      Output from high to low according to grades      
*******************************************************
number:30 name:pzh score:30
number:42 name:aap score:67
number:7 name:lyv score:71
number:14 name:apm score:77
number:51 name:vbi score:81
number:8 name:imz score:84
number:9 name:fey score:87
number:28 name:xhx score:88
number:35 name:spe score:91
number:16 name:uvv score:100