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: 3831623 • 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

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 program sample:

#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[32];
    int age;
    struct person *next; //ADDED so that the structure now behaves like a LinkedList node
};

int main(void) {
    /* Declare variables. */
  
    FILE* people_data;
    int number_records;
    struct person *people, *tmp; //we now need pointers to the structures, not fixed length array
    int age_total, age_average;
    people = NULL; //people is the head of the linkedList
    tmp = malloc(sizeof(struct person)); //tmp holds the data temporarily
    number_records = 0;
    /* Read and print information from the file. */
    people_data = fopen(FILENAME,"r");
    if (people_data == NULL) {
        printf("Error opening data file. ");
    }
  
  
    while (2 == fscanf(people_data,"%s %d", tmp->name, &(tmp->age))) { //changes made here to read data properly
        age_total += tmp->age;
   tmp->next = people;
   people = tmp;
        number_records++; //to keep track of how many data being read
        tmp = malloc(sizeof(struct person)); //create another temporay node to store next line of data
    }
  
    age_average = age_total/number_records; //calculate avg
  
    tmp = people;
    while(tmp!=NULL) { //loop to traverse the LinkedList
        if(tmp->age >=age_average) {
            printf("%s ", tmp->name);
        }
   tmp = tmp -> next;
    }

    fclose(people_data);
  
    return EXIT_SUCCESS;
}

INPUT FILE:

Sam 54
Swati 52
Isha 40
Pragnik 52

Output:

Pragnik Swati Sam

In the code I have commented the lines I have changed. I tried my best to keep the code simple and easy. If you still have any doubt or having trouble understanding the code, please feel free to comment below. I shall be glad to help you.

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