为什么程序打印0? [英] Why is the program printing 0s?
问题描述
我已经在code和重新编写了好几次已经和每一次我打印阵列和平均时得到0。我使用$ C $的cblock作为IDE。
下面是statlib.c
//计算数组的平均值
双calculateMean(INT totnum,双倍数据[])
{
双总和= 0.0;
双平均= 0.0;
INT I; //添加一个数组中的一个中的元素
对于(i = 0; I< totnum;我++)
总和+ =数据[I] 平均=(SUM / totnum);回到平均水平;
} //函数结束calculateMean
下面是其他文件
的#includestatlib.c
#包括LT&;&stdio.h中GT;诠释主要(无效){ INT I; //在印刷排序的数组使用计数器
双平均= 0.0;
双倍数据[10] = {30.0,90.0,100.0,84.0,72.0,40.0,34.0,91.0,80.0,62.0}; //分配中的测试数据给
INT totnum = 10;在阵列//总数
//打印排序的数组
的printf(未排序的数组:{);
对于(i = 0; I< totnum;我++){
的printf(%LF,数据[I]);
输出(,);
}
的printf(} \\ n);//获取和显示阵列的平均
平均= calculateMean(totnum,数据);
的printf(中庸是:%LF \\ N的意思);返回0;}
您要打印的意思是
与%LF
格式说明。这种格式说明无效,所以可能出现错误那里。
为正确的格式说明双
是%F
和→
长度修改为只允许整数格式。 (对于浮点有→
,使得%LF
正确的格式说明符长双
)。
I've gone over the code and re-written it several times already and each time I get 0s when printing the array and the mean. I'm using codeblocks as the ide.
Below is statlib.c
// Calculates the mean of the array
double calculateMean(int totnum, double data[ ])
{
double sum = 0.0;
double average = 0.0;
int i;
// adds elements in the array one by one
for(i = 0; i < totnum; i++ )
sum += data[i];
average = (sum/totnum);
return average;
}// end function calculateMean
Below is the other file
#include "statlib.c"
#include <stdio.h>
int main (void){
int i; // counter used in printing unsorted array
double mean = 0.0;
double data[10] = {30.0,90.0,100.0,84.0,72.0,40.0,34.0,91.0,80.0,62.0}; // test data given in assignment
int totnum = 10; // total numbers in array
//Print the unsorted array
printf("The unsorted array is: {");
for ( i = 0; i < totnum; i++){
printf(" %lf",data[i]);
printf(",");
}
printf("}\n");
//Get and display the mean of the array
mean = calculateMean(totnum,data);
printf("The mean is: %lf\n",mean);
return 0;
}
You are trying to print mean
with a %lf
format specifier. That format specifier isn't valid, so probably something goes wrong there.
The correct format specifier for double
would be %f
, and the l
length modifier is only allowed for integer formatting. (For floating point there is L
, making %Lf
the correct format specifier for long double
).
这篇关于为什么程序打印0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!