Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write the missing sort and display function. home / study / questions and answer

ID: 673881 • Letter: W

Question

Write the missing sort and display function.

home / study / questions and answers / engineering / computer science / write the sort and display function. #include #include ...

Your question has been posted.

We'll notify you when a Chegg Expert has answered. Post another question.

Question

Edit question

Write the sort and display function.

#include <stdio.h>
#include<stblib.h>


#define MAX_CHAR 64

struct person {
char name[MAX_CHAR];
int age;

};

void swap(struct person **a,struct person **b);
void sort(struct person **foo, int n);
void displayAll(struct person **foo,int n);

int main(void){
struct person **group;
int n,i;

printf("How many people are in the group?");
scanf (" %d,&n");


//alocate pointer array

group =(struct person **) malloc(n*sizeof(struct person*));

//Ask individual name and age for each member.
for(i=0;i<n;i++)
{
group[i]=(struct person **) malloc(n*sizeof(struct person*));
printf(" Name if member?");
scanf(" %s", group[i]->name);
printf("nAge of member?");
scanf (" %d", &(group[i]->age ));
}


//call sort function
sort(group,n);

//call displayAll function
displayAll(group,n);


return 0

}

Write both the missing sort() and displayAllO functions missing in the code below. The sort function takes in an array of pointers where each pointer can point to a person structure. This sort function should sort based on age from low to high. The declaration of the sort function should look like: void sort(struct person **foo, int n); Within the sort() function you will have to use the swap0 function void swap(struct person *"a, struct person **b) The displayAll(O) function also takes in an array pointers and the number of elements in the array of pointer. The displayAll(0 the members. function should print the name and age for all void display All(struct person "foo, int n); Below is some example code for the main function and the structure declaration write the code for the sort function on the back of this paper.

Explanation / Answer

#include <stdio.h>

#include<stdlib.h>

#define MAX_CHAR 64

struct person {

char name[MAX_CHAR];

int age;

};

void swap(struct person **a,struct person **b);

void sort(struct person **foo, int n);

void displayAll(struct person **foo,int n);

int main(void){

struct person **group;

int n,i;

printf("How many people are in the group?");

scanf ("%d",&n);

  

//alocate pointer array

group =(struct person **) malloc(n*sizeof(struct person*));

//Ask individual name and age for each member.

for(i=0;i<n;i++)

{

group[i]=(struct person *) malloc(n*sizeof(struct person*));

printf(" Name if member?");

scanf(" %s", group[i]->name);

printf(" Age of member?");

scanf ("%d", &(group[i]->age));

}

  

//call sort function

sort(group,n);

//call displayAll function

displayAll(group,n);

  

return 0;

}

void swap(struct person **a,struct person **b) {

person temp = *(a[0]);

*(a[0]) = *(b[0]);

*(b[0]) = temp;

}

void sort(struct person **foo, int n) {

for(int i=0;i<n;i++) {

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

if((foo[i])->age > (foo[j])->age) {

swap(&foo[i], &foo[j]);

}

}

}

}

void displayAll(struct person **foo,int n) {

for(int i=0;i<n;i++) {

printf("Name: %s, Age: %d ", (foo[i])->name, (foo[i])->age);

}

}