|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
为什么top和lowest的值会是0,我调试的时候发现子程序运行完后,top和lowest的值是对的,但是为什么回到主程序就变成0了,是不是全局变量的问题,求大佬解答
- #include <stdio.h>
- int top,lowest;
- int main(void)
- {
- double average(int b[]);
- double ave;
- int i,a[10];
- printf("Input 10 students' score:");
- for(i=0;i<10;i++)
- scanf("%d",&a[i]);
- ave=average(a);
- printf("%lf\n %d\n %d\n",ave,top,lowest);
- }
- double average(int b[])//形参可以不定义数组大小
- {
- double ave;
- int i,top=b[0],lowest=b[0];
- for(i=1,ave=b[0];i<10;i++)
- {
- if(top<b[i])
- top=b[i];
- else if(lowest>b[i])
- lowest=b[i];
- ave=ave+b[i];
- }
- ave=ave/10.0;
-
- return ave;//全局变量不用返回
- }
复制代码
本帖最后由 xieglt 于 2021-1-12 15:50 编辑
- #include <stdio.h>
- int top,lowest;
- int main(void)
- {
- double average(int b[]);
- double ave;
- int i,a[10];
- printf("Input 10 students' score:");
- for(i=0;i<10;i++)
- scanf("%d",&a[i]);
- ave=average(a);
- printf("%lf\n %d\n %d\n",ave,top,lowest);
- return 0;
- }
- double average(int b[])//形参可以不定义数组大小
- {
- double ave;
- int i;
- top=b[0],lowest=b[0];
- for(i=1,ave=b[0];i<10;i++)
- {
- if(top<b[i])
- top=b[i];
- else if(lowest>b[i])
- lowest=b[i];
- ave=ave+b[i];
- }
- ave=ave/10.0;
-
- return ave;//全局变量不用返回
- }
复制代码
|
|