#include #include #define MAXDATA 1000 float cor(float[], float[], int); int main() { int numdata; float x[MAXDATA]; float y[MAXDATA]; float correlation; /* (1) The first line of the file 'test.dat' consists of a single */ /* integer indicating the number of lines of data that follow. */ /* Read the integer into the variable 'numdata'. If the value */ /* is greater than MAXDATA, print an error message and return. */ /* (2) The 'numdata' lines in 'test.dat' consist of two floating- */ /* point number separated by a comma. For each line, read the */ /* first number into the array x[] and the second number into */ /* the array y[]. Close the file after you have read all of */ /* the data. */ /* Call the correlation() function - DON'T CHANGE THIS */ correlation = cor(x, y, numdata); /* (3) Open the file 'test.out' for writing. Write the following */ /* message to the file: */ /* "The correlation is ." */ /* ^^^^^^ */ /* replace with value of correlation */ /* Close the file after you have written the output. */ return 0; } /* cor() - compute the correlation of the data in x[] and y[] */ /* x[] - first data array */ /* y[] - second data array */ /* len - length of x[] and y[] */ float cor(float x[], float y[], int len) { float sumx = 0.0; float sumy = 0.0; float sumx2 = 0.0; float sumy2 = 0.0; float sumxy = 0.0; float ans, num, den; int i; for (i = 0; i < len; i++) { sumx += x[i]; sumy += y[i]; sumx2 += (x[i] * x[i]); sumy2 += (y[i] * y[i]); sumxy += (x[i] * y[i]); } num = ( len * sumxy - sumx * sumy ); den = sqrt(len * sumx2 - sumx * sumx) * sqrt(len * sumy2 - sumy * sumy ); ans = num / den; return ans; }