"C" finding MIN and MAX from .txt file?

This is a discussion on "C" finding MIN and MAX from .txt file? within the C Programming forums, part of the General Programming Boards category; Hey guys, I just got assigned a project in my C programming class. The objective is to find the min ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Location
    Miami, FL
    Posts
    3

    Lightbulb "C" finding MIN and MAX from .txt file?

    Hey guys,

    I just got assigned a project in my C programming class. The objective is to find the min and max number out of a .txt file full of numbers on separate lines called "data.txt". I am pretty much stuck at this point. I don't know how to actually scan the file and print the min and max. If anyone could lend a helping hand, I would really appreciate it!
    Also, the list of numbers in the data.txt file contains hundred on hundreds of numbers. So I couldn't specify the exact amount of numbers listed in the file. I must also find the count number, sum, and the average of the given data, thanks.


    So far I have:

    Code:
    #include<stdio.h>
    
    
    int main()
    {
           int counter=0;
           int maximum=0, minimum=1;
           int num_data_points, avg;
           double max, min;
    
        FILE *sensor;
        sensor = fopen("data.txt", "r");
           if (sensor == NULL)
        {
           printf("Unable to read/write file.\n");
    }
          else
        {
          fscanf(sensor, "%d", &num_data_points);
    
             for(counter=0; counter<num_data_points; counter++)
       {
         fscanf(sensor, "%lf", max);
         fscanf(sensor, "%lf", min);
       }
           printf("Maximum = %lf\nMinimum = %lf\n", max, min);
        }
    
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,745
    If your data file has one number on each line why are you reading the file three different times? This sounds like you should be using a while loop to read each line, determine if the value is the highest or lowest, add the value to your sum, increment your counter and continue until you read all the lines of the file. Then after you read the information you can use the sum of the values and the counter to determine the average.

    Jim

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    333
    You need to nail down the format of the input file.

    Presumably it's line-oriented, with a variable number of numbers on each line. Are they all integers? How are they separated? If they are comma-separated, is there a trailing comma after the last one on a line?

    Somehow you've got implement readnextnumber() on top of that file. You might need to store state between calls, e.g .the line you are on, and an index into the line. Or you might be able to do everything with fscanf(). It doesn't really matter. The important thing is to have a stream of numbers, with some flag to tell you when you have the last.

    Then finding the minimum and maximum is trivial.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    http://www.malcolmmclean.site11.com/www

  4. #4
    Registered User
    Join Date
    Mar 2013
    Location
    Miami, FL
    Posts
    3
    Here's just a snippet of the file:
    "
    1680815250
    10074
    3659
    8931
    11273
    7545
    10879
    17924
    17710
    4441
    18166
    4493
    3043
    7988
    2504
    2328
    "

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    333
    Quote Originally Posted by Joey Labelle View Post
    Here's just a snippet of the file:
    So it's one number per line, all integers, no delimiters.

    So that makes things easier. Read in a line at a time using fgets().

    But those numbers can get pretty big. If you use strtol() with the "end" parameter set, it will tell you if a number is too big to parse. If that's the case then you need to do something a bit more complicated (print out an error and stop the program for the first attempt).
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    http://www.malcolmmclean.site11.com/www

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Joey Labelle View Post
    Hey guys,

    I just got assigned a project in my C programming class. The objective is to find the min and max number out of a .txt file full of numbers on separate lines called "data.txt". I am pretty much stuck at this point. I don't know how to actually scan the file and print the min and max. If anyone could lend a helping hand, I would really appreciate it!
    Also, the list of numbers in the data.txt file contains hundred on hundreds of numbers. So I couldn't specify the exact amount of numbers listed in the file. I must also find the count number, sum, and the average of the given data, thanks.

    Use unsigned long long int variable for all your ints, because some of those numbers may exceed the range of regular ints.

    min and max:

    Assign both to the first value in the file, after the value read, is turned from a char digit say "22" into a number - 22.

    As each value is read from the file, compare it with these two values:

    if(newValue>max) then max equals newValue

    if(newValue<min) then min equals newValue

    sum:
    set sum to zero before anything else then sum+=newValue

    scan the file:
    the file appears perfectly formatted. I prefer fgets() for this, but fscanf() is OK today.

    Code:
    assign initial values before the loop starts:
    sum equals zero, count equals zero, etc.
    while((fscanf(filePointerName,"%llu",newValue))>0) {
    
       if(count==0)  {   
            min equals newValue
            max equals newValue
        }else {
             //use min max logic mentioned above
        }
        
        increment count, sum, etc.
    }
    
    average equals sum divided by count
    count:
    as shown above

    Work with the above pseudo code and ideas, and see what you can do with it. It should be productive.
    Last edited by Adak; 03-16-2013 at 02:58 PM.

  7. #7
    Registered User
    Join Date
    Mar 2013
    Location
    Miami, FL
    Posts
    3
    This is what I've adjusted it to, except the final specification of the assignment is that I must incorporate the implementation and function call of the function MAX and the function MIN. I'm having trouble doing so, if anyone could lend a helping hand I would greatly appreciate it.

    Code:
    #include <stdio.h>
    int main()
    {
        FILE *sensor;
        int num, min, max, sum, count, first;
            sensor = fopen("C:\\Users\\joseph.labelle\\Documents\\data.txt", "r");
            if (sensor != NULL)
            {
                for (sum = count = first = 0;
                fscanf(sensor, "%d", &num) == 1; sum += num, ++count)
                    if (!first)
                    {
                        min = max = num; first = 1;
                    }
                    else if (num > max) max = num;
                    else if (num < min) min = num;
                fclose(sensor);
                printf("count = %d, min = %d, max = %d, sum = %d, avg = %.1lf\n",
                            count, min, max, sum, sum / (double) count);
            }
            else
            {
                printf("Unable to read file 'C:\\Users\\joseph.labelle\\Documents\\data.txt'.\n" );
            }
        return 0;
    }
    Thanks for all your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-19-2012, 06:15 AM
  2. Replies: 5
    Last Post: 01-18-2006, 10:02 PM
  3. Need help finding "string"
    By KingDubya in forum C++ Programming
    Replies: 0
    Last Post: 12-01-2005, 07:11 PM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 07:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 06:59 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21