1

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.

1
  • Use different names for your variables and functions Commented Oct 31, 2015 at 15:41

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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

double N = mean(...);

Comments

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

Comments

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)

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 Comment

Do not access out of range (a[2000], b[2000], etc.)!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.