[英]Why is the program printing 0s?
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. 我已经检查了一下代码,并已经将它重写了几次,每次打印数组和均值时得到0。 I'm using codeblocks as the ide.
我正在使用代码块作为ide。
Below is statlib.c 下面是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. 您正在尝试使用
%lf
格式说明符打印mean
。 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. double
的正确格式说明符应为%f
,而l
length修饰符仅用于整数格式。 (For floating point there is L
, making %Lf
the correct format specifier for long double
). (对于浮点数为
L
,使%Lf
成为long double
的正确格式说明符)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.