Fatal division by zero attempted

Hello. I'm writing an awk script that looks at a .csv file and calculates the weighted grade based on the scores and categories in the file. I keep getting a fatal division by zero attempted error and I know what it means but I've been looking over the code for awhile and am not sure what is causing it. Here is the code that I have:

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

{

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

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

END{

for (student 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
        total=Homework+Lab+Final+Quiz+Survey
}


printf "%s\t%.2f\t",student, total

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

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

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

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

else
        print "E"

}

Data?

Your indices don't seem to be consistent.

as soon as I posted I saw the error. Should ha e been sum[$1$2] and not [$1]. However, I am now getting the error "fatal: attempt to use array `total' in a scalar context"

why are you using total in 2 diff context?

        total[$1] += $5

and

       total=Homework+Lab+Final+Quiz+Survey

Also sum[$1] += $4 is indexed by a single index - not by $1$2

1 Like

DON'T start two threads for the same problem.

See your other thread.