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

Write the sort and displayAll() functions #include <stdio.h> #include<stblib.h>

ID: 673945 • Letter: W

Question

Write the sort and displayAll() functions

#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

}

Explanation / Answer

#include <stdio.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() {
   struct person **group;
   int n, i;

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

   printf(" No of people : %d ",n);
//alocate pointer array

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

   struct person *person;
//Ask individual name and age for each member.

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

   //displayAll(group, n);

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

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

   return 0;

}

void swap(struct person **a, struct person **b) {
       char tempname[MAX_CHAR];
       int tempAge;

       strcpy(tempname, (*a)->name);
       tempAge = (*a)->age;

       strcpy((*a)->name, (*b)->name);
       (*a)->age = (*b)->age;

       strcpy((*a)->name,tempname);
       (*b)->age = tempAge;

}

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

   int i,j;
   for(i = 0; i < n-1; i++) {
       for(j = i; j < n; j++) {
           if(foo[i]->age > foo[j]->age) {
               swap(&foo[i], &foo[j]);
           }
       }
   }
}

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

   int i;
   for(i = 0; i < n; i++) {
       printf(" Name: %s   Age: %d", foo[i]->name, foo[i]->age);
   }
}