慢就是快123 2022-10-11 10:44 采纳率: 100%
浏览 999
已结题

C语言编译时出现expected expression before ‘float’报错

再练习C语言二维数组函数时,定义了求每年降水总量,年平均降水量以及月平均降水量三个函数,通过GCC编译时出现expected expression before ‘float’报错,调式很久也无法解决,特来求助。


#include<stdio.h>
#define MONTHS 12
#define YEARS 5
void year_total(float ar[][MONTHS],int years,int months);
void year_average(float ar[][MONTHS],int years,int months);
void month_average(float ar[][MONTHS],int years,int months);
int main(void)
{
 const float rain[YEARS][MONTHS]=
 {
         {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}
 };
 year_total(float rain[][MONTHS],5,12);
 year_average(float rain[][MONTHS],YEARS,MONTHS);
 month_average(float rain[][MONTHS],YEARs,MONTHS);
 return 0;
}

void year_total(float ar[][MONTHS],int years,int months)
{
 int n,k;
 float sum;
 for(n=0,sum=0.0;n<years;n++)
{
  for(k=0;k<months;k++)
  {
     sum+=ar[n][k];
     printf("THE %dyear is %lf inch\n",n,sum);
  }
  sum=0;
 }
}


void year_average(float ar[][MONTHS],int years,int months)
{
  int n,k;
 float sum;
 for(n=0,sum=0.0;n<years;n++)
 {
  for(k=0;k<months;k++)
     sum+=ar[n][k];
 }
 printf("The yearly average is %lf inch",sum/years);
}


void month_average(float ar[][MONTHS],int years,int months)
{
  int n,k;
 float sum;
 for(n=0,sum=0.0;n<years;n++)
 {
  for(k=0;k<months;k++)
     sum+=ar[n][k];
 }
 printf("The yearly average is %lf inch",sum/(years*months));
}

以下是报错截图:

img


求各位帮忙解答一下,实在是苦恼。

  • 写回答

1条回答 默认 最新

  • 伍六七0804 2022-10-11 10:53
    关注
    
     
    #include<stdio.h>
    #define MONTHS 12
    #define YEARS 5
    void year_total(const float ar[][MONTHS],int years,int months);
    void year_average(const float ar[][MONTHS],int years,int months);
    void month_average(const float ar[][MONTHS],int years,int months);
    int main(void)
    {
     const float rain[YEARS][MONTHS]=
     {
             {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}
     };
     year_total( rain,5,12);
     year_average( rain,YEARS,MONTHS);
     month_average( rain,YEARS,MONTHS);
     return 0;
    }
     
    void year_total(const float ar[][MONTHS],int years,int months)
    {
     int n,k;
     float sum;
     for(n=0,sum=0.0;n<years;n++)
    {
      for(k=0;k<months;k++)
      {
         sum+=ar[n][k];
         printf("THE %dyear is %lf inch\n",n,sum);
      }
      sum=0;
     }
    }
     
     
    void year_average(const float ar[][MONTHS],int years,int months)
    {
      int n,k;
     float sum;
     for(n=0,sum=0.0;n<years;n++)
     {
      for(k=0;k<months;k++)
         sum+=ar[n][k];
     }
     printf("The yearly average is %lf inch",sum/years);
    }
     
     
    void month_average(const float ar[][MONTHS],int years,int months)
    {
      int n,k;
     float sum;
     for(n=0,sum=0.0;n<years;n++)
     {
      for(k=0;k<months;k++)
         sum+=ar[n][k];
     }
     printf("The yearly average is %lf inch",sum/(years*months));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 10月19日
  • 已采纳回答 10月11日
  • 创建了问题 10月11日

悬赏问题

  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含