gpt4 book ai didi

c - PIL 逊系数计算数组

转载 作者:行者123 更新时间:2023-11-30 17:08:54 25 4
gpt4 key购买 nike

我正在编写一个程序来计算 c 中的 PIL 逊系数,但我遇到了一些麻烦,我不确定问题是什么,这是我的代码:

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

如果我运行该程序,我会得到一个不正确的系数值,然后是一个段错误,这对我来说真的毫无意义。

如果我打印数组的大小,我得到的值与数组大小无关。

如有任何帮助,我们将不胜感激。

最佳答案

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

数组在传递给函数时会衰减为指针,因此 sizeof(mean_array) 相当于 sizeof(double) 这不是您想要的。在附加函数参数中传递数组大小。

关于c - PIL 逊系数计算数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33477974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com