Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int main(void)
{
FILE* input;
const char input_fn[]="proj3input.dat";
char buf[1025];
char *tokptr, *strptr = buf;
int col=0, line_num=0, win_min, win_max, win_size, i=0, j=0, x=0, record=0, curr_col, initial, tmp2, p, q;
double *tdata, *ydata, *tmean, *ymean, tmp, tsum, ysum;
input=fopen("proj3input.dat","r");
/*Verify file*/
if (input==(FILE*) NULL)
{
printf("Cannot open required file!\n");
printf("Please ensure your data file\n");
printf("is in the correct directory\n");
exit(EXIT_FAILURE);
}
else
/*Count number of lines*/
{
while((fgets(buf, 1024, input) != NULL))
{
line_num++;
}
printf("Number of lines is %d \n", line_num);
}
rewind(input);
fgets(buf, 1024, input);
while((tokptr=(char*)strtok(strptr, " \t")) != NULL)
{
strptr = NULL;
col++;
}
printf("Number of columns is %d \n", col);
rewind(input);
/* Find minimum and maximum sizes of window */
win_min = 10;
win_max = line_num;
/* Read user input for window size */
printf("Please enter your desired window size.\n");
printf("This should be between %d and %d.\n", win_min, win_max);
scanf("%s", buf);
win_size = atoi(buf);
printf("Your window size will be %d\n", win_size);
/* Allocate memory space */
tdata = (double*) malloc((line_num)*sizeof(double));
ydata = (double*) malloc((line_num)*sizeof(double));
tmean = (double*) malloc((win_size)*sizeof(double));
ymean = (double*) malloc((win_size)*sizeof(double));
printf("Allocated memory for %d records\n",line_num);
rewind(input);
while(fscanf(input, "%lf", &tmp) == 1)
{
curr_col = (record % col) + 1;
if(curr_col==1)
{
tdata[i] = tmp;
}
else
{
ydata[i] = tmp;
i++;
}
record++;
}
fclose(input);
for(x=9990; x<10000; x++)
{
printf("(%lf %lf)\n", tdata[x], ydata[x]);
}
i=0;
for(p=0; p<(win_size/line_num); p++)
{
for(i=win_size*p; i<((win_size*p)+win_size); i++)
{
tsum=0;
ysum=0;
tsum = tsum + tdata[i];
ysum = ysum + ydata[i];
tmean[q] = tsum/win_size;
ymean[q] = ysum/win_size;
}
}
printf("Tmean is %lf, Ymean is %lf\n", tmean[0], ymean[0]);
}
data file in 2 columns named proj3input.dat
Thanks