编写一个程序使之读入一系列整型数据直到读入标志值-1时为止。此时程序显示出之前所输入数据的平均值,编写的程序应能产生下面的示例
this program averages a list of integers.
enter -1 to signal the end of the list.
?95
?100
?89
?97
?91
?97
?-1
the average is 94.4
main()
{int num,sum=0,n=0;
float average;
printf("this program averages a list of integers.\n");
printf("enter -1 to signal the end of the list.\n");
printf("?");
scanf("%d",&num);
while(num!=-1)
#include <stdio.h>
int main(void) {
float avg = 0.0F ;
int i , current_integer ;
char *msg = "this program averages a list of integers.\n"
"enter -1 to signal the end of the list.\n" ;
printf(msg) ;
for (i = 1 ;putchar('?') ,scanf("%d",¤t_integer) && current_integer != -1;i++)
avg += (current_integer - avg) / i ;
printf("the average is %.1f\n" , avg) ;
return 0 ;
}