Guest User

Untitled

a guest
Apr 26th, 2012
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <conio.h>
  4. #include <stdlib.h>
  5.  
  6.  
  7. #define NO_OF_STUDENTS 4
  8.  
  9. struct data {
  10. char name[20];
  11. int age;
  12. int weight;
  13. };
  14.  
  15.  
  16. int compare (const void * a, const void * b);
  17.  
  18.  
  19. int main()
  20. {
  21. struct data Students[NO_OF_STUDENTS]; // 20 students.
  22. int Counter = 0;
  23. char temp[20];
  24. int i,j;
  25. for (Counter = 0; Counter < NO_OF_STUDENTS; ++Counter) {
  26. scanf("%s", Students[Counter].name);
  27. scanf("%d", &Students[Counter].age);
  28. scanf("%d", &Students[Counter].weight);
  29. }
  30.  
  31. for (Counter = 0; Counter < NO_OF_STUDENTS; ++Counter) {
  32. printf("Name=%s, ", Students[Counter].name);
  33. printf("Age=%d, ", Students[Counter].age);
  34. printf("Weight=%d\n", Students[Counter].weight);
  35. }
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45. qsort(Students,4,sizeof(int),compare);
  46.  
  47.  
  48. for(i=1;i<5;i++)
  49. {
  50. for(j=1;j<5;j++)
  51. {
  52. if(strcmp(Students[j-1].name,Students[j].name)>0)
  53. {
  54. strcpy(temp,Students[j-1].name);
  55. strcpy(Students[j-1].name,Students[j].name);
  56. strcpy(Students[j].name,temp);
  57. }
  58. }
  59. }
  60.  
  61.  
  62.  
  63.  
  64. for (Counter = 0; Counter < NO_OF_STUDENTS; ++Counter) {
  65. printf("Name=%s, ", Students[Counter].name);
  66. printf("Age=%d, ", Students[Counter].age);
  67. printf("Weight=%d\n", Students[Counter].weight);
  68. }
  69.  
  70.  
  71.  
  72.  
  73. return 0;
  74.  
  75.  
  76. }
  77.  
  78.  
  79.  
  80.  
  81.  
  82. int compare (const void * a, const void * b)
  83. {
  84. return ( *(int*)a - *(int*)b );
  85. }
Advertisement
Add Comment
Please, Sign In to add comment