[英]Why is the program printing 0s?
我已經檢查了一下代碼,並已經將它重寫了幾次,每次打印數組和均值時得到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 one
for(i = 0; i < totnum; i++ )
sum += data[i];
average = (sum/totnum);
return average;
}// end function calculateMean
以下是其他文件
#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;
}
您正在嘗試使用%lf
格式說明符打印mean
。 該格式說明符無效 ,因此那里可能出錯了。
double
的正確格式說明符應為%f
,而l
length修飾符僅用於整數格式。 (對於浮點數為L
,使%Lf
成為long double
的正確格式說明符)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.