i need a C program that will do this notes on the code explaining it Write a pro
ID: 3571932 • Letter: I
Question
i need a C program that will do this notes on the code explaining it
Write a program that reads data about people from a file. You are supposed to create a quick data file for testing purposes yourself, which has the following structure: each line contains a record of data about exactly one person each record has the person's name, followed by a space, followed by the person's age Unlike in other assignments, there is no indication of how many records there will be in the file. Also, we don't want to assume that you do a first scan to find out. Instead, I ask you to solve the problem of creating enough space for all the records by incrementally reading one record at a time, put it into a struct person and link that structure to the list of person structures that is already there. This means that your person structure needs to contain a pointer to the next structure in the list. This also means that you need to keep track of the structure that is the head of the list, as well as the structure that is at the end of the list by maintaining corresponding pointers. Here is a critical piece of code. It reads a record from the file, creates the space (i.e. structure) to hold the data and updates the pointers involved: while (2 == fscanf(people_data, "%s %d", person_current rightarrow name, &person;_current rightarrow age)) {person_current rightarrow next = NULL;//provide space for the new last person person_last rightarrow next = (struct person *) malloc(sizeof(struct person));//set the new last person person_last = person_last rightarrow next;//copy the current's person data just read into the new last person *person_last = *person_current;//update age total and number of records age_total += person_current rightarrow age; number_records++;} I want you to write a program that reads person data out of the above described data file, puts the data into a linked list of person structures and then print out the names of those persons, who are older than the average of that sample.Explanation / Answer
people.txt:
adam 40
anna 60
mathew 30
program:
#include<stdio.h>
#include<stdlib.h>
typedef struct person {
char name[80];
int age;
struct person *next;
} person;
int age_total,number_records,avg_age;
struct person *head, *person_current, *person_last;
void loadlistFromFile()
{
FILE *people_data;
if(!(people_data = fopen("people.txt","r"))) {
perror("people.txt");
abort();
}
number_records=0;
age_total=0;
person_current = (struct person *) malloc(sizeof(struct person));
person_last = (struct person *) malloc(sizeof(struct person));
head=person_last;
while (2 == fscanf(people_data,"%s %d", person_current->name, &person_current->age))
{
person_current->next = NULL;
// provide space for the new last person
person_last->next = (struct person *) malloc(sizeof(struct person));
// set the new last person
person_last = person_last->next;
// copy the current's person data just read into the new last person
*person_last = *person_current;
// update age total and number of records
age_total += person_current->age;
number_records++;
}
avg_age=age_total/number_records;
}
int main(){
loadlistFromFile();
struct person *ll=head;
while(ll!=NULL){
if(ll->age>avg_age)
printf("%s age %d is older than average age %d ",ll->name,ll->age,avg_age);
ll=ll->next;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.