Reputation: 1038
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
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
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
Reputation: 75062
mean
in stan_dev_seq
.malloc
or make them static variable.a[2000000]
, b[2000000]
or any equivalent because they are out of range.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
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
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