0

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:

#include <stdio.h>
#include <stdlib.h>
#include <string.h> 
#include <math.h>
#include <time.h>

#define aSize 2000000


double mean(double* mean_array){
    double mean = 0;

    for (int i=0; i<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<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<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<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;
} 

If I run the program I get an incorrect value for the coefficient then a seg fault, which really makes no sense to me.

If I print the size of the arrays I get values in no way related to what the array size should be.

Any help would be appreciated.

Gilles
  • 9,269
  • 4
  • 34
  • 53
user2320239
  • 1,021
  • 2
  • 18
  • 43
  • In addition to the `sizeof` error, you should look into the format specifiers. You need `%zu` to print `size_t`, the result type of `sizeof`, and I believe the seg fault occurs when you try to print `clock_t` with `%f`. (Compiler warnings should help you to detect mismatched format specifiers and arguments.) – M Oehm Nov 02 '15 at 12:56
  • the posted code does not cleanly compile!! the compiler raises two problem statements one about ` printf("%f %s\n", end, "ms");` and one about `printf("%f %s\n", begin, "ms");`. When compiling, always enable all warnings (for gcc, at a minimum use: `-Wall -Wextra -pedantic`) then fix those warnings. – user3629249 Nov 03 '15 at 23:51
  • Always check (!=NULL) the returned value from `malloc()` to assure the operation was successful – user3629249 Nov 03 '15 at 23:58
  • Note: `clock_t` is (depending on the underlying clock) a 32 or 64 bit unsigned int, not a float. – user3629249 Nov 03 '15 at 23:59

2 Answers2

0
for (int i=0; i<sizeof(mean_array); i++){

Arrays decay to pointers when passed to a function, so sizeof(mean_array) is equivalent to sizeof(double) which is not what you want. Pass the array size in an additional function argument.

Magisch
  • 7,312
  • 9
  • 36
  • 52
0

For me, crash occurs in printf statements because of using %f for clock_t type. According to the link here, there may be no specific modifier to use for clock_t. Instead you can typecast the value and print as below.

printf("%f %s\n", (float) end, "ms");
printf("%f %s\n", (float) begin, "ms");
Community
  • 1
  • 1
user1969104
  • 2,340
  • 14
  • 15