c语言初学者请大佬帮我看一下错在哪折磨我一晚上了
#include <stdio.h>void main()
{
int i,j,sum,average,averages;
int a={{80,75,92},{61,65,71},{59,63,70},{85,87,90},{76,77,85}};
for(i=0;i<3;i++)
{
for(j=0;j<5;j++)
{
sum=sum+a;
}
average=sum/5;
}
averages=(average+average+average)/3;
printf("average is %d\n%d\n%d\n",average,average,average);
printf("averagesis %d\n",averages);
}
结果为 average is 92 165 247
averages is 168
本帖最后由 jackz007 于 2022-11-6 22:32 编辑
sum 没有初始化为 0 值。
#include <stdio.h>
int main(void)
{
int i , j , sum , average , averages ;
int a={{80,75,92},{61,65,71},{59,63,70},{85,87,90},{76,77,85}} ;
for(i = 0 ; i < 3 ; i ++)
{
for(sum = j = 0 ; j < 5 ; j ++) sum=sum + a ;
average = sum / 5 ;
}
averages = (average + average + average) / 3 ;
printf("average is %d %d %d\n" , average , average , average) ;
printf("averagesis %d\n" , averages) ;
}
编译、运行实况:
D:\\C>g++ -o x x.c
D:\\C>x
average is 72 73 81
averagesis 75
D:\\C> 感谢感谢,能睡个好觉了
页:
[1]