write the following functions : a. float get_section_average(char *filename, int
ID: 3746133 • Letter: W
Question
write the following functions :
a. float get_section_average(char *filename, int section_num);
This function will open a datafile whose name will be passed as an argument in filename, read the records, and calculate the “average score” of all students enrolled in section_num. That is, for each student, you will check to ensure that the section number (last field) matches section_num. If it does, then you will add their score (third field) to a total sum and increment a counter tracking the number of students enrolled in the section. Once all records have been read, you will divide the sum by the number of sutdents enrolled and return the class average.
b. int get_top_score(char *filename);
This function will open a datafile whose name will be passed as an argument in filename, read through all records, and return the maximum final score out of all sections.
c. int create_fail_list(char *in_file, char *out_file, int cutoff);
Given a cutoff value cutoff, this function will read in_file and for each line determine whether the final score is below the provided cutoff. If it is, then the section number, followed by “ - “, followed by the student id, followed by “ - “, followed by their name should be written to out_file. For instance, if your input file looked like: Mike Smith;999235486;86%;2 Joe Wilson;534236748;52%;1 Luke Skywalker;532886654;98%;2 Leia Organa Solo;532886655;72%;1 and create_fail_list() was called with a cutoff value of 90, the output would look like: 2 - 999235486 - Mike Smith 1 - 532886654 - Joe Wilson 1 - 532886655 - Leia Organa Solo Finally, the function should track the number of students below the threshold and return it upon completion.
d. int create_grade_report(char *in_file, char *out_file, int section_num);
Given the following grade cutoffs: A >= 90% B >= 80% C >= 70% D >= 60% F < 60% Read all records in in_file and, if they are enrolled in section_num, determine their final letter grade. You should then write the following for each student to out_file: student_id: letter_grade Thus given the same input in the description of create_fail_list(), and providing a section_num of “2”, the out_file would contain: 999235486: B 532886654: A This function should return the number of students that are enrolled in section section_num.
Explanation / Answer
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//a.)
float get_section_average(char *filename, int section_num)
{
FILE *infile;
char line[256];
int comma_identifier=0;
int i,j=-1,k=-1;
int sum=0;
char temp1[3],temp2[2];
int line_count=0;
infile = fopen("C:/Users/temp-01968/Desktop/input.txt", "r"); //filename
if (infile == NULL)
{
fprintf(stderr," Error opening file ");
exit (1);
}
// read file Lines till end of file
while(fgets ( line, sizeof(line), infile ) != NULL)
{
j=-1;k=-1;
memset(temp1, '', sizeof(temp1));
memset(temp2, '', sizeof(temp2));
comma_identifier=0;
for(i=0;line[i]!=''&&i<256;i++)
{
if(comma_identifier==2)
{
if(line[i]!='%'&&line[i]!=';')
{
j++;
temp1[j]=line[i];
temp1[j+1]='';
}
}
if(comma_identifier==3)
{
k++;
temp2[k]=line[i];
temp2[k+1]='';
}
if(line[i]==';')
{
comma_identifier++;
}
}
if(atoi(temp2)==section_num)
{
line_count++;
sum+=atoi(temp1);
}
}
fclose(infile);
if(line_count>0)
{
return (sum/line_count);
}
else
{
printf(" No Matching Session_Num found ");
return 0;
}
}
//b.)
int get_top_score(char *filename)
{
FILE *infile;
char line[256];
int comma_identifier=0;
int i,j=-1,k=-1;
char temp1[3];
int greatest=0;
infile = fopen("C:/Users/temp-01968/Desktop/input.txt", "r"); //filename
if (infile == NULL)
{
fprintf(stderr," Error opening file ");
exit (1);
}
// read file Lines till end of file
while(fgets ( line, sizeof(line), infile ) != NULL)
{
j=-1;k=-1;
memset(temp1, '', sizeof(temp1));
comma_identifier=0;
for(i=0;line[i]!=''&&i<256;i++)
{
if(comma_identifier==2)
{
if(line[i]!='%'&&line[i]!=';')
{
j++;
temp1[j]=line[i];
temp1[j+1]='';
}
if(line[i]=='%')
{
break;
}
}
if(line[i]==';')
{
comma_identifier++;
}
}
if(greatest<atoi(temp1))
{
greatest=atoi(temp1);
}
}
fclose(infile);
return greatest;
}
//c.)
int create_fail_list(char *in_file, char *out_file, int cutoff)
{
FILE *infile,*outfile;
char line[256],line_out[256];
int comma_identifier=0;
int i,j=-1,k=-1,l=-1;
char temp1[20],temp2[10],temp3[3];
infile = fopen("C:/Users/temp-01968/Desktop/input.txt", "r"); //in_file
if (infile == NULL)
{
fprintf(stderr," Error opening file ");
exit (1);
}
// read file Lines till end of file
while(fgets ( line, sizeof(line), infile ) != NULL)
{
j=-1;k=-1;
memset(temp1, '', sizeof(temp1));
comma_identifier=0;
for(i=0;line[i]!=''&&i<256;i++)
{
if(comma_identifier==2)
{
if(line[i]!='%'&&line[i]!=';')
{
j++;
temp1[j]=line[i];
temp1[j+1]='';
}
if(line[i]=='%')
{
break;
}
}
if(line[i]==';')
{
comma_identifier++;
}
}
if(atoi(temp1)<cutoff)
{
j=-1;k=-1,l=-1;
memset(temp1, '', sizeof(temp1));
memset(temp2, '', sizeof(temp2));
memset(temp3, '', sizeof(temp3));
memset(line_out, '', sizeof(line_out));
comma_identifier=0;
for(i=0;line[i]!=''&&i<256;i++)
{
if(comma_identifier==0)
{
if(line[i]!=';')
{
j++;
temp1[j]=line[i];
temp1[j+1]='';
}
}
if(comma_identifier==1)
{
if(line[i]!=';')
{
k++;
temp2[k]=line[i];
temp2[k+1]='';
}
}
if(comma_identifier==3)
{
if(line[i]!=' ')
{
l++;
temp3[l]=line[i];
temp3[l+1]='';
}
}
if(line[i]==';')
{
comma_identifier++;
}
}
strcpy(line_out,temp3);
strcat(line_out,"-");
strcat(line_out,temp2);
strcat(line_out,"-");
strcat(line_out,temp1);
strcat(line_out," ");
outfile=fopen("C:/Users/temp-01968/Desktop/output.txt","a");
if(outfile==NULL)
{
exit(1);
}
fputs(line_out,outfile);
fclose(outfile);
//printf(" ................................ %s ................................ ",line_out);
}
}
fclose(infile);
return 0;
}
int main()
{
float average;
int greatest=0;
if((average=get_section_average("input.txt", 2))!=0)
{
printf(" Average Score = %f ",average);
}
if((average=get_section_average("input.txt", 1))!=0)
{
printf(" Average Score = %f ",average);
}
if((average=get_section_average("input.txt", 3))!=0)
{
printf(" Average Score is %f ",average);
}
greatest=get_top_score("input.txt");
printf(" Greatest Score = %d%% ",greatest);
create_fail_list("input.txt","output.txt", 90);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.