In this code, the integer "m" is supposed to be counting the number of times the program encounters a value less than zero (I'm measuring rainfall; -999.99 is "missing data"), while "n" is supposed to be counting the number of data points the program is generating. We need this output so that we can gauge the amount of error that having to skip over missing data points incurs, but "m" is only ever giving me 0, which I know is not true.
Can anyone spot my problem, please? I need a fresh set of eyes.
Code:
/*********************************************************************
* This program is designed to calculate a series of autocorrelations *
* based on a set of data that is called when the program is run. *
* The set of data needs to be in "float" format (i.e., nothing but *
* numbers), or else the program will generate an error. The *
* resulting file will also be a series of numbers, meant to go into *
* some kind of graphing program to plot the original data against *
* the values of the autocorrelations for each point. *
*********************************************************************/
/* Autocorrelation Program, Written By Don Ford */
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
int main(int argc, char *argv[]){
FILE* dataFile;
FILE* outputFile;
char string[80];
char *old_filename;
char new_filename[256];
double x[1000];
int i = 0;
int j;
int number_of_values;
double x_sum = 0.0;
double x_average;
int k;
int t;
double sum_of_squares = 0.0;
double numerator = 0.0;
char buffer[80];
int m;
int n;
for (j=1; j<argc; j++){
i = 0;
m = 0;
n = 0;
sum_of_squares = 0.0;
numerator = 0.0;
x_sum = 0.0;
old_filename = argv[j];
dataFile = fopen(old_filename, "r");
if (dataFile == NULL){
fputs("File error", stderr);
exit(1);
}
printf("Autocorrelating \"%s\" ...\n", old_filename);
while (fgets(string, 80, dataFile) != NULL){
if (!isalpha(string[0])){
x[i] = strtof(string, NULL);
if (x[i] >= 0){
i++;
}
}
}
fclose(dataFile);
number_of_values = i;
double values = number_of_values;
double y[number_of_values];
for (i=0; i<number_of_values; i++){
if (x[i] >= 0){
x_sum += x[i];
}
}
x_average = (x_sum/values);
/* Now, we actually calculate the autocorrelation! */
for (t=0; t<number_of_values; t++){
if (x[t] >= 0){
sum_of_squares += ((x[t] - x_average)*(x[t] - x_average));
}
}
for (k=0; k<(number_of_values/2); k++){
for (t=0; t<(number_of_values - k); t++){
if ((x[(t+k)] >= 0) && (x[t] >= 0)){
numerator += ((x[(t+k)] - x_average)*(x[t] - x_average));
}
else
m++;
n++;
}
y[k] = (numerator/sum_of_squares);
numerator = 0.0;
}
for (i=0; i<strlen(old_filename), old_filename[i] != '.'; i++){
new_filename[i] = old_filename[i];
}
new_filename[i] = '-';
new_filename[i+1] = 'A';
new_filename[i+2] = 'U';
new_filename[i+3] = 'T';
new_filename[i+4] = 'O';
new_filename[i+5] = '.';
new_filename[i+6] = 't';
new_filename[i+7] = 'x';
new_filename[i+8] = 't';
new_filename[i+9] = '\0';
outputFile = fopen(new_filename, "wb");
if (outputFile == NULL){
fputs("File error", stderr);
exit(2);
}
snprintf(buffer, 80, "%d entries skipped out of a total of %d\0",m,n);
fputs(buffer, outputFile);
fputs("\n", outputFile);
snprintf(buffer, 80, "Lag Autocorrelation\0");
fputs(buffer, outputFile);
fputs("\n", outputFile);
for (i=0; i<(number_of_values/2); i++){
snprintf(buffer, 80, "%3d %.8f\0", i, y[i]);
fputs(buffer, outputFile);
fputs("\n", outputFile);
}
fclose(outputFile);
printf("File created: \"%s\"\n", new_filename);
}
return 0;
}