c - 平均数组类型 double

标签 c arrays double mean

#include <stdio.h>

int main()
{
int n, i;
double num[100], sum = 0.0, average;

printf("Enter the numbers of elements: ");
scanf("%d", &n);

for(i = 0; i < n; i++)
{
    printf("Enter number %d: ", i+1);
    scanf("%f", &num[i]);
    sum += num[i];
}

average = sum / n;
printf("Average = %f", average);

return 0;
}

您好,我在显示平均值、显示空间数字时遇到问题,如何解决?

最佳答案

double 的正确格式说明符是(您必须使用它)

scanf("%lf", &num[i]);

对于 printf%f%lf 都可以。请注意,使用 scanf 时请检查 scanf 的返回值 - 如果失败,您将采取必要的操作。 (输入错误或其他错误)。

关于c - 平均数组类型 double,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48600453/

相关文章:

javascript - Node.JS - filter() 仅在索引中进行硬编码时才起作用

c - malloc() 和固定大小的简单声明之间的区别

C++使用配置类从txt文件设置变量值

c++ - 操作数是否存储在内存中? (C/C++)

检查两个数组中的共同值并将不同的值放入第三个数组中

c - C 中的字节顺序和套接字编程

c# - 我如何使用 linq 从索引数组转到对象集合?

python - 如何动态创建三维数组

java - for 带有十进制数的语句

c - 错误: conflicting types for 'removeSpaces'