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 ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    13

    Question 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. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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];
    
    	}
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,487
    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?
    Last edited by Mario F.; 11-21-2006 at 02:19 AM.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    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. #5
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Lol practice makex perfect, and if you don't know the answer then simply ask
    I'm just trying to be a better person - My Name Is Earl

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    9
    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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimensional Arrays
    By jordanguyoflove in forum C Programming
    Replies: 4
    Last Post: 10-16-2008, 06:16 PM
  2. Multidimensional Arrays?
    By CreatedByShadow in forum C++ Programming
    Replies: 7
    Last Post: 01-13-2006, 09:35 PM
  3. Pointers to Multidimensional Arrays
    By kidburla in forum C Programming
    Replies: 10
    Last Post: 10-29-2005, 10:45 PM
  4. Multidimensional arrays in Korn shell
    By Yasir_Malik in forum Tech Board
    Replies: 3
    Last Post: 04-11-2004, 02:16 PM
  5. Passing multidimensional arrays to functions
    By maxthecat in forum C Programming
    Replies: 3
    Last Post: 12-22-2001, 02:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21