c - 参数 1 的类型不兼容

标签 c arrays incompatibletypeerror

我有这个程序,在 C 中,我得到了这些错误:

calcularpi.c:37:2: 错误:'media' 的参数 1 的类型不兼容

calcularpi.c:22:8: 注意:应为“double *”但参数类型为“double”

calcularpi.c:38:2: 错误:“eam”的参数 1 的类型不兼容

calcularpi.c:23:8: 注意:应为 'double ' 但参数的类型为 '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;
        }

我想知道数组或变量问题在哪里,这样我就可以完成应用程序。 我认为其余的都很好。 谢谢。

最佳答案

改变:

printf("El valor es: \n %lf", media(valores[100]));
printf("El error es: \n %lf", eam(valores[100]));

到:

printf("El valor es: \n %f", media(valores));
printf("El error es: \n %f", eam(valores));

valores 是您的数组,在这里,并在此上下文中衰减以键入 double * - valores[100] 是的第 101 个元素您的数组(比它实际拥有的元素数多一个)并衰减为 double 类型。由于 media()eam() 都接受类型为 double * 的单个参数(double media(double valores[100] ); 等同于 double media(double * valores);),这就是你得到类型不匹配错误的原因。

此外,%f 是更适合 doubleprintf() 的格式说明符。这不同于 scanf() 和 friend ,其中 %lf 是正确的。

关于c - 参数 1 的类型不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26963889/

相关文章:

c - 错误: 'answer' undeclared (first use in this function)

arrays - 如何从另一个数组中获取具有已批准 ID 的项目? map ()过滤器()或?

arrays - 不能使用 "a"(字符串类型)作为 go 中数组元素的类型

c - C 中的数据类型

c++ - 当我尝试使用 AVX 功能时 Clang 生成错误

C:传递指针:不兼容的指针类型警告

ios - 从不兼容的 double 类型分配给 NSNumber

java - java中数组中的不兼容类型

c - 时间段错误(0);

arrays - 如何修复 JSON 解析应用程序中的 'Index Out Of Range' 错误