Project #30800 - Computer Science - Array & I/O File Reading

#include <stdio.h>
#include <math.h>

#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;
}
  


  
  

Subject Computer
Due By (Pacific Time) 05/12/2014 11:30 pm
Report DMCA
TutorRating
pallavi

Chat Now!

out of 1971 reviews
More..
amosmm

Chat Now!

out of 766 reviews
More..
PhyzKyd

Chat Now!

out of 1164 reviews
More..
rajdeep77

Chat Now!

out of 721 reviews
More..
sctys

Chat Now!

out of 1600 reviews
More..
sharadgreen

Chat Now!

out of 770 reviews
More..
topnotcher

Chat Now!

out of 766 reviews
More..
XXXIAO

Chat Now!

out of 680 reviews
More..
All Rights Reserved. Copyright by AceMyHW.com - Copyright Policy