C 为什么程序要打印0s?
我已经检查了代码并重新编写了好几次,每次我在打印数组和平均值时得到0。我使用代码块作为ide 下面是statlib.cC 为什么程序要打印0s?,c,arrays,C,Arrays,我已经检查了代码并重新编写了好几次,每次我在打印数组和平均值时得到0。我使用代码块作为ide 下面是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 on
// 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
//计算数组的平均值
双计算表(整数totnum,双数据[])
{
双和=0.0;
双平均=0.0;
int i;
//在数组中逐个添加元素
对于(i=0;i
下面是另一个文件
#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;
}
#包括“statlib.c”
#包括
内部主(空){
int i;//用于打印未排序数组的计数器
双平均值=0.0;
双数据[10]={30.0,90.0100.0,84.0,72.0,40.0,34.0,91.0,80.0,62.0};//作业中给出的测试数据
int totnum=10;//数组中的总数
//打印未排序的数组
printf(“未排序的数组是:{”);
对于(i=0;i
您正试图使用%lf
格式说明符打印意思。那个格式说明符,所以可能出了问题
double
的正确格式说明符应该是%f
,而l
长度修饰符只允许用于整数格式。(对于浮点,有L
,使%Lf
成为长双精度
的正确格式说明符)。简单一看,您的代码看起来不错。我运行了它,结果看起来和预期的一样。我也看不出有什么错误。我希望使用通常的整数舍入等,但没有…尝试使用“%f”作为格式说明符..我使用GCC编译并在Debian上启动它,我得到未排序的数组是:{30.000000,90.000000,100.000000,84.000000,72.000000,40.000000,34.000000,91.000000,80.000000,62.000000,}平均值是:68.300000
MSVC。未排序的数组是:{30.000000,90.000000,100.000000,84.000000,72.000000,40.000000,34.000000,91.000000,80.000000,62.000000,}
。平均值是:68.300000
OP显示“代码块”,但没有指定使用什么编译器。例如,clang将用这种结构输出警告。@Jongware我认为编译器是GNU GCC