Thread: I need some quick help...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    8

    I need some quick help...

    Hi I just finished my code for this program I am working on that is due today. I am having 1 problem and I can't seem to figure it out. My averages are being totalled way too high...I've been up midnight working on this. Any help/tips would be greatly appreciated ^_^

    here is my code:

    insert
    Code:
    //Mario Iglesias
    //Date: 03/30/2011
    //COP3223     
    //UF Idol 2
    
    
    
    #include <stdio.h>
    #include <math.h>
    
    int main (void)                              //the begining of the program
    {
        
       
       //Local Variables
       FILE* file_in;                            // Varable for the input file
       int num_show;                             // (n) number shows to compute
       int num_cont;                             // (c) number of contestants being scored
       int num_judge;                            // (a) number of judges scoring contestants
       int stDev_thre;                           // (s) threshold Standard Deviation
       int i,j,k,l,x,y, z;                       // Loop Counters
       int num_cheat;                            // Number of judges whos scores will be dropped
       int n; 
       int winner;                               //Sample size used in StDev calculations
       float deviation;                          // Holds individual deviations                     
       float sum;                                // used for StDev summation
       float sumsqr;                             // used for StDev summation
       float mean;                               // Sample Mean for StDev
       float variance;                           // s^2 used for StDev
       float stDev;                              // Calculated StDeviation
       int avg = 0;                               // hold the number of the winner contestant
       float winAvg = 0;                               //holds the winners average score
       
       file_in = fopen("ucfidol2.txt", "r");     //Opens file for reading
       
        if(file_in == NULL)                      //Displays Error message to user if file_in = Null
        {
            printf("It appears that the file does not exist. Please check you folder.\n"); 
            printf("And re-run Program\n\n");     
            system("PAUSE");
             exit(0);   
        }
       
        fscanf(file_in, "%d", &num_show);         //reads in the number of shows to process
        
        //Loop gathers data for each show and calls the appropriate functions to compute the results
        //Will iterate once for each show. Displaying results at the end of each iteration
        for(z =0;z<num_show;z++)
        {
              num_cheat = 0;
              fscanf(file_in,"%d %d %d",&num_cont, &num_judge, &stDev_thre);
              int scores[num_cont][num_judge];     //Array to hold scores for each contestant
              int unfair[num_judge];               //holds index of unfair judges
        
              //Outer/inner for loops read in show data for processing, And stores in 2D-array
              for(j=0;j<num_cont;j++)              
              {
                   for(k=0;k<num_judge;k++)
                   {
                        fscanf(file_in,"%d",&scores[j][k]);
                   }
              }
              //calculate judge standard deviation per judge
              float value [num_cont];              //holds value used in summation                                           
              i=j=k = 0;                           //clears loop counters for reuse
              
              //Following three for loops used to compute the standard deviation for
              //an individual show.               
              for (l=0; l< num_judge ; l++)                            
              { 
                  sum = sumsqr = n = 0 ;
                  
                  //Computes summations needed to calculate the mean 
                  for(i=0;i<num_cont;i++)
                  {
                      value[i] = scores[i][l];
                      sum += value[i];                                   
                      n += 1; 
                  }                                                                                                                                     
                  mean = sum/(float)n;             //Last step in calculation of the Mean                                                                                                      
                                                 
                  //computes summations needed to calculate the variance (s^2)                                                              
                  for (j=0; j<n; j++)                               
                  {                                                      
                      deviation = mean - value[j];                       
                      sumsqr += (deviation*deviation);                   
                  }                                                      
                  variance = sumsqr/(n-1) ;       //Last step for computing variance                          
                  stDev = sqrt(variance);         //Last step for computing St Deviation
                  
                  //compare each judge standarddev to standardDev threshold
                  if(stDev - 1.857> stDev_thre)
                  {
                      num_cheat++; 
                      unfair[k] = l;
                      k++;
                      printf("%d", unfair[k-1]);
                  }
                  
                  //in the calculation of averages.
                  for(x=0;x<num_cont; x++)
                  {
                      for(y=0;y<num_judge; y++)
                      { 
                          avg += scores[x][y];
                          
                          for(k=0; k<num_cheat; k++)
                          {
                              scores[x][unfair[k]]=0;
                          }
                      }
                      if(winAvg<avg)
                      {
                          winner = (x+1);
                          winAvg = avg ;
                          if(num_cheat == 0 || (num_cheat + 1)== num_judge)
                              winAvg /= num_judge;
                          else
                              winAvg/= (num_judge-num_cheat);
                          
                      }
                  }
                  
              }
                                         
              //calculate averages with/without appropriate judge 
              //print results
              printf("UF IDOL SHOW # %d \n\n\n", (z+1));
              printf("Contestant # %d, You are the winner with an score of %.2f\n\n", winner, winAvg);
              if(num_cheat == 0 || (num_cheat + 1)== num_judge)
              {
                   printf("All Judges scores were used!\n");
              }
              else
              {
                   printf("These judges Scores were not used: Judges #'s");
                   for(l=0; l<num_cheat; l++)
                       printf("%d, ", unfair[l] + 1);
              }
               printf("\n\n\n");        
        }
        
       
       
        
        
        system("pause");
    }//End Main

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Please post a small example of your input file.

    And to use system calls you must include stdlib.h

    Jim

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    8
    Thanks Jim and Sry this is the piece of code I am having trouble with. I just posted the whole thing to try and make understanding my code easier.

    insert
    Code:
     //in the calculation of averages.
                  for(x=0;x<num_cont; x++)
                  {
                      for(y=0;y<num_judge; y++)
                      { 
                          avg += scores[x][y];
                          
                          for(k=0; k<num_cheat; k++)
                          {
                              scores[x][unfair[k]]=0;
                          }
                      }
                      if(winAvg<avg)
                      {
                          winner = (x+1);
                          winAvg = avg ;
                          if(num_cheat == 0 || (num_cheat + 1)== num_judge)
                              winAvg /= num_judge;
                          else
                              winAvg/= (num_judge-num_cheat);
                          
                      }
                  }

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Please post a small example of your input file.
    Where is a sample of your input file.


    Jim

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    8
    Apologies I'm tired right now... lol this is the input fill

    3
    2 4 10
    85 90 100 98
    78 82 47 77
    3 5 12
    85 90 100 98 99
    78 82 47 77 75
    94 6 97 98 99
    2 3 15
    80 20 50
    78 22 53

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    What do you think the results should be for this input file?

    Jim

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    8
    This is what the results should be:

    UCF IDOL SHOW #1
    Contestant #1, you win with an average score of 87.50.
    The following judges’ scores were thrown out: 3 4
    UCF IDOL SHOW #2
    Contestant #3, you win with an average score of 97.00.
    The following judges’ scores were thrown out: 2 3
    UCF IDOL SHOW #3
    Contestant #2, you win with an average score of 51.00.
    All judges’ scores were used.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    It looks to me that you have some variables that are not getting cleared between loops. Check for variables where you are +=, /=, *=, etc. One variable causing problems is avg, it needs to be zeroed out before you start another loop.

    Jim

  9. #9
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    C Program help!
    same class?

  10. #10
    Registered User
    Join Date
    Feb 2011
    Posts
    8
    Bayint Naung are you saying your in the "Same Class" or you go to UCF and are in the same class lol

    and
    jimblumburg
    Thank you so much! I am going to look over it all and see if I find it. I was also wondering... how come I can't send PM's? is there a filter that you must reach X amount of posts before you are able to message people?
    Last edited by MarioI; 03-31-2011 at 11:38 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. Do you know...
    By davejigsaw in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2005, 10:33 AM
  3. Questions on basic Quick Sort
    By Weng in forum C++ Programming
    Replies: 4
    Last Post: 12-16-2003, 10:06 AM
  4. Quick Sort Help
    By NavyBlue in forum C Programming
    Replies: 1
    Last Post: 03-02-2003, 10:34 PM
  5. Replies: 0
    Last Post: 04-30-2002, 07:24 PM