Answered! Question: I am making a code that takes the five students, prints them, and then sorts them based on age. I……

I am making a code that takes the five students, prints them, and then sorts them based on age. I need help with creating the function that prints them. Please let me know if you see any other errors in addition. Thanks in advanced!

#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 S Sarrow 16 for (i 0: i n i++) print person info (student [i]) sort by age (n student print AFTER SORTING An) for (i 0: i n i++) print person info (student [i]) return 0; void print person info (struct person clone)

Expert Answer

 #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 ———-nn”);

for(i = 0; i < n; i++)
print_person_info(student[i]);

return 0;

}

void sort_by_age(int n, struct person a[])
{

struct person temp;
int i, j;

for(i = 0; i < n – 1; i++)
{
for(j = i + 1; j < n; j++)
{

if(a[i].age > a[j].age)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}

}

void print_person_info(struct person clone)
{

printf(“First Name = %sn”, clone.first_name);
printf(“Last Name = %sn”, clone.last_name);
printf(“Age = %in”, clone.age);
printf(“———————————–n”);
}

Still stressed from student homework?
Get quality assistance from academic writers!