$ cat score pchen72 50 71 55 93 115 jmaszk 45 76 49 88 102 bvbui 59 78 53 96 145
ID: 3670587 • Letter: #
Question
$ cat score
pchen72 50 71 55 93 115
jmaszk 45 76 49 88 102
bvbui 59 78 53 96 145
mtcrowle 33 65 39 82 100
mrchave3 54 77 56 98 158
Write an awk script that computes the average score for each student, as well as the class average. You are to define a function that accepts an array as parameter, compute and return the average of its elements In the processing block, assign each field (except the first) as element of an array, then pass that array to the function.
The output should be :
pchen72 76.8
jmaszk 72
bvbui 86.2
mtcrowle 63.8
mrchave3 88.6
END OF FILE
Explanation / Answer
Consider both the AWK files and both the database files and use shell script to run them on the terminal
------------------------------------------------------------------
gatename.awk
BEGIN {
found=0
}
{
if($3==grno)
{
found=1
print "Name:"$1" "$2 " GRNO:"$3
}
}
END {
if(found==0)
{
print "No such student found"
}
}
report.awk
BEGIN {
count=0
found=0
loop=0
sum=0
}
{
if($1==grno)
{
found=1
count++
marks[count]=$4
subject[count]=$3
}
}
END {
if(found==1)
{
print "********************************"
print " SRNO Subject Marks "
print "********************************"
for(loop=1;loop<=count;loop++)
{
sum=sum+marks[loop]
}
for(loop=1;loop<=count;loop++)
{
printf "%d %20s %d ",loop,subject[loop],marks[loop]
print "********************************"
}
print "************"
print " Sum="sum
print "************"
print "Average="sum/count
print "************"
Average=sum/count
if(Average>90)
{
print "Class=AA"
}
if(Average<90 && Average>80)
{
print "Class=AB"
}
if(Average<80 && Average>70)
{
print "Class=BB"
}
if(Average<70 && Average>60)
{
print "Class=BC"
}
if(Average<60 && Average>50)
{
print "Class=CD"
}
if(Average<50 && Average>40)
{
print "Class=DD"
}
if(Average<40)
{
print "Class=FR"
}
}
else
{
print"No records found"
}
}
reportGen.sh
#!/bin/bash
echo "Enter the GRNO whose record you want to generate"
read grno
awk -f'getname.awk' grno=$grno student.db
awk -f'report.awk' grno=$grno subject.db
Student.db
Ritwik Kuity 111122
Shekhar Bhabad 111443
Saurabh Chaudhari 111152
Akshay Pawar 111434
subject.db
111152 1 EngineeringGraphics 80
111152 1 AlgebraandGeometry 85
111152 1 Physics 83
111152 1 Chemistry 81
111152 1 Spanish 74
111434 3 Maths 84
111434 3 InorganicChemistry 79
111434 3 Excel 77
111434 3 OrganicChemistry 76
111434 3 Flute 89
111443 4 Probablity 74
111443 4 Transformations 78
111443 4 Microcontrollers 80
111443 4 ComputerOrganization 75
111443 4 Guitar 78
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.