C语言 格式“%f”需要“double”类型得参数,但参数2得类型为“float(*)(float *)”

col17t5w  于 2022-12-03  发布在  其他
关注(0)|答案(1)|浏览(230)

我是一个新的c,我试图使一个计算器,询问您是否要计算一组的标准差或平均值。但是,当我运行代码时,我得到的错误,如图所示。Error screen。我不知道如何纠正它。我相信我也犯了一些错误,通过代码,所以如果你看到任何其他的一个错过,你介意指出它。谢谢。

#include <stdio.h>
#include <math.h>
float StandardDev(float data[]);
float Average(float data[]);

float Average(float data[]) {
    int n;
    float num[100], sum = 0.0, avg;

    avg = sum / n;
    return 0;
}

float StandardDev(float data[]){
    float sum = 0.0, mean, SD = 0.0;
    int i, n;
    for (i = 0; i < n; ++i) {
        sum += data[i];
    }
    mean = sum / n;
    for (i = 0; i < n; ++i) {
        SD += pow(data[i] - mean, 2);
    }
    return sqrt(SD / 10);
}

int main()
{  
    int n, i;
    float num[100], sum = 0.0, c;
    printf("Enter the numbers of elements: ");
    scanf("%d", &n);

    while (n > 100 || n < 1) {
        printf("Error! number should in range of (1 to 100).\n");
        printf("Enter the number again: ");
        scanf("%d", &n);
    }

    for (i = 0; i < n; ++i) {
        printf("%d. Enter number: ", i + 1);
        scanf("%lf", &num[i]);
        sum += num[i];
    }
    printf("Do you want to calculate the Standard Deviation(1) of   a set or find the   Average(2) of a set? ");
    scanf("%u", &c);
    if(c==1){
        printf("The Standard Deviation of your set is %f", StandardDev);
    }
    else{
        printf("The average of your set is %f", Average);
    }
    return(0);
}
hs1ihplo

hs1ihplo1#

您声明了一个元素类型为float的数组

float num[100], sum = 0.0, c;

因此需要使用转换说明符%f而不是%lf

scanf("%f", &num[i]);

在这个叫scanf的

scanf("%u", &c);

您正在使用为unsigned int类型的对象指定的转换说明符%u和float类型的对象
将变量c声明为

unsigned int c;

在printf的这些调用中

printf("The Standard Deviation of your set is %f", StandardDev);
    printf("The average of your set is %f", Average);

您正在尝试输出函数指针,而不是函数调用的结果。
您的意思似乎如下所示

printf("The Standard Deviation of your set is %f", StandardDev( num ));
    printf("The average of your set is %f", Average( num ) );

请注意,程序将有未定义的行为,至少因为您在函数中使用了未初始化的变量,例如

int n;
float num[100], sum = 0.0, avg;

avg = sum / n;
//...

相关问题