I'm trying to work out the standard deviation for a set of students marks in different subjects. I'm just a bit stuck on the last calculation I need to do and I'm just not sure what the issue is.
BEGIN {
i=0
printf("\nResults for form 6B\n")
}
$1=="SUBJECT" {
i++
subject[i]=$2
total[i]=0
count[i]=0
printf("\nLits of %s Students\n",subject[i])
printf("Name Mark Pass/Fail\n")
printf("---- ---- ---------\n")
}
NF>2 { mark[i] = ($3+$4)/2
student=$2" "$1
total[i] = total[i]+mark[i]
count[i] = count[i]+1
if (mark[i]>49)
result="Pass"
else
result="Fail"
printf("%-14s%-3d%10s \n",student, mark[i], result)
}
END { top = i
printf("\nSubject Mean Standard Deviation\n")
printf("------- ---- ------------------\n")
var=0
for(i=1;i<=top;i++){
mean[i]=total[i] / count[i]
var+=((mark[i]-mean[i])^2) #Standard deviation not working#
stdev=sqrt(var/count[i])
printf("%16-s%-3d%12d \n",subject[i],mean[i],stdev)
}
}
Forgot to add input file "marks"
FORM 6B
SUBJECT Maths
Smith John 40 50
Evans Mike 50 80
SUBJECT Physics
Jones Tom 35 65
Evans Mike 46 76
Smith John 34 56
SUBJECT Chemistry
Jones Tom 50 60
Evans Mike 30 40
Output I'm getting is Maths 7 Physics 7 Chemistry 11
The correct values are 10 6 10