Thread: C program is not file-scanning correctly

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    51

    C program is not file-scanning correctly

    Hi all!

    First post here. I am having a problem with simple file scanning.

    Below is what is contained in the file
    Code:
    3 5
    84 24 25 25 2
    82 96 100 13 9
    23 45 77 23 12
    Here is what I have so far:
    Code:
    # include <stdio.h>
    
    int main()
    {
        // Open the input file.
        FILE *ifp = fopen("input.txt", "r");
    
        if(!ifp)
        {
            printf("Unable to open file\n");
            return -1;
        }
    
        int num_students, num_scores, i;
    
        fscanf(ifp, "%d", &num_students);
        fscanf(ifp, "%d", &num_scores);
    
        int min, max, j, current_score;
        float weighted_score;
        for (i = 0; i < num_students; i++)
        {
            int j, current_score, min=101, max=-1, sum=0;
            for (j = 0; j < num_scores; j++)
            {
                fscanf(ifp, "%d", &current_score);
                //printf("the score is: %d\n", current_score);
    
               if (current_score < min)
               {
                   min = current_score;
               }
               else if (current_score > max)
                {
                    max = current_score;
                }
                sum += current_score;
            //printf("The sum is: \n%d\n\n", sum);
          }
        //printf("The min score is:%d\nThe max score is:%d\n\n", min, max);
        weighted_score = (float)(sum-max-min)/ 3;
        printf("Student (number): Max Score:%d Min Score:%d Weighted Score:%.2f\n", max, min, weighted_score);
        //printf("Student 2: Max Score:%d Min Score:%d Weighted Score:%.2f\n", max, min);
        //printf("Student 3: Max Score:%d Min Score:%d Weighted Score:%.2f\n", max, min);
        }
    
        fclose(ifp);
        system("PAUSE");
        return 0;
    
    }
    In the first set of numbers (starting at 84 and ending at 2) for some reason it is not setting 84 as the maximum, instead it is setting 25 as the maximum. I tried to debug it to see if the program was skipping all first numbers altogether, but it wasn't, it just seems as if the program is skipping the first number, 84. Any idea why?

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    You've initialized min to 101 and max to -1; and just read current_score of 84
    Code:
               if (current_score < min)
               {
                   min = current_score;
               }
               else if (current_score > max)
                {
                    max = current_score;
                }
    Only the first if will be executed.
    After this block, min is 84 and max is -1.

    You need to change your code
    Last edited by qny; 11-01-2012 at 03:26 PM. Reason: typo

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Code:
    if (current_score < min)
    {
        min = current_score;
    }
    else if (current_score > max)
    {
        max = current_score;
    }
    Take a careful look there. The 'else' makes the check for max dependent on the check for min. It only checks if (current_score > max) when the first part is false, i.e. when current_score >= min. Remove the else.

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by C0__0D View Post
    Code:
        int min, max, j, current_score;
        float weighted_score;
        for (i = 0; i < num_students; i++)
        {
            int j, current_score, min=101, max=-1, sum=0;
    You also have two variables named max, two named min, two named j, and two named current_score. The min and max values that you are interested in are stored in the variables inside the outer for loop, and they go out of scope when the for loop ends.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Scanning Arrays from a file
    By Hikaru90 in forum C Programming
    Replies: 1
    Last Post: 02-10-2010, 09:06 PM
  2. OOP & File scanning.
    By epidemic in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2007, 02:46 PM
  3. file scanning?
    By yahn in forum C++ Programming
    Replies: 4
    Last Post: 12-03-2005, 10:27 AM
  4. Scanning integers from a file:
    By xarmenx in forum C Programming
    Replies: 6
    Last Post: 11-29-2004, 04:57 PM
  5. Text file scanning
    By spyderwb in forum C++ Programming
    Replies: 6
    Last Post: 03-08-2002, 03:31 PM