#include <stdio.h> #include <stdlib.h> /* Define structure to represent a hurric
ID: 3576031 • Letter: #
Question
#include <stdio.h>
#include <stdlib.h>
/* 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("person_data.txt","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;
printf("Person above average age are: ");
for (i=0; i<number_records; i++) {
if(people[i].age >=age_average) {
printf("%s ", people[i].name);
printf("%d ",people[i].age);
}
}
fclose(people_data);
return EXIT_SUCCESS;
}
this is the output that is giving me please fix
this is the text file data :
5
Khalid 31
Ali 57
Rizwan 19
Ammar 18
Fida 13
this is the homework instructions:
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
/* 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("person_data.txt","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++;
}
int x = number_records, y = age_total;
age_average = y/x;
//printf("%d %d %d ",number_records, age_total, age_average);
printf("Person above average age are: ");
for (i=0; i<x; i++) {
if(people[i].age >=age_average) {
printf("%s ", people[i].name);
printf("%d ",people[i].age);
}
}
//age_average = age_total/number_records;
fclose(people_data);
return EXIT_SUCCESS;
}
/* Output
Person above average age are:
Khalid 31
Ali 57
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.