//Assignment 4
//Kyle Weeks
//3/17/11
#include <stdio.h>
#include <math.h>
int main(){
//Load point
FILE *ifp;
//open file
ifp = fopen("/Users/kylew/Documents/example/ucfidol2.txt", "r");
//variables
int con, judg, shows, dev, k=0, j=0, c=0;
fscanf(ifp, "%d", &shows);
//Loop all the shows
for (k=0; k<shows; k++) {
fscanf(ifp, "%d%d%d", &con, &judg, &dev);
int sum[shows][con][judg];
int badjud[judg];
int corp=0, honjud = judg, win=0;
double bestavg=0;
double avga[con], sdev[judg], avgj[judg];
avgj[0]=0.0;
avga[0]=0.0;
sdev[0]=0.0;
badjud[0]=0;
//Loop all the contestants
for (c=0; c<con; c++) {
//Loop all the judges
for (j=0; j<judg; j++) {
//add up all the judges scores
fscanf(ifp, "%d", &sum[k][c][j]);
avga[c] = avga[c] + sum[k][c][j];
avgj[j] = avgj[j] + sum[k][c][j];
}
avga[c]=avga[c]/judg;
}
for (j=0; j<judg; j++) {
avgj[j] = avgj[j] / con;
}
for (j=0; j<judg; j++) {
for (c=0; c<con; c++) {
sdev[j]=sdev[j]+pow(avgj[j] - sum[k][c][j], 2);
}
sdev[j] = sdev[j] / con;
sdev[j] = sqrt(sdev[j]);
//printf("%lf\n",sdev[j]);
}
for (j=0; j<judg;j++){
if (sdev[j] > dev) {
corp = corp + 1;
printf("true %lf,\n", sdev
[j
]);
}
}
if (corp < judg) {
for (c=0; c <con; c++) {
for (j=0; j<judg; j++) {
if (sdev[j] > dev) {
avga[c] = avga[c] * honjud;
//printf("%.2lf \n",avga[c]);
avga[c] = avga[c] - sum[k][c][j];
//printf("%.2lf \n",avga[c]);
honjud = honjud - 1;
avga[c] = avga[c] / honjud;
//printf("%.2lf \n",avga[c]);
}
}
honjud = judg;
if (avga[c] > bestavg){
bestavg = avga[c];
win = c + 1;
}
}
}
else {
for(c=0; c<con;c++) {
if (avga[c] > bestavg) {
bestavg = avga[c];
win = c + 1;
}
}
}
printf("\nUCF IDOL SHOW #%d \n",k
+1);
printf("Contestant #%d, you win with an average score of %.2lf \n",win,bestavg
);
for (j=0; j<judg; j++) {
if (sdev[j]>dev){
badjud[j]=j+1;
}
else{
}
}
}
fclose(ifp);
return 0;
}