title: Pearson Coefficient calculation array
body: <p>I'm writing a program to calculate the pearson coefficient in c, but I'm having some trouble and I'm not sure what the problem is, here is my code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt; 
  #include &lt;math.h&gt;
  #include &lt;time.h&gt;
  
  #define aSize 2000000
  
  
  double mean(double* mean_array){
      double mean = 0;
  
      for (int i=0; i&lt;aSize; i++){
          mean = mean + mean_array[i];
      }
  
  
      mean = mean/aSize;
  
      return mean;
  
  }
  
  double stan_dev_seq(double stan_array[], double stan_mean){
  
      double a = 0;
  
      for (int i=0; i&lt;aSize; i++){
          a = a + pow((stan_array[i]-stan_mean), 2);
      }
  
      a = a/aSize;
  
      a = sqrt(a);
  
      return a;
  }
  
  int pearson_seq(void){
  
      clock_t begin, end;
      double time_spent;
  
      begin = clock();
  
      double *a;
      a = malloc(sizeof(double)*aSize);
  
  
      double *b;
      b = malloc(sizeof(double)*aSize);
  
      double mean_a;
      double mean_b;
  
      for (int i=0; i&lt;aSize; i++){
          a[i] = sin(i);
          b[i] = sin(i+2);    
  
      }
  
      mean_a = mean(a);
      mean_b = mean(b);
  
  
  
      double stan_dev_a = stan_dev_seq(a, mean_a);
      double stan_dev_b = stan_dev_seq(b, mean_b);
  
      double pearson_numer;
  
      for(int i=0; i&lt;aSize; i++){
          pearson_numer = pearson_numer + ((a[i]-mean_a)*(b[i]-mean_b));
      }
  
      pearson_numer = pearson_numer/aSize;
  
      double pearson_coef = pearson_numer/(stan_dev_a*stan_dev_b);
  
      printf("%s %G\n", "The Pearson Coefficient is: ", pearson_coef);
  
      end = clock();
  
      time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  
  
      printf("%f %s\n", end, "ms");
      printf("%f %s\n", begin, "ms");
      printf("%f %s\n", time_spent, "ms");
  
  
  
      free(a);
      free(b);
  
      return 0;
  }
  
  int main(void) {
  
      pearson_seq();
  
  return 0;
  } 
  </code></pre>
  
  <p>If I run the program I get an incorrect value for the coefficient then a seg fault, which really makes no sense to me.</p>
  
  <p>If I print the size of the arrays I get values in no way related to what the array size should be. </p>
  
  <p>Any help would be appreciated. </p>