# Doing calculations on a data file

• 03-30-2009
bassist11
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.

Code:

```#define C_F 286.8 #define MAX_VALUES 225 #define FILENAME "pixel_load.txt" //Start of main function int main(void) {         //Declarations         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;         floor(amount_of_points);                 //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... ");     getchar();     return 0;  }```
Thanks!
• 03-30-2009
MK27
Use a global variable.
• 03-30-2009
quzah
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:
Code:

```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.