Thread: C Program help!

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    9

    C Program help!

    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
    Last edited by mastrxplodr; 03-30-2011 at 09:02 PM. Reason: posted code in quotes.

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Why don't you post your relevant code here in code tag?
    What problem exactly are you facing?
    crash? produce incorrect output? then post expected and actual output. input.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There are precious few people who actually want to compile and run random code to hunt for errors you are too lazy to mention.

    1. Post your code in code tags.
    2. State exactly what it is that you are having problems with.
    3. Post any errors you get, and highlight the lines in your code that correspond with them.

    edit - beaten to the punch

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    9
    I posted the code in the quote tags but I don't know if that was the correct way to do it since it didn't seem to keep any indentation..

    Edit: nevermind, fixed that.

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    9
    well that's the same exact problem, which is cool, but they don't go past the initial step of filling out the scores array. I have that part complete and it's the execution of the latter part of the program that is giving me problems.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    9
    Ahhh, finally got it to work, hours later. thanks anyway.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM