Thread: Need help opening file specified by user and referencing to it within function.

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

    Need help opening file specified by user and referencing to it within function.

    This program is intended to ask the user what file to open then use a function to process the data. I am having trouble figuring out how to use the pointers and addresses to accomplish this. Any ideas?


    Code:
    #include <stdio.h>
    #include <math.h>
    
    #define MAX_CONTESTANTS 9
    #define MAX_JUDGES 100
    
    // Set this to 1 if running DevC++, or 0 otherwise.
    #define DEVCPP 1
    #define DEBUG 0
    
    void ucfidol(FILE* ifp);
    
    int main() {
        //variable for file name
        char fileName[80];
        
        //ask for and read file name
        printf("What is the name of the input file with the UCF Idol data?\n");
        scanf("%s", fileName);
        
        FILE* ifp;
        ifp = fopen ("fileName", "r");
        ucfidol(ifp);
      
            system("PAUSE");
        return 0;
    }
        
        /* 
    Pre-condition: ifp points to the beginning of a file that stores data
                   about UCF Idol shows, as specified in Program 4.
    Post-condition: The results of all the shows in the file will be
                    printed to the screen and FILE pointed to by ifp will
                    be closed.
    */
    void ucfidol(FILE* ifp){
    
        int numCases;
        int i,j,k;
        
        // Open the text file.
        //FILE* ifp
        //fin = fopen ("*ifp", "r");
        
        fscanf(ifp, "%d", &numCases);
        
        int bestContestant = 1;
        double bestScore = 0;
        
        // Go through each show.
        for (i=1; i<=numCases; i++) {
            
            // Read in the parameters for this show.
            int numContestants, numJudges, stdDevLimit;
            fscanf(ifp, "%d%d%d", &numContestants, &numJudges, &stdDevLimit);
            
            int allScores[MAX_CONTESTANTS][MAX_JUDGES];
            
            // Read in all the scores given in this show.
            for (j=0; j<numContestants; j++)
                for (k=0; k<numJudges; k++)
                    fscanf(ifp, "%d", &allScores[j][k]);
            
            // Go through and calculate the average for each judge.
            double allAverages[MAX_JUDGES];
                    
            for (j=0; j<numJudges; j++) {
    
                // Add up each score this judge gave.
                int sum = 0, score;
                for (k=0; k<numContestants; k++)
                    sum += allScores[k][j];
                    
                allAverages[j] = (double)sum/numContestants;
                
                if (DEBUG)
                    printf("Judge %d avg = %lf\n", j+1, allAverages[j]);
            }
            
            // Now, go back and calculate the standard deviation of each judge.
            double allStdDev[MAX_JUDGES];
    
            for (j=0; j<numJudges; j++) {
    
                // Add up each term in the sum for the standard deviation.
                double sum = 0;;
                for (k=0; k<numContestants; k++)
                    sum += pow(allAverages[j] - allScores[k][j], 2);
                    
                // This is part of the formula. After this line, sum = variance.
                sum /= numContestants;
                
                // The square root of this is the standard deviation.
                allStdDev[j] = sqrt(sum);
                
            }
            
            // Calculate the number of cheating judges & store which ones are the
            // cheaters.
            int numCorruptJudges = 0;
            int badJudges[MAX_JUDGES];
            for (j=0; j<numJudges; j++) {   
                if (allStdDev[j] > stdDevLimit) {
                    badJudges[numCorruptJudges] = j;
                    numCorruptJudges++;
                }
            }
            
            // According to the rules, if all the judges are corrupt, none are.
            if (numCorruptJudges == numJudges)
                numCorruptJudges = 0;
                
            // Go through each contestant to find the best one.
            double bestScore = 0;
            int bestSinger = 0;
            for (j=0; j<numContestants; j++) {
                
                // Add up this contestant's VALID scores.
                double sum = 0;
                int judgeIndex = 0;
                for (k=0; k<numJudges; k++) {
                
                    // These are the requirements of a corrupt judge.
                    // This only works since the corrupt judges are stored in 
                    // numerical order.
                    if (judgeIndex < numCorruptJudges && badJudges[judgeIndex] == k) {
                        judgeIndex++;
                        continue;
                    }
                    
                    sum += allScores[j][k];
                }
                
                
                // Based on the specs, this will never divide by 0.
                double avg = sum/(numJudges - numCorruptJudges);
                
                if (avg > bestScore) {
                    bestScore = avg;    
                    bestSinger = j;
                }        
            } // end j loop for all contestant scoring.
            
            // First part of the output, add one because contestant numbers are one based.
            printf("UCF IDOL SHOW #%d\n", i);
            printf("Contestant #%d, you win with an average score of %.2lf.\n", bestSinger+1, bestScore);
            
            // If all our judges behave.
            if (numCorruptJudges == 0)
                printf("All judges' scores were used.\n\n");
                
            // This is if we have some bad ones.
            else {
                printf("The following judges' scores were thrown out: ");
                
                for (j=0; j<numCorruptJudges; j++)
                    printf("%d ", badJudges[j] + 1);    
                printf("\n\n");
            }
        } // End numCases loop.
        
        // Close the file.
        fclose(ifp);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int main() {
        //variable for file name
        char fileName[80];
        
        //ask for and read file name
        printf("What is the name of the input file with the UCF Idol data?\n");
        scanf("%s", fileName);
        
        FILE* ifp;
        ifp = fopen ("fileName", "r");
        ucfidol(ifp);
    Lose the "" around that, and you should be fine.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Opening File with function
    By bleuz in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2009, 10:33 PM
  2. opening a user selected file
    By SymDePro in forum C Programming
    Replies: 10
    Last Post: 07-14-2009, 01:35 PM
  3. No Matching Function Error When Opening File
    By zephyrcat in forum C++ Programming
    Replies: 19
    Last Post: 07-15-2008, 01:36 PM
  4. limiting a plugin function from opening a file
    By Jath in forum C Programming
    Replies: 3
    Last Post: 01-31-2008, 08:05 AM
  5. Opening a user-defined file
    By mr_diss in forum C Programming
    Replies: 8
    Last Post: 02-28-2005, 03:47 PM