# Thread: Help with multidimensional arrays

1. ## Help with multidimensional arrays

Hello everyone! I'm new to the forums and could use a little help.

Here's my problem...

I need to Name and load the rainfall from the following table into an array. Then print the average rainfall for each month and the average rainfall per year.

Code:
```          Jan   Feb     Mar  Apr   May	  Jun	 Jul	 Aug  Sep    Oct   Nov	  Dec
1999	5	3	2	6	5	3	1	3	2	3	4	2
2000	2	2	1	7	4	2	4	2	2	2	2	2
2001	3	3	3	3	4	2	2	3	2	2	1	2```
Here's what I did so far...
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.

Your input and help is greatly appreciated!

Thank You!

2. Code:
```float month_sum[12] = {0,0,0,0,0,0,0,0,0,0,0,0};
float total = 0.0;

for(col=0; col<12; ++col)
{
for(row=0; row<3; ++row)
{
month_sum[col]= month_sum[col] + rainfall[row][col];
}
total += month_sum[col];

}```

3. It is best both for security reasons and clarity of code that you define your array with meaningful names for the dimensions:

Code:
```const int NUM_MONTHS = 12;
const int NUM_YEARS = 3;

int rainfall[NUM_YEARS][NUM_MONTHS] =
{
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
};```
Next you want to construct your loops also with more meaningful names. It will help you spot trends and possible optimizations too. Names like 'month' and 'year' are more easy for your brain to process than cols and rows, and is also more true to the matrix contents.

Code:
```for (int month = 0; month != NUM_YEARS; ++month) {
for (int year = 0; year != NUM_MONTHS; ++year) {
}
}```
With that you don't need to worry anymore if you are processing a column or a row. Your brain will immediatly recognize the words and your thought process for what goes inside the loops will be much easier.

So...

- you will want to sum all months in one year and then divide by the total of months in that year to get the average in one year.
- You will want to sum all years in one month and then divide by the total of years to get the average in one month.

Total of years and total of months have already been defined by those constant variables up there. That's another advantage of defining an array size with constants; i.e. you will find yourself using those names often and in a more clar way because, again, it's easier to understand NUM_YEARS that it is to understand 3.

Next...

Lets start by getting the sum of all months in an year:

Code:
```const int START_YEAR = 1999; // The matrix starting year

int sum_year = 0; // will hold the sum of months in one year

for (int year = 0; year != NUM_YEARS; ++year) { // for each year
for (int month = 0; month != NUM_MONTHS; ++month) { // for each month in that year
sum_year = sum_year + rainfall[year][month];    // add the months
}
// that year is finished. Output the average
std::cout << START_YEAR + year << " average: "; // remember that year starts at 0
std::cout << (sum_year / NUM_MONTHS) << std::endl;
}```
And you just have done the average of each year.

The rest I leave up to you. You can do another loop for the months or do the months inside that loop. Will it be easier now that you have clear names defined?

4. Thank you so much for your help!!! Got the program up and running in no time.

Mario - Great idea using clear names to define the different dimensions. I was having a very hard time figuring out how the loops were effecting the program and how to set them up so they would function the way I wanted them to. What a difference!!! Now everything is very clear and easy to understand! I wish my book would lay out examples as nice as this.

I can't thank you enough for your help and look forward to chatting with everyone as my knowledge of C++ expands.

Thanks Again!

5. Lol practice makex perfect, and if you don't know the answer then simply ask

6. avg rainfall per year
Code:
```for(int i =0;i<3;i++)
{
for(int j=0;j<12;j++)
{
yrsum =    rainfall[i][j];
}
cout<<yrsum/12;
}```