ile attached Write a program that reads data about peoplefrom a file. You are su
ID: 655836 • Letter: I
Question
ile attached Write a program that reads data about peoplefrom a file. You are supposed tooeate aquickdata fiefortesting purposes yourself which has the ainclude estdio.hy following sructure include adefine FILENAME /people data.txt each inecortarsarecord of data about exactly one person each record has the person's rame, olowedbyaspace, folowed by the person's ae Define structure to represent a hurricane. struct person Unlike in other assignments, there is roindication of how manyrecordsthere wil beinthe fleAlso, we don't want to assune that you do afrit Scan char nane[321; tofind out. Instead, lask youto solve the problem of aeatingenough space for altherecords by incrementally reading one recordatatime, putit int age into a struct person and link that structure to the listof person structures thatisaready there. This means that your personstructure needsto containapointer to the next structure in the list. This also means that you need tokeep track of the structure that is the head ofthelist,aswellas the int nain(void) H structure thatisattheend of the listby maintainingcorresponding pointers. Ve Declare variables, Hereisaortical piece of code.lt readsarecord from the file, creates the space e.structure to hold the data and updates the pointersinvolved FILEx people data; int number records while (2 fscanf(people data,"is id", person current nane, 6person current->age)) struct person people[10]: int itt0 person current->next NULL int age total, age average: ll provide space for the new last person Ve Read and print information fron the file. e/ person last- next (struct person nalloo (sizeof(struct person)); people data fopen(FILENAME,"r")i if (people data mm NULL) ll set the new last person printf ("Error opening data file. Vn") person lastE person last->next; copy the current's person data just read into the new last person fscanf(people data "wd", &nunber; records) kperson last xperson current; While (2 fscanf (people data,"ss vd", peoplelil.nane, Epeopleli].age)) H age total people[i].age: i++ update age total and nunber of records age total t: person current- age; age average age total/number records number records++; for (is0 ienunber records i++) if (peoplelil age age average) printf("ts people lil nate lwant yout write aprogram that readspersondataoutoftheabowe described daafileput the dataintoilinked ist of personsnuctures and then y pritoutthe names of those persons who are older than the average of that sample. close people data) return EXIT SUCCESSExplanation / Answer
#include<stdio.h>
#include<stdlib.h>
#define FILENAME "input.txt" // here change to your file.
struct person{
char name[32];
int age;
struct person* next;
};
int main(){
FILE* people_data;
int number_records=0;
int i=0;
int age_total,age_average;
people_data = fopen(FILENAME, "r");
if(!people_data){
printf("Error opening the data file ");
return 0;
}
//Create Space for person current.
struct person* person_current = (struct person*)malloc(sizeof(struct person));
//Read data for person current;
fscanf(people_data, "%s %d",person_current->name,&person_current->age);
// Create space for person last.
struct person* person_last = (struct person*)malloc(sizeof(struct person));
// Copy data just read from person current to person last.
*person_last = *person_current;
// point begin to the first person...
struct person* begin = 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 person data just read into new last person;
*person_last = *person_current;
//update age_total and number of records;
age_total += person_current->age;
number_records++;
}
age_average = age_total/number_records;
while(begin){
if(begin->age >= age_average)
printf("%s ",begin->name);
begin = begin->next;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.