I have below code. My program is objectives to count yearly total rainfall by year 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. Please help to fix warning and incorrect output
- Compiler gives warning
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.
/*
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:
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
2010 32.4
2011 37.9
2012 49.8
2013 44.0
2014 32.9
CodePudding user response:
This array declaration
float rain[YEARS][MONTHS]= ...
is equivalent vto
float rain[5][12]= ...
That is each element of the array has 12 sub-elements. But you supplied 13 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},
^^^
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(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. 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. Maybe you need to output it in the program.
CodePudding user response:
Your first row contains 13 column which is supposed to be 12.
CodePudding user response:
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.
the 3rd element should be 4.3 instead of 4,3.
Edit :As mentioned by @Jonathan using spaces after each element will help finding such mistakes easier.
