Thread: Reading Floats from a File

  1. #16
    Registered User
    Join Date
    Nov 2017
    Posts
    29
    Ok I got a little guidance from the TA in the class so it's better. But when I run it doesn't do anything. I honestly have no clue how to make this thing work and it's really driving my anxiety up the wall. See below for what I have. It was the TA's suggestion to use a for loop to get the max and min. I have to add range and standard deviation to the mess for part B of the assignment THEN write a linear regression program after this doing all of this same garble. Needless to say, I'm not very excited. I need like a legit tutor to show me how to apply the code, not just how to write it.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    #define NMAX 300
    int main()
    {
        int data[NMAX], N=0;    //array of data
        float min=9999999.0, max=0.0;     //max and min of data set
        float sum = 0.0, avg, std_dev, range;     //average, standard deviation, range of data set
        FILE *fpin;
    
    
        if((fpin = fopen("data.txt","r"))==(FILE*)NULL)
        {
            printf("File does not exist\n"); exit(-1);
        }
    
    
        for(N=0; fscanf(fpin, "%d", &data[N]) != EOF; N++);
        {
            for (N=0; data[N]>max; N++)
            {
               max = data[N];
            }
    
    
            for (N=0; data[N]<min; N++)
            {
               min = data[N];
            }
    
    
            for (N=0; data[N]; N++)
            {
               sum += data[N];
               avg = sum / N;
            }
        }
        fclose(fpin); // Close File after Reading
        printf("The minimum value of this data set is %f.\n", min);
        printf("The maximum value of this data set is %f.\n", max);
        printf("The average value of this data set is %f.\n", avg);
    
    
        return 0;
    }

  2. #17
    Registered User
    Join Date
    Jun 2017
    Posts
    88
    Sorry to say this for all the work you have done, but I think if you already have the data contained within a file, you don't need an array to hold it as well. If I understand you correctly, all you need to do is:

    1. open a file in read mode and start at the top of the file.
    2. start a counter to count how many numbers you have read
    3. start a loop to read in one number from the file at a time.
    4. compare number to current min and max and update them
    5. add number to sum
    6. increment number count
    7. end loop
    8. calculate average by sum / count
    9. print min, max, and average.

    Here is a comment arrangement that shows this better:
    Code:
    int main() {
        // open file
     
        // initialize count, min, max, sum
     
        // start loop with a read statement.  read until all values are read. {
     
            // compare current number with min max and update
     
            // add current number to sum.
     
            // increment count + 1
     
        // end loop }
     
        // initialize and calculate average
     
        // print min, max, average
    }
    I am also sorry to disagree with your TA, but when something gets very difficult, I always go with while loops over for loops. Even my suggestions to have a read operation inside the condition might be a bad idea, but it is something you are already doing.
    Last edited by jack jordan; 11-06-2017 at 09:07 PM.

  3. #18
    Banned
    Join Date
    Aug 2017
    Posts
    861
    then go read post #4 and post #6 again.

    If your actually study it you should just discover some very useful information.
    and
    I second that motion on while loops vs for loop usage.

    for loops do have a purpose, but while loops also have their purpose. it is knowing which one to use when that is the "trick."

    Code:
      for(N=0; fscanf(fpin, "%d", &data[N]) != EOF, N<3; N++);
    
    // if your TA clued you into to do that code
    // in your for loop he or she is wrong.
    // plus you're reading in float or double, not int 
    //adding the while N < 3 // because that is what the code is saying 
    //gets me this
    
    
    userx@slackwhere:~/bin
    $ ./term2
    The minimum value of this data set is 0.000000.
    The maximum value of this data set is 3.000000.
    The average value of this data set is inf.
    yes I still have your data file,
    Last edited by userxbw; 11-06-2017 at 09:37 PM.

  4. #19
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by scotdani View Post
    I loaded your example here into my compiler, CodeBlocks 16.01, and it doesn't like the last for loop you wrote, line 181. Reading through it line by line I just got confused because I don't have a good foundation on loops. While and Do While I am ok with, but For loops are completely foreign to me. Honestly I learn better by watching and doing stuff, definitely not by reading.
    I just found this, are you talking about this line? that is line 181 in my editor.
    Code:
      for ( int i = 0; i<MAX_SZ; i++) data1[i] = 0.0;
    if yes then just change it to this
    Code:
    int i;
      for (  i = 0; i<MAX_SZ; i++) data1[i] = 0.0;
    because not all compilers like that declaration of the int inside of the for loop,
    if it still does not like it then do this
    Code:
      for (  i = 0; i<MAX_SZ; i++) 
             data1[i] = 0.0;
    if it still does not like it, then stop using code blocks because that is a valid line of code.

    what the for loop is basically saying is this

    for ( i equals zero ; while i is less than < 10 ; keep count of how many times I loop around what is after this line)
    line to keep track of;
    Last edited by userxbw; 11-06-2017 at 09:54 PM.

  5. #20
    Registered User
    Join Date
    Nov 2017
    Posts
    29
    I finally had success in getting this program to function. This stuff is not intuitive to me at all so little things were causing it to fail, such as having the array declaration stand alone from other variables.

    Say what you will about my study style, but some of the examples you guys provided are just noise if I don't know what I am looking for. I have trouble reading it in code format so I've found plain words to be better to absorb. This has been a whole learning experience other than learning computer coding.

    I only use CodeBlocks because it's what the university uses, and my first experience with coding altogether.

  6. #21
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by scotdani View Post
    I finally had success in getting this program to function. This stuff is not intuitive to me at all so little things were causing it to fail, such as having the array declaration stand alone from other variables.

    Say what you will about my study style, but some of the examples you guys provided are just noise if I don't know what I am looking for. I have trouble reading it in code format so I've found plain words to be better to absorb. This has been a whole learning experience other than learning computer coding.

    I only use CodeBlocks because it's what the university uses, and my first experience with coding altogether.
    you're doing fine no need to feel you need to justify yourself.
    You might just need to learn not to take negative criticism to harshly, it is hard to discern the manner people are talking when you're just reading it in a post.

    The more you work the easier it should get. Practice makes perfect.

    You might just need to take smaller bytes and work with them until you get a better understanding of the are doing and how they are doing it.

    Try using printf a lot to print out the values of your data you're working with to see if you're getting it on both sides of the operation, and if any changes where made its value. you should know if they is to be any or not. and what it should be, or close to it to find out if your code is working.

    Putting process of elimination into practice helps a lot in everything you do.
    Last edited by userxbw; 11-08-2017 at 08:41 AM.

  7. #22
    Registered User
    Join Date
    Nov 2017
    Posts
    6
    There are several errors in your program - mismatched braces, undefined variables, etc. The most important, however, and the one most likely to be causing your problem, is that you're not passing a pointer to myvariable in your fscanf() call. You'll want to use &myvariable there, so that fscanf()[COLOR=#242729][FONT=Arial] can fill it in appropriately.
    Last edited by Salem; 11-12-2017 at 02:17 AM. Reason: spammy links deleted

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can't read floats into 2D array from file
    By std10093 in forum C++ Programming
    Replies: 3
    Last Post: 04-12-2014, 09:36 AM
  2. Newbie at C, about reading floats into an array
    By someoney3000 in forum C Programming
    Replies: 9
    Last Post: 10-14-2009, 12:40 PM
  3. Reading errors in floats
    By Improvolone in forum C++ Programming
    Replies: 8
    Last Post: 03-21-2006, 03:20 PM
  4. reading int's and floats...
    By flightsimdude in forum C Programming
    Replies: 8
    Last Post: 09-20-2003, 01:17 PM

Tags for this Thread