Thread: Doing calculations on a data file

  1. #1
    Registered User
    Join Date
    Mar 2009

    Doing calculations on a data file

    Ok new problem... The assignment I'm working on right now requires us to read in a file, make calculations on the file, and write the results to a new file. The file contains like 225 sets of data but we need to make the program so that you can select a value N so that it only calculates every Nth value. So lets say you pick 5, it would calculate every 5th value and print 45 values to the screen.

    I think I got the skipping values part down, I just manipulated my for loop variable in the arrays. The real question is, how can I keep a running total of my calculations. I'm calculating a displacement so you calculate first two points, get a displacement, calculate next two points, get a displacement, but that displacement has to be the total displacement so far.... Get what I'm saying?

    Here's some code.

    #define C_F 286.8
    #define MAX_VALUES 225
    #define FILENAME "pixel_load.txt"
    //Start of main function
    int main(void)
    	FILE *pixel_load = NULL;
    	int i, j, N;
    	double displacement_pixels[MAX_VALUES], displacement_inches[MAX_VALUES], load[MAX_VALUES]; 
    	double num_measurements, amount_of_points;
    	//Header of program and user input for number of data points
    	printf("                    LOAD AND DEFLECTION \n");
    	printf("------------------------------------------------------------ \n\n");
    	printf("Only one out of every n data point will be recorded in the output. \n");
    	printf("What value of N would you like: ");
    	scanf("%i", &N);
    	//Open file
    	pixel_load = fopen(FILENAME, "r");
    	//Check to see if file opens
    	if(pixel_load == NULL)
    		printf("Could not open data file\n");
    		return -1;
    	//Read in number of measurements
        fscanf(pixel_load, "%lf", &num_measurements);
    	 //Calculate number of data points
    	amount_of_points = num_measurements / N;
    	//Display number of points recorded
    	printf("\n\nRecording 1 out of every %i data samples.", N);
    	printf(" There will be %4.0f samples in \nthe output file. \n\n", amount_of_points);
    	//Headings for output
    	printf("				DISPLACEMENT[in]				LOAD[LBS] \n");
    	//For loop to read values 
    	for( i = 0; i < num_measurements; i++)
    		fscanf(pixel_load, "%lf %lf", &displacement_pixels[i], &load[i] );
    	//Print zero deflection
    	printf("                  0.000\n");
    	//For loop to calculate  
    	for( j = 0; j < amount_of_points; j++)
    		displacement_inches[j] = (displacement_pixels[(N * j) + 1] - displacement_pixels[N * j]) / C_F;
    		printf("                 %6.3lf 					%7.3lf\n", displacement_inches[j], load[(N * j)]);
    	//Calculations to convert pixels to inches
    	//displacement_inches[j] = (displacement_pixels[j] - displacement_pixels[j]) * C_F;
    	//printf("				   %6.3lf                     %7.3lf\n", displacement_inches[j], load[j]);
        //End the program
    	printf("Press ENTER to continue... ");
        return 0;

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    segmentation fault
    Use a global variable.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Why a global? First off, you're doing all of this inside of main, so you could just as easily make this a local variable.

    If you want to figure this out without having to wade through a file first, do a simple version to get the logic down:
    prompt for number
    read it into number
    for( x = starting point; x < ending point; x += number )
         do whatever
    Just use an array, throw in a handful of test variables, and use the above loop. Once you have that concept working, apply it to your file. Replace 'do whatever' it jumping number records at a time.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading data file and calculations
    By testingcw in forum C++ Programming
    Replies: 2
    Last Post: 08-04-2008, 08:31 AM
  2. Replies: 3
    Last Post: 02-26-2008, 02:12 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  5. File Database & Data Structure :: C++
    By kuphryn in forum C++ Programming
    Replies: 0
    Last Post: 02-24-2002, 11:47 AM