正在加载图片...
Local variable variables declared within a block include <stdio. h> int main o int next, n;/* next input, number of inputs * long SUnn i/* running total * for (n =0; scanf(gi,&next)== 1; n++)i sum+=nex七 /* compute and print input average * double avg =(n == 0)? 0.0: (double)sum /n; 2 printf("Average of i values is f.\n", n, avg)i return 0; avg is declared when it is needed PROGRAMMINGMETHDOLODGY AND SOFTWAREENGINEERING 港城市大 Copyrighto1998 Angus Wu ol Hone KonePROGRAMMING METHDOLODGY AND SOFTWARE ENGINEERING Copyright©1998 Angus Wu Local Variable • variables declared within a block avg is declared when it is needed. #include <stdio.h> int main() { int next, n; /* next input, number of inputs */ long sum = 0; /* running total */ for (n = 0; scanf("%i", &next) == 1; n++){ sum += next; { /* compute and print input average */ double avg = (n == 0) ? 0.0 : (double) sum / n; printf("Average of %i values is %f.\n", n, avg); } return 0; }
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有