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

Given a data file named \"students.dat\". Write a C program that reads the file,

ID: 3569540 • Letter: G

Question

Given a data file named "students.dat". Write a C program that reads the file, creates an array of student structures, then processes the array to output many different reports. Use as many defined constants as possible. Please post a screenshot of program output.

Format of the student structure is:
- first name - string, 7 data chars max
- initial - string, 1 data char max
- last name - string, 9 data chars max
- address - nested structure containing:
- street - string, 16 data chars max
- city - string, 11 data chars max
- state - string, 2 data chars max
- zip - string, 5 data chars max
- age - integer
- gpa - double

The program must do the following:

main() creates an array of student structures, then calls a separate function (using
appropriate parameters and return values) to do each of the 7 different tasks below. In
pseudocode:


create an empty array of student structures
read data file for students into the array
print the contents of the array in the same format as the data file
print the full name of the student with the best GPA
calculate and print the average GPA
print the names of all students with GPAs above the average
print the name of the youngest student who has a GPA below average
sort the structures in the array into order from lowest to highest GPA
print the array again (will now be in a different order from last time).

The contents of "student.dat" are:

ANNA A ADAMS 11 A STREET ALLENTOWN PA 11111 11 5.50
BOB B BRADBURY 22 B ROAD BOSTON MA 22222 22 2.22
CARLA C COTTRELL 33 C AVENUE CHICAGO IL 33333 33 3.33
DENNIS D DODD 44 D SQUARE DETROIT MI 44444 44 4.44
ERICA E EVANS 55 E STREET EUGENE OR 55555 55 9.99
FRANK F FIELDS 66 F ROAD FLAGSTAFF AZ 66666 66 6.66
GLENDA G GROGAN 77 G AVENUE GREAT FALLS MT 77777 77 7.77
HARRY H HALL 88 H WAY HONOLULU HI 88888 88 8.88
IDA I IFIELD 99 I STREET INDIANAPOLI IN 99999 99 1.11

Explanation / Answer

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

struct address
{
char street[18];
char city[13];
char state[7];
char zip[5];
};
struct student
{
char first_name[7];
char initial;
char last_name[9];
struct address add;
int age;
double gpa;
};

void readData(struct student students[], int *n);
void print(struct student students[], int n);
void bestGPA(struct student students[], int n);
double averageGPA(struct student students[], int n);
void printAboveGPA(struct student students[], int n, double avg);
void youngestStudent(struct student students[], int n, double avg);
void sort(struct student students[], int n);

int main()
{
struct student students[20];
int n=0;
  
readData(students, &n);
  
print(students, n);
  
bestGPA(students, n);
  
double avg = averageGPA(students, n);
printf("Average GPA is: %.2lf ", avg);
  
printAboveGPA(students, n, avg);
  
youngestStudent(students, n, avg);
  
sort(students, n);
  
printf(" Now sorted based on GPA: ");
print(students, n);
  
return 0;
  
}


void readData(struct student students[], int *d)
{
int n = 0;
FILE *fp;
fp = fopen("students.dat", "r");
  
if(fp == NULL)
printf("FILE CANNOT BE OPENED !! ");
else
{
while(fscanf(fp, "%s %c %s",
students[n].first_name,
&students[n].initial,
students[n].last_name) == 3)
{
  
char add1[10], add2[10], add3[10];
fscanf(fp, "%s %s %s", add1, add2, add3);
  
strcpy(students[n].add.street, add1);
strcat(students[n].add.street, " ");
strcat(students[n].add.street, add2);
strcat(students[n].add.street, " ");
strcat(students[n].add.street, add3);
  
fscanf(fp, "%s %s %s %d %lf", students[n].add.city, students[n].add.state, students[n].add.zip,&students[n].age, &students[n].gpa);
  
n++;
}
}

*d = n;
}

void print(struct student students[], int n)
{
int i;
for( i=0; i<n; i++)
{
printf("%s %c %s %s %s %s %s %d %.2lf "
, students[i].first_name
, students[i].initial
, students[i].last_name
,students[i].add.street
,students[i].add.city
,students[i].add.state
,students[i].add.zip
,students[i].age
,students[i].gpa);
}
}

void bestGPA(struct student students[], int n)
{
struct student s = students[0];
  
int bestGPAIndex = 0;
int i;
for( i=1; i<n; i++)
{
if(students[bestGPAIndex].gpa < students[i].gpa)
{
bestGPAIndex = i;
}
}
  
printf(" The student with best GPA is: %c %s %s ", students[bestGPAIndex].initial, students[bestGPAIndex].first_name,students[bestGPAIndex].last_name);
}

double averageGPA(struct student students[], int n)
{
int i;
  
double total;
double avg;
  
for( i=0; i<n; i++)
{
total += students[i].gpa;
}
  
avg = total/n;
  
return avg;
}

void printAboveGPA(struct student students[], int n, double avg)
{
int i;
printf(" Students with GPA more than average: ");
  
for( i=0; i<n; i++)
{
if(students[i].gpa > avg)
{
printf("%c %s %s ", students[i].initial, students[i].first_name, students[i].last_name);
}
}
}

void youngestStudent(struct student students[], int n, double avg)
{
int i;
printf(" Youngest student with GPA less than average: ");
  
int youngestStudentIndex=students[0].age;
int isFound = 0;
  
  
  
for( i=0; i<n; i++)
{
if(students[i].gpa < avg)
{
if(!isFound)
{
isFound = 1;
youngestStudentIndex = i;
}
  
if(students[youngestStudentIndex].age >= students[i].age)
{
youngestStudentIndex = i;
}
  
}
}
  
if(isFound)
printf("%c %s %s ", students[youngestStudentIndex].initial, students[youngestStudentIndex].first_name, students[youngestStudentIndex].last_name);
else
printf("Not found !! ");
}

void sort(struct student students[], int n)
{
int i,j;
for (i = 0 ; i < ( n - 1 ); i++)
{
for (j = 0 ; j < n - i - 1; j++)
{
if (students[j].gpa > students[j+1].gpa)
{
struct student s = students[j];
students[j] = students[j+1];
students[j+1] = s;
}
}
}
}

------------------------------------------------------------------------

student.dat

ANNA A ADAMS 11 A STREET ALLENTOWN PA 11111 11 5.50
BOB B BRADBURY 22 B ROAD BOSTON MA 22222 22 2.22
CARLA C COTTRELL 33 C AVENUE CHICAGO IL 33333 33 3.33
DENNIS D DODD 44 D SQUARE DETROIT MI 44444 44 4.44
ERICA E EVANS 55 E STREET EUGENE OR 55555 55 9.99
FRANK F FIELDS 66 F ROAD FLAGSTAFF AZ 66666 66 6.66
GLENDA G GROGAN 77 G AVENUE GREATFALLS MT 77777 77 7.77
HARRY H HALL 88 H WAY HONOLULU HI 88888 88 8.88
IDA I IFIELD 99 I STREET INDIANAPOLI IN 99999 99 1.11

------------------------------------------------------------------------

OUTPUT

ANNA A ADAMS 11 A STREET ALLENTOWN PA 11111 11 5.500000
BOB B BRADBURY 22 B ROAD BOSTON MA 22222 22 2.220000
CARLA C COTTRELL 33 C AVENUE CHICAGO IL 33333! 33 3.330000
DENNIS D DODD 44 D SQUARE DETROIT MI 44444, 44 4.440000
ERICA E EVANS 55 E STREET EUGENE OR 555557 55 9.990000
FRANK F FIELDS 66 F ROAD FLAGSTAFF AZ 66666B 66 6.660000
GLENDA G GROGAN 77 G AVENUE GREATFALLS MT 77777M 77 7.770000
HARRY H HALL 88 H WAY HONOLULU HI 88888X 88 8.880000
IDA I IFIELD 99 I STREET INDIANAPOLI IN 99999c 99 1.110000


The student with best GPA is: E ERICA EVANS

Average GPA is: 5.54

Students with GPA more than average:
E ERICA EVANS
F FRANK FIELDS
G GLENDA GROGAN
H HARRY HALL

Youngest student with GPA less than average: A ANNA ADAMS


Now sorted based on GPA:
IDA I IFIELD 99 I STREET INDIANAPOLI IN 99999c 99 1.110000
BOB B BRADBURY 22 B ROAD BOSTON MA 22222 22 2.220000
CARLA C COTTRELL 33 C AVENUE CHICAGO IL 33333! 33 3.330000
DENNIS D DODD 44 D SQUARE DETROIT MI 44444, 44 4.440000
ANNA A ADAMS 11 A STREET ALLENTOWN PA 11111 11 5.500000
FRANK F FIELDS 66 F ROAD FLAGSTAFF AZ 66666B 66 6.660000
GLENDA G GROGAN 77 G AVENUE GREATFALLS MT 77777M 77 7.770000
HARRY H HALL 88 H WAY HONOLULU HI 88888X 88 8.880000
ERICA E EVANS 55 E STREET EUGENE OR 555557 55 9.990000

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