I have this program, which currently has 2 functions (not including main): #incl
ID: 3546823 • Letter: I
Question
I have this program, which currently has 2 functions (not including main):
#include<stdio.h>
#include<stdlib.h>
#define MAX 50
typedef struct{
int id;
int grade;
} data;
data students[MAX];
int loadArray(char* filename)
{
FILE *f1; // file pointer
int n; // store the no of records
int i=1;
char c[100]; // stroing the word while reading the the file word by word
f1=fopen(filename,"r");
/* fopen returns 0, the NULL pointer, on failure */
if(f1 == 0)
{
printf( "Could not open file " );
return 0; // file error no record exists so returning 0
}
else
{
fscanf(f1,"%s",c);
n=atoi(c); // reading the first word which is no of records
while(i<=n) // looping n times
{
fscanf(f1,"%s",c);
students[i].id=atoi(c); // converting string to int
fscanf(f1,"%s",c);
students[i].grade=atoi(c);
i++;
}
fclose(f1);
}
return n;
}
void printArray(int size)
{
int i=1;
printf(" Student record ");
printf("ID Grade ");
while(i<=size)
{
printf("%d %d ",students[i].id,students[i].grade);
i++;
}
}
main(int argc, char *argv[])
{
int rec;
if(argc != 2) // argc should be 2 for correct execution
{
printf( "Please provide proper command line argument ");
}
else
{
// argv[1] is a filename to open
rec=loadArray(argv[1]);
if(rec>0) // checking record exists then print.
printArray(rec);
}
}
I need these 4 functions added to this program (2 of them are basically the same though):
int findHighestgrade(int size): This functions takes an integer which is the size of the global structure array and finds
the student with the highest grade. It returns the index of the student with the highest grade.
int findLowestgrade(int size): Same as above, except it returns the index of the student with the lowest grade.
float averageClassgrade(int size): This functions takes an integer which is the size of the global structure array and
computes the average grade for the class. The average that is computed is returned.
int writeContent(char* fname, int size, int low, int high, float avg): This function takes the output file name,
size of the global structure array, index of the lowest grade, index of the highest grade, and the average grade. This
function will write the following information to the output file:
1. All the student information stored in the global structure array.
2. Information of the student with the lowest grade.
3. Information of the student with the highest grade.
4. Average class grade.
Input File Contents (input.txt):
(7 is just the size of the first row)
Update File Contents (update.txt):
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
#define MAX 50
typedef struct
{
int id;
int grade;
} data;
data students[MAX];
int loadArray(char* filename)
{
FILE *f1; // file pointer
int n; // store the no of records
int i=1;
char c[100]; // stroing the word while reading the the file word by word
f1=fopen(filename,"r");
/* fopen returns 0, the NULL pointer, on failure */
if(f1 == 0)
{
printf( "Could not open file " );
return 0; // file error no record exists so returning 0
}
else
{
fscanf(f1,"%s",c);
n=atoi(c); // reading the first word which is no of records
while(i<=n) // looping n times
{
fscanf(f1,"%s",c);
students[i].id=atoi(c); // converting string to int
fscanf(f1,"%s",c);
students[i].grade=atoi(c);
i++;
}
fclose(f1);
}
return n;
}
void printArray(int size)
{
int i=1;
printf(" Student record ");
printf("ID Grade ");
while(i<=size)
{
printf("%d %d ",students[i].id,students[i].grade);
i++;
}
}
main(int argc, char *argv[])
{
int rec;
if(argc != 2) // argc should be 2 for correct execution
{
printf( "Please provide proper command line argument ");
}
else
{
// argv[1] is a filename to open
rec=loadArray(argv[1]);
if(rec>0) // checking record exists then print.
printArray(rec);
}
}
int findHighestgrade(int size) // This functions takes an integer which is the size of the global structure array and finds
//the student with the highest grade. It returns the index of the student with the highest grade.
{
int highest_grade_index=0,i;
for(i=1; i<size; i++)
{
if(students[i].grade>students[highest_grade_index].grade)
{
highest_grade_index = i;
}
}
return highest_grade_index;
}
int findLowestgrade(int size)
{
int lowest_grade_index=0,i;
for(i=1; i<size; i++)
{
if(students[i].grade<students[lowest_grade_index].grade)
{
lowest_grade_index = i;
}
}
return lowest_grade_index;
}
float averageClassgrade(int size)// This functions takes an integer which is the size of the global structure array and
// computes the average grade for the class. The average that is computed is returned.
{
float sum = 0;
int i;
for(i=0; i<size; i++)
sum = sum + students[i].grade;
return sum/size;
}
int writeContent(char* fname, int size, int low, int high, float avg) // This function takes the output file name,
// size of the global structure array, index of the lowest grade, index of the highest grade, and the average grade. This
// function will write the following information to the output file:
// 1. All the student information stored in the global structure array.
// 2. Information of the student with the lowest grade.
// 3. Information of the student with the highest grade.
// 4. Average class grade.
{
FILE* foutp = fopen(fname,"w");
int i=1;
fprintf(foutp," Student record ");
fprintf(foutp,"ID Grade ");
while(i<=size)
{
fprintf(foutp,"%d %d ",students[i].id,students[i].grade);
i++;
}
fprintf(foutp,"The student with the highest grade has the id %d with the grade %d",students[high].id,students[high].grade);
fprintf(foutp,"The student with the lowest grade has the id %d with the grade %d",students[low].id,students[low].grade);
fprintf(foutp,"The average grade for the class %.2f",avg);
fclose(foutp);
return 0;
}