So I've been trying to figure out this program for my assignment but keep running into bug after bug and it's at the point where I don't know where to go anymore. The program description can be seen here: http://www.cs.ucf.edu/courses/cop322...s/Program4.pdf . Please excuse my code, I'm not very good at this at all so it probably seems very messy. I hope it's clear enough for you to follow.
Attachment 10461
Any help would be greatly appreciated! This is killing my brain. I guess I'm not very cut out for programming.
Edit: Oh, forgot, I have some extra 'printf's and such in there because I've been trying to debug. please excuse them.
Code:
#include <stdio.h>
#define MAX_CONTESTANTS 101
#define MAX_JUDGES 100
int main ()
{
//Arrays
int scores [MAX_CONTESTANTS][MAX_JUDGES];
double averages [MAX_JUDGES];
double std_devs [MAX_JUDGES];
int bad_judges [MAX_JUDGES];
//initialize each array to 0
int r, c;
for (r = 0; r < MAX_JUDGES; r++){
bad_judges [r] = 0;
}
int shows, num_contestants, num_audience, threshold;
//input
//open file
FILE *ifp;
ifp = fopen("ucfidol.txt", "r");
//read number of shows
fscanf (ifp, "%d", &shows);
//loop for each show
int index;
for (index = 0; index < shows; index++){
//read number of cases from file
fscanf (ifp, "%d %d %d", &num_contestants, &num_audience, &threshold);
//printf ("%d, %d, %d", num_contestants, num_audience, threshold);
//system("PAUSE");
//read scores from each judge into array
//system("pause");
for ( c = 0; c < num_contestants; c++) {
for ( r = 0; r < num_audience; r++){
fscanf(ifp, "%d", &scores[c][r]);
}
}
for ( c = 0; c < num_contestants; c++) {
for ( r = 0; r < num_audience; r++){
printf ( "%d\n", scores[c][r]);
}
//system("PAUSE");
}
//calculate and store judge average in array
int sum;
double avg;
for (r = 0; r < num_audience; r++) {
sum = 0;
avg = 0;
for (c = 0; c < num_contestants; c++){
sum += scores [c][r];
}
//printf("%d", sum);
//system("PAUSE");
avg = (double)sum / num_contestants;
printf("%lf", avg);
system("PAUSE");
averages[r] = avg;
}
//printf("%lf", averages[0]);
//printf("%lf", averages[1]);
//printf("%lf", averages[2]);
//printf("%lf", averages[3]);
//system("PAUSE");
//calculate and store standard deviation in array
double x, difsum;
for (r = 0; r < num_audience; r++) {
difsum = 0;
for (c = 0; c < num_contestants; c++){
x = pow((averages [r] - scores [c][r]), 2);
difsum += x;
}
//printf("%lf", difsum);
//system("PAUSE");
std_devs[r] = sqrt(difsum / num_contestants);
}
//store array of bad judges
int i = 0;
for (r = 0; r < num_audience; r++) {
//check each standard of deviation against threshold
if (std_devs [r] > threshold) {
bad_judges [i] = r+1;
i++;
}
}
for (i=0; i<num_audience; i++)
printf("%d", bad_judges[i]);
system("PAUSE");
int winner, j, tossed;
double high_score = 0;
//sum all scores for each contestant
for (r = 0; r < num_contestants; r++){
sum = 0;
avg = 0;
for (c = 0; c < num_audience; c++){
sum += scores [r][c];
}
//subtract scores of bad judges
tossed = 0;
for (j = 0; j < num_audience; j++){
if (bad_judges [j] != 0) {
sum -= scores [r][bad_judges[j]];
tossed++;
}
}
//calculate average for contestant
avg = (double)sum / (num_audience - tossed);
//if highest score, replace winning score and contestant
if (avg > high_score){
high_score = avg;
winner = r+1;
}
}
//output
//print results
printf("UCF IDOL SHOW #%d\n", index + 1 );
printf("Contestant #%d, you win with an average score of %lf.\n", winner, high_score );
if (tossed = 0)
printf("All judges' scores were used.");
else {
printf("The following judges' scores were thrown out:");
for (j = 0; j < num_audience; j++){
if (bad_judges [j] != 0) {
printf("%d ", bad_judges [j]);
}
}
printf("\n\n");
}
}
system("PAUSE");
fclose (ifp);
return 0;
}
Like I said, there's a few extra printfs in there, some of them commented out. I've managed to get it to stop crashing and I don't get any errors but there seems to be a problem starting at the point where standard deviation is calculated. I haven't determined yet if that's the only place with a problem, but that's as far as I've gotten through the program.
Edit again: also here's the txt file for test cases. Attachment 10462