程序1:
#include
#include
int main()
{
int i;
float num;
float sum = 0;
i = 10;
while(i--)
{
scanf("%f",&num);
sum += num;
}
sum /= 10.0;
printf("输入的十个数的平均数为: %f",sum);
getch();
return 0;
}
思考:
#include
#include
int main()
{
int i,amount;
float num;
float sum = 0;
printf("请输入数据的总个数\n");
scanf("%d",&amount);
printf("请依次输入%d个数据.\n",amount);
i = amount;
while(i--)
{
scanf("%f",&num);
sum += num;
}
sum = sum / (amount / 1.0);
printf("输入的%d个数的平均数为: %f \n",amount,sum);
getch();
return 0;
}
这个注意循环次数就行了,do-while语句同while语句最大的区别就是一个是先判断,一个是先执行一次再判断!