我是 c 的新手,我正在嘗試制作一個計算器,詢問您是要計算一組的標準偏差還是一組的平均值。但是,當我運行代碼時,出現了圖片中的錯誤。錯誤畫面。我不知道如何糾正它。我相信我在代碼中也犯了一些錯誤,所以如果你看到任何其他錯誤,你會介意指出它。謝謝你。
#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);
}
uj5u.com熱心網友回復:
你宣告了一個元素型別的陣列 float
float num[100], sum = 0.0, c;
所以你需要使用轉換說明符%f而不是%lf. 最后一個是為 double 型別的物件設計的
scanf("%f", &num[i]);
在這個 scanf 呼叫中
scanf("%u", &c);
您正在使用%u為型別物件指定的轉換說明符unsigned int和型別為 float 的物件
c像這樣宣告變數
unsigned int c;
在 pruntf 的這些呼叫中
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 ) );
請注意,程式在 lwast 處將具有未定義的行為,因為您在函式中使用了未初始化的變數,例如
int n;
float num[100], sum = 0.0, avg;
avg = sum / n;
//...
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/380474.html
下一篇:無法檢索下一個物件:迭代器已結束
