Reading in a file and printing out a graph from the data

This is a discussion on Reading in a file and printing out a graph from the data within the C Programming forums, part of the General Programming Boards category; I'm provided with a file full of data on a car trip, here's an example: Code: 10 13 1.131232 0.000183 ...

  1. #1
    Registered User levitylek's Avatar
    Join Date
    Sep 2010
    Location
    Orlando
    Posts
    19

    Reading in a file and printing out a graph from the data

    I'm provided with a file full of data on a car trip, here's an example:

    Code:
    10 13
    1.131232 0.000183
    2.872323 0.000312
    4.233291 0.000422
    5.823823 0.000367
    6.464139 0.000316
    6.554324 0.000275
    6.001023 0.000245
    5.293232 0.0
    7.012323 0.000612
    7.013323 0.000583
    The 10 is the number of minutes the trip lasted (it will always be a multiple of 5 and less than 30), and the 13 is the radius of the tires. The following numbers on the left represent the revolutions the tires made in a given second, and the right numbers are the gas in gallons used in that second.

    Basically we have to ask the user for the file the data is storred in and compile a graph from it like so:

    Code:
    What file stores the car data?
    sample.txt
    
    95                                      
    90                                      
    85                                      
    80                                      
    75                                      
    70                                      
    65                                      
    60                                      
    55                                      
    50                                      
    45                                      
    40                                      
    35                                      
    30        *****                         
    25        ***** *****                   
    20        ***** *****                   
    15        ***** *****                   
    10  ***** ***** *****                   
    5   ***** ***** *****                   
    0   ***** ***** *****                   
        -----------------------------------
        00-05 05-10 10-15 15-20 20-25 25-30
    To be honest I'm a bit behind and I'm still having trouble printing out graphs and reading in files. This is what i've got so far:

    Code:
    #include <stdio.h>
    
    int main() {
    
    	FILE *ifp;
    	int file, rad, mins;
    	double revs, gas;
    
    	printf("What file stores the car data?\n");
    	scanf("%d", &file);
    	
    	//Open file
    	ifp = fopen(file, "r");
    	fscanf(ifp, "%d%d", &mins, &rad);
    We also have to include a function after the main function and I really lack that knowledge. I'd appreciate it if anyone could get me a direction to go in...

  2. #2
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    I'm a little confused with what you want to graph? All you said was:
    Basically we have to ask the user for the file the data is storred in and compile a graph from it like so:
    And then you have an example graph but don't explain what is represented by each axis. Is the x axis trip duration? Or is it revolutions per second for a given trip length? Is the y axis amount of petrol used?

    Btw, am I the only one thinking that gallons is a bit of a silly unit of volume to measure 'amount of petrol consumed per revolution'? Wouldn't it make more sense to use a smaller unit of volume for this problem. Oh well, never mind.

    You have made a start with this problem but your code suddenly stops when it extracts the trip duration and the radius of the wheels.

    You could then try doing something like this:
    Code:
    do
    {
          ret = fscanf(ifp, "%f %f", &value1, &value2);
          /* Maybe store value1 and value2 into an array for later processing */
    } while( ret != EOF );
    Until I know more about the graph you are trying to print (i.e. which variable against which variable), it is hard to give you much more advice.

    Edit:

    In a given sample file will there be more than one data set?
    Last edited by Swarvy; 10-26-2010 at 06:19 PM.

  3. #3
    Registered User levitylek's Avatar
    Join Date
    Sep 2010
    Location
    Orlando
    Posts
    19
    The x axis is the five minute intervals based on the amount of time the trip lasts, and the y axis is the mpg you get for that interval.

    Here's a link to a test file:
    https://webcourses.ucf.edu/webct/urw.../7724986325101

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You must have a classmate who posted here:
    Bar graph troubles

    Since he never got around to stating fully what the data was, I can't say if his math was correct, or not.

    In any case, I would change your FILE *ifp to the more conventional *fp. if(p) is a common syntax in C, and having a file pointer that starts with an i is just confusing. Must have been in the prof's notes, because he had the same odd name for it.

    Drawing the graph is no problem. Fill the array with your calculated data, and it's the logic shown in that thread, is (maybe), good to go. I don't want to say that it's been tested, because it has not been, AFAIK.

    Code:
    Date from milesPG[]:
     10.033465
     32.441399
     28.829947
    ===================
    
     45|                               
     40|                               
     35|                               
     30|       *****                   
     25|       ***** *****             
     20|       ***** *****             
     15|       ***** *****             
     10| ***** ***** *****             
      5| ***** ***** *****             
      0| ***** ***** *****             
         -----------------------------------
         00-05 05-10 10-15 15-20 20-25 25-30
    Press any key to continue . . .
    He limited his scale to 45, but there's no reason not to extend it higher, in your version.

    Looking at your code:
    Code:
    #include <stdio.h>
    
    int main() {
    
    	FILE *ifp;
    	int file, rad, mins;
    	double revs, gas;
    
    	printf("What file stores the car data?\n");
    	scanf("%d", &file);
    	
    	//Open file
    	ifp = fopen(file, "r");
    	fscanf(ifp, "%d%d", &mins, &rad);
    
    }
    I would: delete "int file", that should be your char array for the filename.
    add: char file[40]; double alltheBloodyGasUsedSoFar; int interval;
    Code:
    while("endless loop here") {
      n=fscanf(fp, "%f", "%f", interval, gas);
      if(n>1) {
        alltheBloodyGasUsedSoFar += gas;
        //do your other calculations here
    
    
      }
      else  //end of file has been reached, n=EOF(usually -1)
         break;
    }
    fclose(fp); just above the last getchar() of pause line.
    getchar() or system("pause"); to just before the,
    return 0; at the very end of main.

    check if the file was opened OK or not (is fp==NULL). If so, print an error message and return 1, ending the program.

    change: scanf("%d", &file); to scanf("%s", file); //(no ampersand)
    ifp to fp as mentioned earlier

    That should get you going nicely.

    Be careful that this is your own work - simple copying with a tweak here or there would be plagerism.
    Last edited by Adak; 10-26-2010 at 08:34 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 11:04 AM
  2. Reading from file and printing double data
    By hammari in forum C Programming
    Replies: 4
    Last Post: 07-14-2009, 08:02 AM
  3. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 06:26 PM
  4. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  5. problems reading data into an array and printing output
    By serino78 in forum C Programming
    Replies: 4
    Last Post: 04-28-2003, 09:39 AM

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