【发布时间】:2014-01-29 23:45:50
【问题描述】:
我必须读取 10 个整数,计算每个值的 90%,以倒序打印它们,最后打印我获得的各种值的中间值。
但它不起作用,它只打印零。另一件事是,在我输入了 10 个值之后,它仍然需要另一个输入才能开始打印。我是 C 新手,所以我可能犯了一个愚蠢的错误......
#include <stdio.h>
int main ()
{
int n = 10; //number of numbers
float array[10];
for(int i = 0 ; i < n ; i++) {
scanf("%d \n", &array[i] ) ; //read the keyboard input and memorize it in the array
}
for( int i = 9; i > -1 ; i-- ) {
array[i] = array[i]*90.0/100.0; //calculate the 90% of every value
printf("%f \n", array[i]); //prints the values in opposite order
}
float s = 0;
for(int i = 0 ; i < 10 ; i++){
s = s+array[i]; //add up all the values
}
float m =s/n; //calculate the mid value
printf("%f \n",m); //prints it
system("PAUSE");
return 0;
}
【问题讨论】:
-
90/100 是整数运算,结果为 0。使用 90.0/100.0
-
这不是 C++代码。您的意思是使用
C标签吗?如果您想要 C++ 答案,惯用的方式会更加简洁。此外,您将超出array的范围。 -
@OldProgrammer:应该不是问题,因为表达式的从左到右求值(float*90/100 保持浮动)...
-
我已经制作了 10 个元素的数组而不是 9 个元素,并且我在 90.0 和 100.0 中转换了 90 和 100,但仍然无法正常工作
-
@ninilo1: 也许
scanf和%d不适用于float参数(但我真的不知道这些C 函数)。尝试将%f与scanf 一起使用,并在浮点数处输入整数,如4.0。
标签: c arrays printf scanf percentage