Please Use C Language! In this assignment, you will load some “Persons” (the str
ID: 3715339 • Letter: P
Question
Please Use C Language!
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
Alex 22 67
Bobby 31 100
Mark 21 34
Samantha 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);
void printPeople(Person people[],int length);
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);
}
Please Use C Language!
Explanation / Answer
Before executing below program we need to create a input file for our C program named "peopleFile" & store it in folder where C program is stored.
Following are the sample content of "peopleFile" file for testing the program:-
4
Alex 22 67
Bobby 31 100
Mark 21 34
Samantha 20 80
Program
#include <stdio.h> /* used for printf() scanf() */
#include <string.h> /* used for strcmp() function */
#include <stdlib.h> /* used for exit() function */
/* structure defination */
typedef struct
{
char name[50];
int age,score;
}Person;
/* Functions Prototype */
void printPerson(Person p);
void printPeople(Person people[],int length);
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);
/* main() function */
void main()
{
Person people[100];
int length;
char fileName[] = "peopleFile";
loadPeople(fileName,people,&length); /* calling loadpeople() function */
printf(" Printing the content of file loaded: ");
printPeople(people,length);
Person p = findOldest(people,length); /* calling findOldest() function */
printf(" Oldest Person: ");
printPerson(p);
sortByScore(people,length); /* calling sortByScore() function */
printf(" Printing sort by Score: ");
printPeople(people,length);
sortByName(people,length); /* calling sortByName() function */
printf(" Printing sort by Name:: ");
printPeople(people,length);
} /* end of main() function */
/* Function will load the content of file into 'people' array */
void loadPeople(char fileName[],Person people[],int * length)
{
FILE *fp;
int i;
fp=fopen(fileName,"r"); /* open file for reading */
if(fp==NULL) {
printf("File cannot be opened");
exit(1);
}
fscanf(fp,"%d",length); /* reading number of people in file */
for(i=0;i < *length;i++) {
fscanf(fp,"%s%d%d",people[i].name,&people[i].age,&people[i].score); /* reading name, age & score of people from file & storing into 'people' array */
}
close(fp); /* closing file */
} /* end of function */
/* Function will return the oldest person in the array */
Person findOldest(Person people[],int length)
{ int oldest,loc,i;
oldest=people[0].age;
loc=0;
for(i=0;i < length;i++)
{
if(people[i].age>oldest) {
oldest=people[i].age;
loc=i;
}
}
return(people[loc]);
}
/* function to sort the 'people' array by score */
void sortByScore(Person people[],int length)
{ Person temp;
int i,j;
for(i=0;i<length-1;i++)
{
for(j=0;j<length-i-1;j++)
{
if(people[j].score<people[j+1].score) {
//swap the elements of arrays
temp=people[j]; people[j]=people[j+1]; people[j+1]=temp;
}
}
}
}
/* function to sort the 'people' array by name */
void sortByName(Person people[], int length)
{ Person temp;
int i,j;
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) {
//swap the elements of arrays
temp=people[j]; people[j]=people[j+1]; people[j+1]=temp;
}
}
}
}
/* function to print details of single person */
void printPerson(Person p)
{
printf(" Person-Name Person-Age Person-Score ");
printf("%s %d %d ",p.name,p.age,p.score);
}
/* function to print the all the content of 'people' array */
void printPeople(Person people[],int length)
{ int i;
printf(" Person-Name Person-Age Person-Score ");
for(i=0;i < length;i++) {
printf("%s %d %d ",people[i].name,people[i].age,people[i].score);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.