weixin_73000861 2022-08-16 11:21 采纳率: 97.9%
浏览 73
已结题

你知道为什么这段代码偏偏第七个数字是个垃圾数吗?


#include <stdio.h>
#define MONTH 12
#define YEARS 5
void maverage (float arr1[MONTH] , float (*arr2)[MONTH]);
void yaverage (float arr1[YEARS] , float (*arr2)[MONTH]);
//用VLA
void showit(float *arr1 , int num);
int main(void)
{
    float monaver[MONTH];//每月平均
    float yearaver[YEARS];//每年平均
    //float averagemon;
    //float averageyear;
    float allyear;
    const float rain[YEARS][MONTH]=  {
        {4.3,4.3,4.3,3.0,2.0,1.2,0.2,0.2,0.4,2.4,3.5,6.6},
        {8.5,8.2,1.2,1.6,2.4,0.0,5.2,0.9,0.3,0.9,1.4,7.3},
        {9.1,8.5,6.7,4.3,2.1,0.8,0.2,0.2,1.1,2.3,6.1,8.4},
        {7.2,9.9,8.4,3.3,1.2,0.8,0.4,0.0,0.6,1.7,4.3,6.2},
        {7.6,5.6,3.8,2.8,3.8,0.2,0.0,0.0,0.0,1.3,2.6,5.2},
    };
    
    printf(" Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  ");
    printf(" Nov  Dec\n");//%4.1f;
    maverage(monaver , rain);
    //showit(monaver , MONTH);
    
    return 0;
}

void maverage (float arr1[MONTH] , float (*arr2)[MONTH])
{
    float subit[MONTH];
    int i , j;
    
    for(i = 0 ; i < MONTH  ; i++)
    {
        for(j = 0 ; j < YEARS ; j++)
        {
            subit[i] += arr2[j][i];
        }
        //arr1[i] = subit[i] / YEARS;
    printf("%4.1f " , subit[i] / YEARS);
    }
}

为什么这段代码偏偏第七个数字是个垃圾数?其余全部都是没问题的呢?

  • 写回答

2条回答 默认 最新

  • a5156520 2022-08-16 12:06
    关注

    应该是因为maverage()函数的subit数组没有初始化,把subit初始化下就可以了,修改如下:

    
     
    #include <stdio.h>
    #define MONTH 12
    #define YEARS 5
    void maverage (float arr1[MONTH] , const float (*arr2)[MONTH]);
    void yaverage (float arr1[YEARS] , const float (*arr2)[MONTH]);
    //用VLA
    void showit(float *arr1 , int num);
    int main(void)
    {
        float monaver[MONTH];//每月平均
        float yearaver[YEARS];//每年平均
        //float averagemon;
        //float averageyear;
        float allyear;
        const float rain[YEARS][MONTH]=  {
            {4.3,4.3,4.3,3.0,2.0,1.2,0.2,0.2,0.4,2.4,3.5,6.6},
            {8.5,8.2,1.2,1.6,2.4,0.0,5.2,0.9,0.3,0.9,1.4,7.3},
            {9.1,8.5,6.7,4.3,2.1,0.8,0.2,0.2,1.1,2.3,6.1,8.4},
            {7.2,9.9,8.4,3.3,1.2,0.8,0.4,0.0,0.6,1.7,4.3,6.2},
            {7.6,5.6,3.8,2.8,3.8,0.2,0.0,0.0,0.0,1.3,2.6,5.2},
        };
        
        printf(" Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  ");
        printf(" Nov  Dec\n");//%4.1f;
        maverage(monaver , rain);
        //showit(monaver , MONTH);
        
        return 0;
    }
     
    void maverage (float arr1[MONTH] , const float (*arr2)[MONTH])
    {
        float subit[MONTH]={0};
        int i , j;
        
        for(i = 0 ; i < MONTH  ; i++)
        {
            for(j = 0 ; j < YEARS ; j++)
            {
            //    printf("%.2f ",arr2[j][i]);
                subit[i] += arr2[j][i];
            }
           
            
            arr1[i] = subit[i] / YEARS;
            printf("%4.1f " , subit[i] / YEARS);
        }
    }
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 8月24日
  • 已采纳回答 8月16日
  • 创建了问题 8月16日

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀