鱼C论坛

 找回密码
 立即注册
查看: 2332|回复: 2

[已解决]c语言初学者请大佬帮我看一下错在哪折磨我一晚上了

[复制链接]
发表于 2022-11-6 22:08:23 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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

最佳答案
2022-11-6 22:24:32
本帖最后由 jackz007 于 2022-11-6 22:32 编辑

        sum 没有初始化为 0 值。
  1. #include <stdio.h>

  2. int main(void)
  3. {
  4.     int i , j , sum , average[3] , averages                                ;
  5.     int a[5][3]={{80,75,92},{61,65,71},{59,63,70},{85,87,90},{76,77,85}}   ;
  6.     for(i = 0 ; i < 3 ; i ++)
  7.     {
  8.         
  9.         for(sum = j = 0 ; j < 5 ; j ++) sum=sum + a[j][i]                  ;
  10.         average[i] = sum / 5                                               ;
  11.     }
  12.     averages = (average[0] + average[1] + average[2]) / 3                  ;
  13.     printf("average is %d %d %d\n" , average[0] , average[1] , average[2]) ;
  14.     printf("averages  is %d\n" , averages)                                 ;
  15. }
复制代码

        编译、运行实况:
  1. D:\[00.Exerciese.2022]\C>g++ -o x x.c

  2. D:\[00.Exerciese.2022]\C>x
  3. average is 72 73 81
  4. averages  is 75

  5. D:\[00.Exerciese.2022]\C>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-11-6 22:24:32 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2022-11-6 22:32 编辑

        sum 没有初始化为 0 值。
  1. #include <stdio.h>

  2. int main(void)
  3. {
  4.     int i , j , sum , average[3] , averages                                ;
  5.     int a[5][3]={{80,75,92},{61,65,71},{59,63,70},{85,87,90},{76,77,85}}   ;
  6.     for(i = 0 ; i < 3 ; i ++)
  7.     {
  8.         
  9.         for(sum = j = 0 ; j < 5 ; j ++) sum=sum + a[j][i]                  ;
  10.         average[i] = sum / 5                                               ;
  11.     }
  12.     averages = (average[0] + average[1] + average[2]) / 3                  ;
  13.     printf("average is %d %d %d\n" , average[0] , average[1] , average[2]) ;
  14.     printf("averages  is %d\n" , averages)                                 ;
  15. }
复制代码

        编译、运行实况:
  1. D:\[00.Exerciese.2022]\C>g++ -o x x.c

  2. D:\[00.Exerciese.2022]\C>x
  3. average is 72 73 81
  4. averages  is 75

  5. D:\[00.Exerciese.2022]\C>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-11-6 23:34:53 | 显示全部楼层
感谢感谢,能睡个好觉了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-14 18:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表