user2320239
user2320239

Reputation: 1038

Using an array as a function parameter

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.

Upvotes: 1

Views: 49

Answers (5)

Humam Helfawi
Humam Helfawi

Reputation: 20264

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)

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

Upvotes: 0

haccks
haccks

Reputation: 106012

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.

Upvotes: 2

MikeCAT
MikeCAT

Reputation: 75062

  • The name conflicts. Change the name of the local variable mean in stan_dev_seq.
  • 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.
  • You mustn't access a[2000000], b[2000000] or any equivalent because they are out of range.
  • 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;
}

Upvotes: 0

ForceBru
ForceBru

Reputation: 44838

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

double N = mean(...);

Upvotes: 0

Marco Tompitak
Marco Tompitak

Reputation: 658

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.

Upvotes: 0

Related Questions