Question: In C, please! 1) In this assignment, you will load some Persons (the struct from the previous homework) into an array of people. Next, you

In C, please!

1) In this assignment, you will load some Persons (the struct from the previous homework) into an array of people. Next, you will write a function that finds and returns the oldest person from the array. Lastly, you will write two functions that sorts the people in the array by their score or name, respectively. You are encouraged to re-use part of your solutions from the previous homeworks.

I) Write a function void loadPeople(char fileName[],Person people[],int * length) that reads a file with the format described below. fileName is the name of the file. people is the array where you store the people in the file. (You may assume that the array is long enough to store all the people in the file.) length should be set to equal the number of people that are stored in the file.

Ex. of input file

4 John 22 67

Billy 31 100

Andrew 21 34

Sarah 20 80

N = (#Number of people) Name_of_person_1 Age_of_person_1 Score_of_person_1 .. Name_of_person_N Age_of_person_N Score_of_person_N

Previous Homework Struct

#include

#include

typedef struct {

char name[20];

int age;

double score;

}Person;

Person createPerson(char name[], int age, double score);

void printPerson(Person p);

void changeAge(Person * p,int newAge);

void printPeople(Person people[],int length);

void main(){

Person p = createPerson("Rickard",31,100);

printPerson(p);

changeAge(&p,30);

printPerson(p);

Person people[3];

people[0] = createPerson("John", 22,83);

people[1] = createPerson("Danny", 24,63);

people[2] = createPerson("Marry", 20,81);

printPeople(people,3);

}

Person createPerson(char name[], int age, double score) {

Person p;

strcpy(p.name, name);

p.age = age;

p.score = score;

return p;

}

void printPerson(Person p) {

printf("%s %d %d ", p.name, p.age, p.score);

}

void changeAge(Person *p, int newAge) {

p -> age = newAge;

}

void printPeople(Person people[], int length) {

int i;

for(i=0; i

printf("___Person#%d___ ", i+1);

printPerson(people[i]);

}

}

II) Write a function Person findOldest(Person people[], int length) that returns the oldest person in the array. If there are multiple people that are equally old, just return one of them. Use only a single for loop to solve the problem. You are not allowed to change the order or the people in the array (do NOT sort the array based on the age). III) Write a function void sortByScore(Person people[], int length) that sorts the people in the array based on each persons score. The people should be ordered from highest score to the lowest score. People with the same score may be sorted in any order. IV) Write a function void sortByName(Person people[],int length) that sorts the people in the array based on the name of each person. People with the same name may be sorted in any order.

The function prototypes and a sample main is provided below:

void printPerson(Person p); //from hw5 void printPeople(Person people[],int length); //from hw5

void loadPeople(char fileName[],Person people[],int * length);

Person findOldest(Person people,int length);

void sortByScore(Person people[],int length);

void sortByName(Person people[], int length);

void main(){ Persons people[100]; int length; char fileName[] = "peopleFile";

loadPeople(fileName,people,&length);

printPeople(people,length); Person p = findOldest(people,length);

printPerson(p);

sortByScore(people,length);

printPeople(people,length);

sortByName(people,length);

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!