I) (In C Language) Write a function “void loadPeople(char fileName[],Person peop
ID: 3718452 • Letter: I
Question
I) (In C Language) 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
Rickard 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
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 person’s 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);
}
Explanation / Answer
Solution:
code:
#include <stdio.h>
#include <string.h>
typedef struct PERSON {
char name[64];
int age;
int score;
} Person;
void loadPeople(char fileName[],Person people[],int * length) {
FILE *fp = fopen(fileName, "r");
int i;
fscanf(fp, "%d", length);
for (i = 0; i < *length; i++)
fscanf(fp, "%s %d %d", people[i].name, &(people[i].age), &(people[i].score));
}
Person findOldest(Person people[], int length) {
int oldest = 0;
int i;
for (i = 1; i < length; i++)
if (people[i].age > people[oldest].age)
oldest = i;
return people[oldest];
}
void sortByScore(Person people[], int length) {
int i, j;
Person temp;
for (i = 0; i < length - 1; i++)
for (j = 0; j <= length - i - 1; j++)
if (people[j].score < people[ j + 1 ].score) {
temp = people[j];
people[j] = people[ j + 1 ];
people[ j + 1 ] = temp;
}
}
void sortByName(Person people[],int length) {
int i, j;
Person temp;
for (i = 0; i < length - 1; i++)
for (j = 0; j <= length - i - 1; j++)
if (strcmp(people[j].name, people[ j + 1 ].name) > 0) {
temp = people[j];
people[j] = people[ j + 1 ];
people[ j + 1 ] = temp;
}
}
void printPerson(Person p) {
printf("=============================== ");
printf(" name: %s ", p.name);
printf(" age: %d ", p.age);
printf(" score: %d ", p.score);
printf("=============================== ");
}
void printPeople(Person people[],int length) {
int i;
printf("people[] = ");
for (i = 0; i < length; i++)
printPerson(people[i]);
printf("END ");
}
void main(){
Person 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);
}
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)