繁体   English   中英

为什么程序显示0s?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM