Advertisement
Slyfoxx724

p7.c

Dec 7th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1.  
  2. 1 #include <stdio.h>
  3. 2
  4. 3 struct person
  5. 4 {
  6. 5 char first_name[20];
  7. 6 char last_name[20];
  8. 7 int age;
  9. 8 };
  10. 9
  11. 10 void print_person_info(struct person clone);
  12. 11 void sort_by_age(int n, struct person a[]);
  13. 12
  14. 13 int main(void)
  15. 14 {
  16. 15
  17. 16 int i, n=5;
  18. 17
  19. 18 struct person student[5] =
  20. 19 {
  21. 20 {"Bob", "Smith", 21},
  22. 21 {"Jimmy", "John", 18},
  23. 22 {"Amy", "Goldberg", 20},
  24. 23 {"Dan", "Marlo", 17},
  25. 24 {"Sally", "Sorrow", 16}
  26. 25 };
  27. 26
  28. 27 for(i=0; i<n; i++)
  29. 28 print_person_info(student[i]);
  30. 29
  31. 30 sort_by_age(n, student);
  32. 31
  33. 32 printf("---AFTER SORTING------------\n");
  34. 33
  35. 34 for(i=0; i<n; i++)
  36. 35 print_person_info(student[i]);
  37. 36
  38. 37 return 0;
  39. 38 }
  40. 39
  41. 40 void print_person_info(struct person clone)
  42. 41 {
  43. 42 printf("Name = %s %s\n", clone.first_name, clone.last_name);
  44. 43 printf("Age = %i\n\n", clone.age);
  45. 44 }
  46. 45
  47. 46
  48. 47 void sort_by_age(int n, struct person a[])
  49. 48 {
  50. 49 int i, j;
  51. 50 struct person temp;
  52. 51
  53. 52 for(i=0; i<n-1; i++)
  54. 53 {
  55. 54 for(j=i+1; j<n; j++)
  56. 55 {
  57. 56 if(a[i].age > a[j].age)
  58. 57 {
  59. 58 temp = a[i];
  60. 59 a[i] = a[j];
  61. 60 a[j] = temp;
  62. 61 }
  63. 62 }
  64. 63 }
  65. 64
  66. 65 }
  67. 66
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement