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