[英]C warning: excess elements in array initializer and program does not provide expected output
I have below code.我有以下代码。 My program is objectives to count yearly total rainfall by year 2010-2014.
我的计划是计算 2010-2014 年的年总降雨量。 I have declared two dimension array.
我已经声明了二维数组。 you can see in code snippet.However I have added 12 month for each year but compiler gives below warning.
您可以在代码片段中看到。但是我每年添加了 12 个月,但编译器给出了以下警告。 Please help to fix warning and incorrect output
请帮助修复警告和不正确的 output
multi_dimension_array.c:14:54: warning: excess elements in array initializer [-Wexcess-initializers]
{4.3,4.3,4,3,3.0,2.0,1.2,0.2,0.2,0.4,2.4,3.5,6.6},
second issue is that it does not gives expected output.第二个问题是它没有给出预期的 output。
/*
Program name: multi_dimension_array.c
Date: 2022-01-29-23:50:41
*/
#include<stdio.h>
#define MONTHS 12 // number of months in a year
#define YEARS 5 // number of years of data
int main()
{
float sub_total = 0;
float total = 0;
//initializing rainfall data for 2010-2014
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}, //warning at this line no.14
{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}
};
for(int i= 0;i<YEARS;i++)
{
for(int j =0; j<MONTHS;j++)
sub_total += rain[i][j];
printf("%d %f \n", (2010+i),sub_total);
total += sub_total; // total for all years
}
return 0;
}
Actual Program output:实际程序 output:
J2V1-MacBook-Air~ # cd "/Users/J2V1/Desktop/coding/c_code/mix/" && gcc multi_dimension_array.c -o multi_dimension_array && "/Users/J2V1/Desktop/coding/c_code/mix/"multi_dimension_array
multi_dimension_array.c:14:54: warning: excess elements in array initializer [-Wexcess-initializers]
{4.3,4.3,4,3,3.0,2.0,1.2,0.2,0.2,0.4,2.4,3.5,6.6},
^~~
1 warning generated.
2010 28.500002
2011 66.400009
2012 116.200005
2013 160.199997
2014 193.100021
Expected output预期 output
2010 32.4
2011 37.9
2012 49.8
2013 44.0
2014 32.9
This array declaration这个数组声明
float rain[YEARS][MONTHS]= ...
is equivalent vto相当于 vto
float rain[5][12]= ...
That is each element of the array has 12 sub-elements.也就是说,数组的每个元素都有 12 个子元素。 But you supplied 13 initializers
但是你提供了 13 个初始化器
{4.3,4.3,4,3,3.0,2.0,1.2,0.2,0.2,0.4,2.4,3.5,6.6},
^^^
It seems there is a typo in the initializer list and you mean初始化列表中似乎有一个错字,你的意思是
{4.3,4.3,4.3,3.0,2.0,1.2,0.2,0.2,0.4,2.4,3.5,6.6},
^^^
Also in this nested for loops同样在这个嵌套的for循环中
for(int i= 0;i<YEARS;i++)
{
for(int j =0; j<MONTHS;j++)
sub_total += rain[i][j];
printf("%d %f \n", (2010+i),sub_total);
total += sub_total; // total for all years
}
you need reassign sub_total in the outer loop.您需要在外循环中重新分配 sub_total 。 SO the loops should look like
所以循环应该看起来像
for(int i= 0;i<YEARS;i++)
{
sub_total = 0.0f;
for(int j =0; j<MONTHS;j++)
sub_total += rain[i][j];
printf("%d %f \n", (2010+i),sub_total);
total += sub_total; // total for all years
}
Pay attention to that the value of the variable total
in fact is not used.注意变量
total
的值实际上并没有被使用。 Maybe you need to output it in the program.也许你需要在程序中 output 它。
The warning is due to {4.3,4.3,4,3,3.0,2.0,1.2,0.2,0.2,0.4,2.4,3.5,6.6}
having 13 elements instead of the intended 12 elements.警告是由于
{4.3,4.3,4,3,3.0,2.0,1.2,0.2,0.2,0.4,2.4,3.5,6.6}
有 13 个元素而不是预期的 12 个元素。 the 3rd element should be 4.3 instead of 4,3.第三个元素应该是 4.3 而不是 4,3。
Edit:As mentioned by @Jonathan using spaces after each element will help finding such mistakes easier.编辑:正如@Jonathan 所提到的,在每个元素后使用空格将有助于更容易地发现此类错误。
Your first row contains 13 column which is supposed to be 12.您的第一行包含 13 列,应该是 12。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.