Thread: Finding the min and max of a file of integers

  1. #1
    Registered User
    Join Date
    Jun 2016
    Posts
    16

    Question Finding the min and max of a file of integers

    Can someone please give me a hint as to what is wrong with my code? I cannot find the correct min and max of a file of integers.

    insert
    Code:
    #include<stdio.h>
    main()
    {
        int count = 0, sum = 0, num, min = 0, max = 0, avg;
        FILE *fin, *fout;
        
        fin = fopen("in.txt", "r");
        fout = fopen ( "out.txt", "w");
        
        while ( fscanf( fin, "%d", &num) != EOF){
        count = count + 1;
        sum = sum + num;
            if (num>=max)
                 max=num;
            else if (num<min)
                 min=num;    
        }
        avg = sum/count;
        fprintf(fout, "The number of integer values in the input file = %d\n", count);
        fprintf(fout, "The minimum integer value in the input file = %d\n", min);
        fprintf(fout, "The maximum integer value in the input file = %d\n", max);
        fprintf(fout, "The average of the integer values in the input file = %d\n", avg);    
        fclose(fin);
        fclose(fout);
    }
    Last edited by SauceGod; 06-22-2016 at 06:08 PM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Pretty sure your assignments are backwards.
    max = num;
    min = num;
    Not vice versa.

    Edit: So that advice didn't fix it? What do you see?
    Last edited by whiteflags; 06-22-2016 at 06:11 PM.

  3. #3
    Registered User
    Join Date
    Jun 2016
    Posts
    16
    Ok. I updated the code and ran and compiled it. I still get the wrong answers. Thanks for pointing that out.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    There is a chance that the real minimum and maximum is smaller or larger than 0. To protect yourself from that issue you can do a priming read and set both min and max to the first number:
    Code:
        if (fscanf (fin, "%d", &max) == 1) { 
           min = max;
           rewind(fin);
        }
        while ( fscanf( fin, "%d", &num) != EOF){
        count = count + 1;
        sum = sum + num;
            if (num>=max)
                 max=num;
            else if (num<min)
                 min=num;    
        }
    Last edited by whiteflags; 06-22-2016 at 06:53 PM.

  5. #5
    Registered User taazz's Avatar
    Join Date
    May 2016
    Posts
    50
    I did not understand whiteflags first suggestion and the second is a bit wasteful for my liking (if I understand it correctly). I would not initialize min to 0 the proper value should be maxInt where maxInt = (2^32 ) -1 if int is 32bits. Similarly I would initialize max to -1*(2^32), although 0 usually works, it depends on the data really. If none of our comments helps then I would appreciate either a sample file or the border numbers in the file ee the smaller and bigger numbers in the file.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by taazz View Post
    I did not understand whiteflags first suggestion
    He was assigning max and min to num in the original post, which he had time to edit and incorporate what I pointed out. It's not my fault if he somehow managed to make me look stupid.

  7. #7
    Registered User taazz's Avatar
    Join Date
    May 2016
    Posts
    50
    Quote Originally Posted by whiteflags View Post
    He was assigning max and min to num in the original post,
    so your first comment has nothing to do with what I see now in the first post. For what I see now my assumption is that num does not get any initialization values it should contain what ever is on the memory aka garbage. Am I correct?
    Quote Originally Posted by whiteflags View Post
    which he had time to edit and incorporate what I pointed out.
    That is why I always quote what I'm answering to.

    EDIT:
    By the way loose the else in your comparison eg
    Code:
         if (num>=max)
    
              max=num;
    
         if (num<min)
    
              min=num;
    otherwise some number might never make it to the min comparison.
    Last edited by taazz; 06-23-2016 at 05:55 AM.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    otherwise some number might never make it to the min comparison.
    This is why I initialized the min and the max to the first number in the sequence. It becomes a strict weak ordering problem. If you look over the rules, you will see there is no number that can be less than the current min and greater than the current max at the same time. If there were some number, it would mean that you can't put the set in ascending order.

    This is why selection sort actually works.
    Last edited by whiteflags; 06-23-2016 at 04:04 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding MAX and MIN of integers
    By David_24 in forum C Programming
    Replies: 15
    Last Post: 11-18-2013, 10:29 AM
  2. Replies: 3
    Last Post: 02-23-2013, 11:55 PM
  3. Finding Pos/Neg Integers Unsigned Can Hold
    By trial123 in forum C Programming
    Replies: 5
    Last Post: 01-26-2013, 12:15 AM
  4. Replies: 2
    Last Post: 05-28-2009, 09:58 PM
  5. File i/o... integers
    By Gnoober in forum C++ Programming
    Replies: 2
    Last Post: 02-14-2003, 03:36 PM

Tags for this Thread