Please help. I have to write this C program and I don\'t know what to put in eac
ID: 3575263 • Letter: P
Question
Please help. I have to write this C program and I don't know what to put in each prototype function and I must use all of them. Any help would be appreciated. At the bottom, I pasted the included code as plain text as well.
#include <stdio.h>
#include <stdlib.h>
#define MAX_STUDENTS 20 #define MAX_LINE 100
// field widths #define FIRST 7 #define MIDDLE 1 #define LAST 9 #define STREET 16 #define CITY 11 #define STATE 2 #define ZIP 5 #define AGE 2 #define GPA 4
typedef struct {
char street[STREET + 1]; char city[CITY + 1]; char state[STATE + 1]; char zip[ZIP + 1];
} Address;
typedef struct {
char first[FIRST + 1]; char initial[MIDDLE + 1]; char last[LAST + 1];
Address address; int age; double gpa;
} Student;
void strsub(char *buffer, char *s, int start, int end);
int readData(Student students[]);
void outputData(Student students[], int num);
void bestGPA(Student students[], int num);
double averageGPA(Student students[], int num);
void aboveAverage(Student students[], int num, double avgGPA);
void youngestBelowAverage(Student students[], int num, double avgGPA);
void sort(Student students[], int num);
//Students.c: Lab 6 students. void main(void)
{
Student students[MAX_STUDENTS ];
int num;
double avgGPA;
num = readData(students); outputData(students, num); bestGPA(students, num); avgGPA = averageGPA(students, num); aboveAverage(students, num, avgGPA); youngestBelowAverage(students, num, avgGPA); sort(students, num); outputData(students, num);
}
//strsub: copy buffer to s from start for size void strsub(char *buffer, char *s, int start, int size)
{
int i, j, finish; i = start;
j = 0;
finish = start + size; while (i < finish) s[j++] = buffer[i++];
s[j] = '';
}
//readData: read file, populate array of structs, return # of Students int readData(Student students[])
{
int i = 0;
char buf[MAX_LINE], temp[20];
FILE *fp;
if (!(fp = fopen(Mstudents.datM, "r"))) { puts("File not found."); exit(1);
}
while (!feof(fp)) {
fgets(buf, MAX_LINE, fp); strsub(buf, students[i].first, 0, FIRST); strsub(buf, students[i].initial, 8, MIDDLE); strsub(buf, students[i].last, 10, LAST); strsub(buf, students[i].address.street, 20, STREET); strsub(buf, students[i].address.city, 37, CITY); strsub(buf, students[i].address.state, 49, STATE); strsub(buf, students[i].address.zip, 52, ZIP);
strsub(buf, temp, 58, AGE); students[i].age = atoi(temp); strsub(buf, temp, 61, GPA); students[i].gpa = atof(temp);
++i;
}
return i;
}
//outputData: output array of structs in easily read format void outputData(Student students[], int num)
{
int i;
printf(" OUTPUT DATA ");
printf("----------- ");
for (i = 0; i < num; i++) {
printf("%s ", students[i].first); printf("%s ", students[i].initial); printf("%s ", students[i].last); printf("%s ", students[i].address.street); printf("%s ", students[i].address.city); printf("%s ", students[i].address.state); printf("%s ", students[i].address.zip); printf("%d ", students[i].age); printf("%.2lf ", students[i].gpa);
}
}
//bestGPA: print the full name of the student with the best GPA void bestGPA(Student students[], int num)
{
}
//averageGPA: calculate and print the average GPA double averageGPA(Student students[], int num)
{
return 123.456; //required to compile. Rewrite this appropriately
}
//aboveAverage: print the names of all students with GPAs above the average
void aboveAverage(Student students[], int num, double avgGPA)
{
[Text Box: //youngestBelowAverage: print the name of the youngest student who has a GPA below average]
}
void youngestBelowAverage(Student students[], int num, double avgGPA)
{
}
//sort: sort the structures in the array into order from lowest to highest GPA void sort(Student students[], int num)
{
}
Anthony W. Smith, 2016 CSCI 112 Programming Fundamentals I Lab 6. Students Lab 6. Students You are given a data file containing information about students. Design, code and test a C program that reads the file, creates an array of student structures, then processes this array to output many different reports. Format of the student structure and data file Format of the student structure is: first name string, 7 data chars max. (So allocate 8 elements to allow for the null character 10') nitial string, 1 data char max string, 9 data chars max last name address nested structure containing: string, 16 data chars max Street string, 11 data chars max city string, 2 data chars max state string, 5 data chars max zip integer age double gpa This information will be read by the program from the "students.dat" data file, which can be downloaded from Blackboard, Course Documents, Week 14 folder. Contents of the data file are 11 A STREET 5.50 ANNA A ADAMS ALLENTOWN B BRADBURY 22 B ROAD MA 22222 22 2.22 BOB BOSTON 33 C IL 33333 33 3.33 CARLA C C OTTRELL AVENUE CHICAGO DENNIS D DODD 44 D SQUARE DETROIT MI 44444 44 4.44 55 EUGENE OR 55555 55 9.99 ERICA E EVANS E S TRE ET FRANK F FIELDS 66 F ROAD FLAGSTAFF AZ 666 66 66 6.66 GLENDA G GROGAN 77 G AVENUE GREAT FALLS MT 77777 77 7.77 HARRY H HALL 88 HONOLULU HI 88888 88 8.88 I FIELD 99 I STREET INDIANAPOLI IN 99999 99 1.11 DA. The main function main appropriate parameters and return valucs to do cach of the 7 different tasks below. In pseudocodeExplanation / Answer
Here is the code for a few more functions. I didn't wrote for all the functions, but tried to complete most of the functions.
#include <stdio.h>
#include <stdlib.h>
#define MAX_STUDENTS 20 #define MAX_LINE 100
// field widths #define FIRST 7 #define MIDDLE 1 #define LAST 9 #define STREET 16 #define CITY 11 #define STATE 2 #define ZIP 5 #define AGE 2 #define GPA 4
typedef struct {
char street[STREET + 1];
char city[CITY + 1];
char state[STATE + 1];
char zip[ZIP + 1];
}Address;
typedef struct {
char first[FIRST + 1];
char initial[MIDDLE + 1];
char last[LAST + 1];
Address address;
int age;
double gpa;
} Student;
void strsub(char *buffer, char *s, int start, int end);
int readData(Student students[]);
void outputData(Student students[], int num);
void bestGPA(Student students[], int num);
double averageGPA(Student students[], int num);
void aboveAverage(Student students[], int num, double avgGPA);
void youngestBelowAverage(Student students[], int num, double avgGPA);
void sort(Student students[], int num);
//Students.c: Lab 6 students.
void main(void)
{
Student students[MAX_STUDENTS ];
int num;
double avgGPA;
num = readData(students);
outputData(students, num);
bestGPA(students, num);
avgGPA = averageGPA(students, num);
aboveAverage(students, num, avgGPA);
youngestBelowAverage(students, num, avgGPA);
sort(students, num);
outputData(students, num);
}
//strsub: copy buffer to s from start for size
void strsub(char *buffer, char *s, int start, int size)
{
int i, j, finish; i = start;
j = 0;
finish = start + size; while (i < finish) s[j++] = buffer[i++];
s[j] = '';
}
//readData: read file, populate array of structs, return # of Students
int readData(Student students[])
{
int i = 0;
char buf[MAX_LINE], temp[20];
FILE *fp;
if (!(fp = fopen(Mstudents.datM, "r")))
{
puts("File not found.");
exit(1);
}
while (!feof(fp))
{
fgets(buf, MAX_LINE, fp);
strsub(buf, students[i].first, 0, FIRST);
strsub(buf, students[i].initial, 8, MIDDLE);
strsub(buf, students[i].last, 10, LAST);
strsub(buf, students[i].address.street, 20, STREET);
strsub(buf, students[i].address.city, 37, CITY);
strsub(buf, students[i].address.state, 49, STATE);
strsub(buf, students[i].address.zip, 52, ZIP);
strsub(buf, temp, 58, AGE);
students[i].age = atoi(temp);
strsub(buf, temp, 61, GPA);
students[i].gpa = atof(temp);
++i;
}
return i;
}
//outputData: output array of structs in easily read format
void outputData(Student students[], int num)
{
int i;
printf(" OUTPUT DATA ");
printf("----------- ");
for (i = 0; i < num; i++)
{
printf("%s ", students[i].first);
printf("%s ", students[i].initial);
printf("%s ", students[i].last);
printf("%s ", students[i].address.street);
printf("%s ", students[i].address.city);
printf("%s ", students[i].address.state);
printf("%s ", students[i].address.zip);
printf("%d ", students[i].age);
printf("%.2lf ", students[i].gpa);
}
}
//bestGPA: print the full name of the student with the best GPA
void bestGPA(Student students[], int num)
{
int best = 0;
for(int i = 0; i < num; i++)
if(students[i].gpa > students[best].gpa)
best = i;
printf("The student with best gpa is: %s.%s.%s ", students[best].last, students[best].initial, students[best].first);
}
//averageGPA: calculate and print the average GPA
double averageGPA(Student students[], int num)
{
double avg = 0;
for(int i = 0; i < num; i++)
avg += students[i].gpa;
printf("The average GPA of the class is: %.2f ", avg/num);
return avg/num; //required to compile. Rewrite this appropriately
}
//aboveAverage: print the names of all students with GPAs above the average
void aboveAverage(Student students[], int num, double avgGPA)
{
printf("The list of students with GPA above the class average is: ");
for(int i = 0; i < num; i++)
if(students[i].gpa > avgGPA)
printf("%s.%s.%s ", students[i].last, students[i].initial, students[i].first)
}
////[Text Box: youngestBelowAverage: print the name of the youngest student who has a GPA below average]
void youngestBelowAverage(Student students[], int num, double avgGPA)
{
}
//sort: sort the structures in the array into order from lowest to highest GPA void sort(Student students[], int num)
{
}