|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <stdio.h>
void main()
{
int i,j,sum,average[3],averages;
int a[5][3]={{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[j][i];
}
average[i]=sum/5;
}
averages=(average[0]+average[1]+average[2])/3;
printf("average is %d\n%d\n%d\n",average[0],average[1],average[2]);
printf("averages is %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[3] , averages ;
int a[5][3]={{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[j][i] ;
average[i] = sum / 5 ;
}
averages = (average[0] + average[1] + average[2]) / 3 ;
printf("average is %d %d %d\n" , average[0] , average[1] , average[2]) ;
printf("averages is %d\n" , averages) ;
}
编译、运行实况: D:\[00.Exerciese.2022]\C>g++ -o x x.c
D:\[00.Exerciese.2022]\C>x
average is 72 73 81
averages is 75
D:\[00.Exerciese.2022]\C>
|
|