This program I am trying to write has a structure of persons with a first name last name and age for each.
What I need it to do it display the order of the persons before sorting and then display the persons after sorting by age.
So far I have written the overall program but am stuck at the most important step...which is defining the functions that will be doing all of the work.
For sort_by_age I want use bubble sorting by writing the function myself and for print_person_info I am confused on how to print data out of the struct. I am pretty lost here please help me out thank you.
Code:
#include <stdio.h>
struct person
{
char first_name[20];
char last_name[20];
int age;
};
void print_person_info(struct person clone);
void sort_by_age(int n, struct person a[]);
int main(void)
{
int I, n=5;
struct person student[5] =
{
{ "Bob", "Smith", 21},
{ "Jimmy", "John", 18},
{ "Amy", "Goldberg", 20},
{ "Dan", "Marlo", 17},
{ "Sally", "Sorrow", 16}
};
for(I=0; I<n; I++)
print_person_info(student[I]);
sort_by_age(n, student);
printf("------AFTER SORTING-------\n");
for(I=0; I<n; I++)
print_person_info(student[I]);
return 0;
}
void print_person_info(struct person clone)
{
printf("Name = %s %s, person.first_name, person.last_name");
printf("Age = %I, person.age");
}
void sort_by_age(int n, struct person a[])
{
int I=0, j, temp;
for(I=0; j<n-1;i++)
{
for(j=I+1;j<n;j++)
{
if(student[I] > student[j])
{
temp =student[I];
student[I]=student[j];
student[j]=temp;
}
}
}
}