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

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 如何删除这个虚拟音频
  • ¥50 hyper默认的default switch
  • ¥15 网站打不开,提示502 Bad Gateway
  • ¥20 基于MATLAB的绝热压缩空气储能系统代码咨询
  • ¥15 R语言建立随机森林模型出现的问题
  • ¥15 中级微观经济学,生产可能性边界问题
  • ¥15 TCP传输时不同网卡传输用时差异过大
  • ¥15 请各位看看我写的属于什么算法,或者有更正确的写法?
  • ¥15 html5 qrcode 扫描器
  • ¥15 爬取网页信息并保存需要完整代码