c programming problems. reading data file and finding max

This is a discussion on c programming problems. reading data file and finding max within the C Programming forums, part of the General Programming Boards category; I have a program I have to write that reads a data file. The data file has four columns(time, altitude, ...

  1. #1
    Registered User
    Join Date
    Feb 2013

    c programming problems. reading data file and finding max

    I have a program I have to write that reads a data file. The data file has four columns(time, altitude, velocity, acceleration). I am supposed to read this data file and find out when the rocket is falling back to Earth. I would do this by finding when the altitude starts to drop off. When the altitude drops off I am supposed to print the time that it starts to drop off to the screen.

    I got a program together but it is not printing off the time that is corresponding to the time the altitude starts to drop off.
    Here is the program:
    #include <stdio.h>
    #define FILENAME "rocket11.txt"
    int main(void)
    int max_found=0, k, lines;
    double time1, time2, time3, alt1, alt2, alt3, vel, acc;
    FILE * data;
    /* Open file and read the number of data points.  */
    data = fopen(FILENAME,"r");
    if (data == NULL)
      printf("Error opening input file.  \n");
    fscanf(data, "%d", &lines);
    /*  Read data and compute summary information.  */
    for (k=1; k<=lines; k++);
    fscanf(data, "%lf %lf %lf %lf",&time1, &alt1, &vel, &acc);
    fscanf(data, "%lf %lf %lf %lf",&time2, &alt2, &vel, &acc);
    fscanf(data, "%lf %lf %lf %lf",&time3, &alt3, &vel, &acc);
            printf("Rocket is returning to Earth at %lf (s)",time2);
    return 0;
    Last edited by drewdude92; 02-21-2013 at 08:08 PM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Long Beach, CA
    Welcome drewdude92!

    Fix your indentation in future posts. Posting hard to read code like that is a good way to not get help. You get a free pass since this is your first time

    Why are you reading 3 lines each time through the loop? Once is enough. Also, you will need to check for "dropping off" inside the loop, so you can check every data point. If you wait until the whole file is read, you missed 99% of the data points. You only need to know the last set of data and the current one, to see if the altitude drops. Thus, you need time, alt, vel, acc as well as prev_time, prev_alt, prev_vel and prev_acc. Read your first set of data. Then, every time through the loop, set the prev_ values equal to the current values, then read the next line in as the new current values. Then check for dropping off

    Your "dropping off" check
    Is all kinds of broken. First, ditch that semicolon, it make the body of the if statement an empty statement, so the printf below will always execute no matter what. Second, you can't compare values like that in C. The correct way to do that would be
    if (alt1 < alt2 && alt2 > alt3)
    However, if you are only using alt and prev_alt, then it's just one simple comparison.

    Lastly, you should really check the return value of your fscanf calls, make sure they succeeded. fscanf returns the number of items scanned. If you ask for 1 (line reading num lines), it returns 1 on success, if you ask for 4 (like reading time, alt, vel and acc), it should return 4. Anything else, and you have a problem. So something like:
    if (fscanf(data, "%lf %lf %lf %lf",&time3, &alt3, &vel, &acc) != 4)
        print error
        exit program

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Reading/Writing Problems [Socket Programming]
    By MaSSaSLaYeR in forum C Programming
    Replies: 5
    Last Post: 11-17-2011, 10:39 AM
  2. Finding and Replacing Data in a File
    By DrC in forum C Programming
    Replies: 6
    Last Post: 03-20-2011, 10:09 AM
  3. Reading data file problems!
    By noodle24 in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2006, 10:13 AM
  4. Replies: 2
    Last Post: 06-16-2005, 10:03 AM
  5. problems reading data into an array and printing output
    By serino78 in forum C Programming
    Replies: 4
    Last Post: 04-28-2003, 08:39 AM

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