# Thread: Doing calculations on a data file

1. ## 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

//Start of main function
int main(void)
{
//Declarations
int i, j, N;
double num_measurements, amount_of_points;

//Header of program and user input for number of data points
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

//Check to see if file opens
{
printf("Could not open data file\n");
return -1;
}

//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);

for( i = 0; i < num_measurements; 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;

//End the program
printf("Press ENTER to continue... ");
getchar();
return 0;
}```
Thanks!

2. Use a global variable.

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