使用数组作为函数参数

时间:2021-11-14 21:47:18

I'm writing a program to work out a value from two arrays. I'm having trouble with passing and using arrays in my functions. Here is my code:

我正在编写一个程序来计算两个数组的值。我在我的函数中传递和使用数组时遇到了麻烦。这是我的代码:

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

const int MAX_STRING = 100;

double mean(double mean_array[]){
    double mean;

    for (int i=0; i<=2000000; i++){
        mean = mean + mean_array[i];
    }

    mean = mean/2000000;

    return mean;

}

double stan_dev_seq(double stan_array[]){

    double mean = mean(stan_array);

    double a;

    for (int i=0; i<=2000000; i++){
        a = a + pow(stan_array[i]-mean, 2);
    }

    a = sqrt(a/2000000);

    return a;
}

int pearson_seq(void){

    double a[2000000];
    double b[2000000];

    double mean_a;
    double mean_b;

    for (int i=0; i<=2000000; i++){
        a[i] = sin(i);
        b[i] = sin(i+2);

    }

    double stan_dev_a = stan_dev_seq(a);
    double stan_dev_b = stan_dev_seq(b);

    return 0;
}

int main(void) {
    pearson_seq();

return 0;
}

And here is the error I get:

这是我得到的错误:

person_mpi.c: In function ‘stan_dev_seq’:
person_mpi.c:22:16: error: called object ‘mean’ is not a function or function pointer
  double mean = mean(stan_array);
                ^
person_mpi.c:22:9: note: declared here
  double mean = mean(stan_array);
         ^

I'm not really sure what is going on, any help would be appreciated.

我不确定发生了什么,任何帮助都会受到赞赏。

5 个解决方案

#1


2  

In same scope you can't declare two variables with same name. Either change the variable name mean or change the function name mean.

在同一范围内,您不能声明具有相同名称的两个变量。更改变量名称均值或更改函数名称均值。

The variable name mean inside the function stan_dev_seq hides the name of function mean.

函数stan_dev_seq中的变量名意味着隐藏函数mean的名称。

#2


0  

double mean = mean(stan_array);

double mean = mean(stan_array);

You have a variable called mean, and the compiler is thinking the second mean also refers to this variable, rather than to your function. Give them different names.

你有一个名为mean的变量,编译器认为第二个意思也是指这个变量,而不是你的函数。给他们不同的名字。

#3


0  

You're trying to call mean, which is a simple variable inside your mean function. Just rename it to something different:

你试图调用mean,这是你的平均函数中的一个简单变量。只需将其重命名为不同的东西:

double N = mean(...);

#4


0  

  • The name conflicts. Change the name of the local variable mean in stan_dev_seq.
  • 名称冲突。在stan_dev_seq中更改局部变量mean的名称。
  • You allocate very large array on the stack and it may lead to Segmentation Fault. Consider allocating them on the heap using malloc or make them static variable.
  • 您在堆栈上分配非常大的数组,这可能会导致分段错误。考虑使用malloc在堆上分配它们或使它们成为静态变量。
  • You mustn't access a[2000000], b[2000000] or any equivalent because they are out of range.
  • 您不能访问[2000000],b [2000000]或任何等效项,因为它们超出范围。
  • The local variable mean in the function mean isn't initlaized. You should initialize it.
  • 函数均值中的局部变量均值未初始化。你应该初始化它。

Fixed code:

固定代码:

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

const int MAX_STRING = 100;

double mean(double mean_array[]){
    double mean = 0;

    for (int i=0; i<2000000; i++){
        mean = mean + mean_array[i];
    }

    mean = mean/2000000;

    return mean;

}

double stan_dev_seq(double stan_array[]){

    double mean_data = mean(stan_array);

    double a;

    for (int i=0; i<2000000; i++){
        a = a + pow(stan_array[i]-mean_data, 2);
    }

    a = sqrt(a/2000000);

    return a;
}

int pearson_seq(void){

    static double a[2000000];
    static double b[2000000];

    double mean_a;
    double mean_b;

    for (int i=0; i<2000000; i++){
        a[i] = sin(i);
        b[i] = sin(i+2);

    }

    double stan_dev_a = stan_dev_seq(a);
    double stan_dev_b = stan_dev_seq(b);

    return 0;
}

int main(void) {
    pearson_seq();

    return 0;
}

#5


0  

Change the naming. Variable mean and function mean should have different naming. and initialize the mean variable. This should work (BTW I reduced the total number cause it caused SOF on my machine)

更改命名。变量均值和函数均值应具有不同的命名。并初始化平均变量。这应该工作(BTW我减少了导致它在我的机器上导致SOF的总数)

double calc_mean(double mean_array[]){
    double mean=0;

    for (int i = 0; i <= 2000; i++){
        mean = mean + mean_array[i];
    }

    mean = mean / 2000;

    return mean;

}

double stan_dev_seq(double stan_array[]){

    double mean = calc_mean(stan_array);

    double a=0;

    for (int i = 0; i <= 2000; i++){
        a = a + pow(stan_array[i] - mean, 2);
    }

    a = sqrt(a / 2000);

    return a;
}

int pearson_seq(){

    double a[2000];
    double b[2000];

    for (int i = 0; i <= 2000; i++){
        a[i] = sin(float(i));
        b[i] = sin(float(i + 2));

    }

    double stan_dev_a = stan_dev_seq(a);
    double stan_dev_b = stan_dev_seq(b);

    return 0;
}

int main(void) {
    pearson_seq();

    return 0;
}

#1


2  

In same scope you can't declare two variables with same name. Either change the variable name mean or change the function name mean.

在同一范围内,您不能声明具有相同名称的两个变量。更改变量名称均值或更改函数名称均值。

The variable name mean inside the function stan_dev_seq hides the name of function mean.

函数stan_dev_seq中的变量名意味着隐藏函数mean的名称。

#2


0  

double mean = mean(stan_array);

double mean = mean(stan_array);

You have a variable called mean, and the compiler is thinking the second mean also refers to this variable, rather than to your function. Give them different names.

你有一个名为mean的变量,编译器认为第二个意思也是指这个变量,而不是你的函数。给他们不同的名字。

#3


0  

You're trying to call mean, which is a simple variable inside your mean function. Just rename it to something different:

你试图调用mean,这是你的平均函数中的一个简单变量。只需将其重命名为不同的东西:

double N = mean(...);

#4


0  

  • The name conflicts. Change the name of the local variable mean in stan_dev_seq.
  • 名称冲突。在stan_dev_seq中更改局部变量mean的名称。
  • You allocate very large array on the stack and it may lead to Segmentation Fault. Consider allocating them on the heap using malloc or make them static variable.
  • 您在堆栈上分配非常大的数组,这可能会导致分段错误。考虑使用malloc在堆上分配它们或使它们成为静态变量。
  • You mustn't access a[2000000], b[2000000] or any equivalent because they are out of range.
  • 您不能访问[2000000],b [2000000]或任何等效项,因为它们超出范围。
  • The local variable mean in the function mean isn't initlaized. You should initialize it.
  • 函数均值中的局部变量均值未初始化。你应该初始化它。

Fixed code:

固定代码:

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

const int MAX_STRING = 100;

double mean(double mean_array[]){
    double mean = 0;

    for (int i=0; i<2000000; i++){
        mean = mean + mean_array[i];
    }

    mean = mean/2000000;

    return mean;

}

double stan_dev_seq(double stan_array[]){

    double mean_data = mean(stan_array);

    double a;

    for (int i=0; i<2000000; i++){
        a = a + pow(stan_array[i]-mean_data, 2);
    }

    a = sqrt(a/2000000);

    return a;
}

int pearson_seq(void){

    static double a[2000000];
    static double b[2000000];

    double mean_a;
    double mean_b;

    for (int i=0; i<2000000; i++){
        a[i] = sin(i);
        b[i] = sin(i+2);

    }

    double stan_dev_a = stan_dev_seq(a);
    double stan_dev_b = stan_dev_seq(b);

    return 0;
}

int main(void) {
    pearson_seq();

    return 0;
}

#5


0  

Change the naming. Variable mean and function mean should have different naming. and initialize the mean variable. This should work (BTW I reduced the total number cause it caused SOF on my machine)

更改命名。变量均值和函数均值应具有不同的命名。并初始化平均变量。这应该工作(BTW我减少了导致它在我的机器上导致SOF的总数)

double calc_mean(double mean_array[]){
    double mean=0;

    for (int i = 0; i <= 2000; i++){
        mean = mean + mean_array[i];
    }

    mean = mean / 2000;

    return mean;

}

double stan_dev_seq(double stan_array[]){

    double mean = calc_mean(stan_array);

    double a=0;

    for (int i = 0; i <= 2000; i++){
        a = a + pow(stan_array[i] - mean, 2);
    }

    a = sqrt(a / 2000);

    return a;
}

int pearson_seq(){

    double a[2000];
    double b[2000];

    for (int i = 0; i <= 2000; i++){
        a[i] = sin(float(i));
        b[i] = sin(float(i + 2));

    }

    double stan_dev_a = stan_dev_seq(a);
    double stan_dev_b = stan_dev_seq(b);

    return 0;
}

int main(void) {
    pearson_seq();

    return 0;
}