Code:
#include<iostream.h>
int main()
{
int rainfall[3][12] = {5, 3, 2, 6, 5, 3, 1, 3, 2, 3, 4, 2,
2, 2, 1, 7, 4, 2, 4, 2, 2, 2, 2, 2,
3, 3, 3, 3, 4, 2, 2, 3, 2, 2, 1, 2};
int row, col,
float jan=0.0, feb=0.0, mar=0.0, apr=0.0, may=0.0, jun=0.0, jul=0.0, aug=0.0, sep=0.0, oct=0.0, nov=0.0, dec=0.0;
float sum = 0.0, total_rainfall, avg_totalrainfall, avg_jan;
for(col=0; col<12; ++col)
{
for(row=0; row<3; ++row)
{
sum = sum + rainfall[row][col];
}
if(col==0) jan = sum;
else if(col==1) feb = sum;
else if(col==2) mar = sum;
else if(col==3) apr = sum;
else if(col==4) may = sum;
else if(col==5) jun = sum;
else if(col==7) aug = sum;
else if(col==8) sep = sum;
else if(col==9) oct = sum;
else if(col==10)nov = sum;
else dec = sum;
sum = 0.0;
}
total_rainfall = jan+feb+mar+apr+may+jun+jul+aug+sep+oct+nov+dec;
avg_totalrainfall = total_rainfall/36;
avg_jan = jan/3;
//continue avg for all months
cout << "\nThe total rainfall is:" << total_rainfall;
cout << "\nThe average rainfall is:" << avg_totalrainfall;
cout << "\nThe average rainfall for January is:" << avg_jan;
return 0;
}
I know there is a much easier way but I can't seem to come up with the proper loop to print avg rainfall per month and avg rainfall per year.