Help with multidimensional arrays

This is a discussion on Help with multidimensional arrays within the C++ Programming forums, part of the General Programming Boards category; Hello everyone! I'm new to the forums and could use a little help.
Here's my problem...
I need to Name ...

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.

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?

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.