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

Write a function “void loadPeople(char fileName[],Person people[],int * length)”

ID: 3715657 • Letter: W

Question

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); } in C code, and make sure it can compiles pls thank you

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
#include<string.h>

// Structure definition for person
typedef struct
{
// Data member to store data
char name[20];
int age;
int score;
}Person; // End of structure

// Prototype of functions
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 definition
void main()
{
// Array of objects of type Person created
Person people[100];
// To store number of records
int length;
// Stores the file name
char fileName[] = "peopleFile.txt";

// Calls the function to read file contents and stores it in array of objects people
loadPeople(fileName, people, &length);
// Calls the function to display all persons information
printPeople(people, length);

// Calls the function to find out the oldest person in the list and returns it
Person p = findOldest(people, length);
// Calls the function to display a person information
printPerson(p);

printf(" Sort by score");
// Calls the function to sort the people information based of score
sortByScore(people, length);
// Calls the function to display all people information
printPeople(people,length);

printf(" Sort by name");
// Calls the function to sort the people information based of name
sortByName(people,length);
// Calls the function to display all people information
printPeople(people,length);
}// End of main function

// Function to display a person information
void printPerson(Person p)
{
printf(" Oldest Person Name: %s Age: %d Score: %d", p.name, p.age, p.score);
}// End of function

// Function to display all people information
void printPeople(Person people[], int length)
{
int c;
for(c = 0; c < length; c++)
printf(" Name: %s Age: %d Score: %d", people[c].name, people[c].age, people[c].score);
}// End of function

// Function to read file contents and stores it in array of objects people
void loadPeople(char fileName[], Person people[], int *length)
{
// File pointer declared
FILE *readF;
int count;
int c = 0;

// Opens the file for reading and checks whether it can be opened or not
if ((readF = fopen(fileName, "r")) == NULL)
{
// If unable to open shows error message
printf("Error! in opening file for reading %s", fileName);
// Program exits if the file pointer returns NULL.
exit(1);
}// End of if condition

// Reads number of records in the file
fscanf(readF, "%d", &count);

// Loops till end of file
for(c = 0; c < count; c++)
{
// Reads name of the person and stores it at c index position of array object's name data member
fscanf(readF,"%s", people[c].name);
// Reads name of the person and stores it at c index position of array object's age data member
fscanf(readF,"%d", &people[c].age);
// Reads name of the person and stores it at c index position of array object's score data member
fscanf(readF,"%d", &people[c].score);
}// End of for loop
// Assigns number of records to length
*length = c;
}// End of function

// Function to find out the oldest person in the list and returns it
Person findOldest(Person people[], int length)
{
// Loop variable
int c;
// Initially starting index position age is stored as the minimum age
int oldest = people[0].age;
// Initially starting index position of people array of objects is stored as the minimum age object
Person p = people[0];

// Loops till number of records
// Starts from one because zero is already considered above
for(c = 1; c < length; c++)
{
// Checks if current person age is greater than the earlier oldest age
if(people[c].age > oldest)
{
// Updates the age with current persons age
oldest = people[c].age;
// Assigns the current object as the oldest age object
p = people[c];
}// End of if condition
}// End of for loop
// Returns the person object
return p;
}// End of function

// Function to sort the persons by score
void sortByScore(Person people[], int length)
{
// Loop variables
int x, y;
// For swapping
Person p;
// Loops till number of records
for(x = 0; x < length; x++)
{
// Loops till number of records minus outer loop variable and minus one times
// Because after every iteration of outer loop one value is sorted
for(y = 0; y < length - x - 1; y++)
{
// Checks if the score at y index position is greater than the score at next index position
if(people[y].score > people[y + 1].score)
{
// Swapping name
p = people[y];
people[y] = people[y + 1];
people[y + 1] = p;
}// End of if condition
}// End of inner for loop
}// End of outer for loop
}// End of function

// Function to sort the persons by name
void sortByName(Person people[], int length)
{
// Loop variables
int x, y;
// For swapping
Person p;
// Loops till number of records
for(x = 0; x < length; x++)
{
// Loops till number of records minus outer loop variable and minus one times
// Because after every iteration of outer loop one value is sorted
for(y = 0; y < length - x - 1; y++)
{
// Checks if the name at y index position is greater than the name at next index position
if(strcmp(people[y].name, people[y + 1].name) > 0)
{
// Swapping name
p = people[y];
people[y] = people[y + 1];
people[y + 1] = p;
}// End of if condition
}// End of inner for loop
}// End of outer for loop
}// End of function

Sample Output:


Name: John Age: 22 Score: 67
Name: Rickard Age: 31 Score: 100
Name: Andrew Age: 21 Score: 34
Name: Sarah Age: 20 Score: 80

Oldest Person
Name: Rickard Age: 31 Score: 100

Sort by score
Name: Andrew Age: 21 Score: 34
Name: John Age: 22 Score: 67
Name: Sarah Age: 20 Score: 80
Name: Rickard Age: 31 Score: 100

Sort by name
Name: Andrew Age: 21 Score: 34
Name: John Age: 22 Score: 67
Name: Rickard Age: 31 Score: 100
Name: Sarah Age: 20 Score: 80

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote