I have this program, in C, and I get these errors:
calcularpi.c:37:2: error: incompatible type for argument 1 of 'media'
calcularpi.c:22:8: note: expected 'double *' but argument is of type 'double'
calcularpi.c:38:2: error: incompatible type for argument 1 of 'eam'
calcularpi.c:23:8: note: expected 'double ' but argument is of type 'double'
double genera_valor(double lim_inf, double rango);
int dentro_circunferencia(double x, double y);
double calcula_pi(long int num_puntos);
double media(double valores[100]);
double eam(double valores[100]);
int main(void){
srand48(time(NULL)); // Inicializamos semilla
long int num_puntos = 0;
double valores[100];
int i=0;
printf("Numero de puntos a generar: ");
scanf("%ld",&num_puntos);
while (i<100){
valores[i] = calcula_pi(num_puntos);
i++;
}
printf("El valor es: \n %lf", media(valores[100]));
printf("El error es: \n %lf", eam(valores[100]));
}
double media(double valores[100]){
double suma=0;
int i;
for (i=0;i<100;i++)
{
suma=suma + valores[i];
}
return suma/100;
}
double eam(double valores[100]){
int i=0;
double suma=0;
double errores[100];
while (i<100)
{
errores[i]= fabs(M_PI - (valores[i]));
suma=suma+ errores[i];
i++;
}
return suma/100;
}
double calcula_pi(long int num_puntos){
int i=0;
double min=-1;
double puntoscircunferencia=0;
double rango=2;
double x=0;
double y=0;
while (i<num_puntos){
genera_valor(min, rango);
x=genera_valor(min,rango);
y=genera_valor(min,rango);
if (dentro_circunferencia (x, y)==1){
puntoscircunferencia++;
}
i++;
}
return (puntoscircunferencia*4)/num_puntos;
}
double media(double valores[100]);
double genera_valor(double lim_inf, double rango){
rango = 2;
lim_inf = -1;
double r=drand48();
double valor= lim_inf + rango * r;
return valor;
}
/* Comprueba si el punto esta dentro de la circunferencia.*/
int dentro_circunferencia(double x, double y){
if(y<1-x*x){
return 1;
}
else
return 0;
}
I'd like to know where is the array or variable problem, so I can finish the application. I think the rest is fine. Thanks.
Change:
to:
valores
is your array, here, and decays in this context to typedouble *
-valores[100]
is the 101st element of your array (which is one more than the number of elements it actually has) and decays to typedouble
. Sincemedia()
andeam()
both accept a single argument of typedouble *
(double media(double valores[100]);
is equivalent todouble media(double * valores);
), that's why you're getting your type mismatch error.Also
%f
is the more appropriate format specifier fordouble
withprintf()
. This is different fromscanf()
and friends, where%lf
would be correct.