Loop doing calculations

Hello. I'm writing an awk script that looks at a .csv file and calculates the weighted grade for each student based on the scores and categories in the file. I am able to get the script to run the only issue however is that the same score for each student is the same. I'm self-teaching myself the command-line and am horrible at loops. What am I missing here.

Here is some sample data:

Chelsey,Quiz,Q05,99,100       
Chelsey,Quiz,Q06,88,100       

Chelsey,Quiz,Q07,100,100       

Chelsey,Final,FINAL,82,100       

Chelsey,Survey,WS,5,5       

Sam,Homework,H01,19,100       

Sam,Homework,H02,82,100       

Sam,Homework,H03,95,100
 

Here is my code:

BEGIN{
FS = ","
print "Name\tPercent\tLetter"
a=0
}

{

if(a==0){
        a+=1
}

else{
        sum[$1$2] += $4
        total[$1$2] += $5
        students[$1]++
        categories[$2]++

}
}

END{

for (b in students){
        Homework=(sum[$1"Homework"]/total[$1"Homework"])*0.10
        Lab=(sum[$1"Lab"]/total[$1"Lab"])*.30
        Final=(sum[$1"Final"]/total[$1"Final"])*.15
        Quiz=(sum[$1"Quiz"]/total[$1"Quiz"])*.40
        Survery=(sum[$1"Survey"]/total[$1"Survey"])*.05
        percent=(Homework+Lab+Final+Quiz+Survey)*100
        printf "%s\t%.2f\t",b,percent

        if(percent>=90 && percent<=100)
                print "A";

        else if(percent>=80 && percent<90)
                print "B";

        else if(percent>=70 && percent<80)
                print "C";

        else if(percent>=60 && percent<70)
                print "D";

        else
                print "E"



}

}

And the output:

Name    Percent    Letter
Andrew    72.64    C
Chelsey    72.64    C
Shane      72.64    C
Ava         72.64     C
Sam        72.64     C
    table \{ \}tr \{ \}col \{ \}br \{ \}td \{ padding-top: 1px; padding-right: 1px; padding-left: 1px; color: black; font-size: 12pt; font-weight: 400; font-style: normal; text-decoration: none; font-family: Calibri, sans-serif; vertical-align: bottom; border: medium none; white-space: nowrap; \}

In the END section, you're using b as the loop variable but use $1 for building the indices. Won't fly.
And, if one or more of the categories don't exist for a student, their total array element will stay unset and give an error in the division.
Why do you skip line 1, and do so that complicated? Try NR > 1 ...

AND - DON'T start a new thread for a problem you already posted!

1 Like