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

Write a program that reads data about people from a file. You are supposed to cr

ID: 3575503 • Letter: W

Question

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->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++;

}

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.

To get you going, I attach the simple program that we developed to get you started with structures.

the file is

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

#define FILENAME "./people_data.txt"

/* Define structure to represent a hurricane. */
struct person {
char name[32];
int age;
};

int main(void) {
/* Declare variables. */
  
FILE* people_data;
int number_records;
struct person people[10];
  
int i=0;
int age_total, age_average;
  
/* Read and print information from the file. */
people_data = fopen(FILENAME,"r");
if (people_data == NULL) {
printf("Error opening data file. ");
}
  
fscanf(people_data, "%d", &number_records);
  
while (2 == fscanf(people_data,"%s %d", people[i].name, &people[i].age)) {
age_total += people[i].age;
i++;
}
  
age_average = age_total/number_records;
  
for (i=0; i<number_records; i++) {
if(people[i].age >=age_average) {
printf("%s ", people[i].name);
}
}

fclose(people_data);
  
return EXIT_SUCCESS;
}

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
#define FILENAME "./people_data.txt"
/* Define structure to represent a hurricane. */
struct person {
char *name; //A Pointer to a string that can hold any number of characters
int age;
struct person *next;
};
int main(void) {
/* Declare variables. */

FILE* people_data;
int number_records=0;
  
int age_total=0, age_average = 1000000;
int age=0;
char name[32];
/* Read and print information from the file. */
people_data = fopen(FILENAME,"r");
if (people_data == NULL) {
printf("Error opening data file. ");
}
  
struct person *head = NULL, *tail= NULL;
  
while (EOF != fscanf(people_data,"%s %d", name, &age)) {
printf("%s%s %d ", "Input : ", name, age);
if(head == NULL){
head = (struct person *) malloc(sizeof(struct person));
tail = head;
head->name = name;
head->age = age;
head->next = NULL;
}
else{
tail->next = (struct person *) malloc(sizeof(struct person));
tail = tail->next;
tail->name = name;
tail->age = age;
tail->next=NULL;
}
age_total += age;
number_records++;
}
if(number_records > 0)
age_average = age_total/number_records;
  
struct person *temp;
temp = head;
while(temp != NULL){
if(temp->age > age_average){
printf("Output : %s ", temp->name);
}
temp=temp->next;
}
fclose(people_data);
  
return EXIT_SUCCESS;
}

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