Consider a file containing grade test grades for students called grades.txt file:
Last,First,A/I,Test1,Test2,Test3
Smith,John,A,90,100,99
Ballard,Sue,I,15,,50
Clark,Sally,A,100,50,
Koen,Jack,I,90,100,99
Ball,Lucy,A,100,100,100
Bently,Mark,A,100,,100
The file is comma delimited. The first line is a header. The records contain Last Name, First Name, Active/Inactive file and several test scores. Ignore the heading line. For each Active student calculate their average grade for each student printing out the Students First Name followed by Last Name and average score. Display a heading at the beginning with a name for the report and headings for the columns of Name and Average.
After all students have been processed display the number of active students and the average class test score.
Format the output so that only 1 digit after the decimal point is shown and the averages scores line up on the decimal points.
In this file, for example, the gawk program should print the following:
$ gawk -f minor1.gawk grades.txt
Student Average Test Scores
Name Average
John Smith 96.3
Sally Clark 50.0
Lucy Ball 100.0
Mark Bently 66.7
4 Students with average score of 78.2
I assume using gawk/awk. It should be inside a .gawk file, using c
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void readFromFile(); //declaration of function
int main()
{
readFromFile();//function call
return 0;
}
void readFromFile()
{
int len=0,isActive=0, avgCount=0;
char firstName[2], lastName[20],score1[3],score2[3],
score3[3];
int marks1=0, marks2=0, marks3=0, total=0;
float avg=0;
int commaCount=0,a=0,b=0,c=0,d=0,e=0;
char line[200]; //considering max size of any line in
file is 200 character. change if required.
FILE *fileRead = fopen("grades.txt", "r"); //r means opening file
in read mode
if(fileRead ==
NULL)
//NULL means either file does not exists or there is no permission
to open file
{
printf("Error while opening file.");
exit(1); // exit program
}
printf("Student
Average Test Scores\n"); //to print heading
fgets(line, sizeof(line), fileRead); //for
heading
while(fgets(line, sizeof(line), fileRead) !=
NULL) //End of file returns NULL. When NULL
found, it will stop.
{
len=strlen(line);//length of line
commaCount=0,a=0,b=0,c=0,d=0,e=0,isActive=0;//initialize to 0
for(int i=0; i<len; i++)//itrate from begining to
end for each line
{
if(line[i]==',')
{
commaCount++; //comma will decide about type of
data
}
else
{
if(commaCount==0)//means it is part of last name
{
lastName[a++]=line[i];
}
else if(commaCount==1)//means it is part of first name
{
firstName[b++]=line[i];
}
else if(commaCount==2)////means it is part of active or not
active
{
if(line[i]=='I')
isActive=1;
}
else if(commaCount==3)//means it is part of first score
{
score1[c++]=line[i];
}
else if(commaCount==4)//means it is part of second score
{
score2[d++]=line[i];
}
else if(commaCount==5)//means it is part of third score
{
score3[e++]=line[i];
}
}
}
firstName[b++]='\0';//initialize it to
nothing
lastName[a++]='\0';//initialize it to
nothing
score1[c++]='\0';//initialize it to
nothing
score2[d++]='\0';//initialize it to
nothing
score3[e++]='\0';//initialize it to
nothing
marks1 = atoi(score1);//convert char array to
integer
marks2 = atoi(score2);//convert char array to
integer
marks3 = atoi(score3);//convert char array to
integer
total = marks1 + marks2 + marks3;//calculate
total score
avg=(float)total/3;//calculate avg
if(avg>=78.2)
avgCount++;//keep a track of all
students having avg>78.2
//this is to print all the data
//printf("\n%s %s %s %s %s %d
%.2f\n", firstName, lastName, score1, score2, score3,total,
avg);
if(isActive==0)
printf("%s %s
%.1f\n",
firstName, lastName, avg);
}
//print list of students avg>78.2
printf("\n\n%d Students with average score of 78.2\n\n",
avgCount);
fclose(fileRead);
//Close file pointer once task is finished
}
Output:

Note: Its 3 students only not 4 with average > 78.3
Code Screenshot:



Consider a file containing grade test grades for students called grades.txt file: Last,First,A/I,Test1,Test2,Test3 Smith,John,A,90,100,99 Ballard,Sue,I,15,,50 Clark,Sally,A,100,50,...