Download presentation
Presentation is loading. Please wait.
Published byHilary Fletcher Modified over 8 years ago
2
1 Multidimensional Arrays Chapter 13
3
2 The plural of mongoose starts with a "p" 0 1 2 3 4 01230123 Initializing a multidimensional array Processing by row Processing by column Printing the contents of an array
4
3 Two dimensional Arrays n A collection of components –all of the same type –structured in TWO dimensions –each component accessed by a PAIR of indices representing the component’s position in each dimension 0 1 2 3 4 01230123 Which cell is Location (2,3) ?
5
4 Declaring Two Dimensional Arrays Syntax: data_type array_name [row_dim][col_dim]; Syntax: data_type array_name [row_dim][col_dim]; n Example: n First element is int_table n First element is int_table[0][0] n Last element is int_table n Last element is int_table[4][3] 0 1 2 3 4 01230123 int int_table [5][4];
6
5 Processing Two-D Arrays n Arrays processed in some pattern –random –along rows –along columns –whole array n We will use the declaration shown below: int int_table [5][4]; int row, col;
7
6 Processing Two-D Arrays n What does the routine below do with the array? What should we name the function? 0 total_a_row
8
7 Processing Two-D Arrays n What does this routine below do with the array? What should we name the function? 0 total_column
9
8 Processing Two-D Arrays n This function initializes an array. n Fill in the blanks with the correct identifiers 3 col tablevalue
10
9 Printing a Table n We must process each row, item by item n Which will be the inner loop? Which will be the outer loop? n The LCV of the inner loop must be the one which changes the most often What goes here? row col endl
11
10 Passing Arrays as Parameters n Recall declaration for 1-D array n we didn’t specify the size in the brackets –we sent the size as a separate parameter n Recall name of the array is a pointer constant –tells where the array starts in memory –this is what is passed to the function void whatever ( float num_list [ ], int size);
12
11 Passing 2-D Arrays as Parameters n For a 2-D array, declare n We are sending the starting address –also how many elements it takes to jump us to the next row –that is -- the number of columns n Note that this could be for an array for ANY number of rows, but exactly 4 columns n As with 1-D, we also send another parameter for the function as a limiting value void whatever ( float num_table [ ][4], int num_rows);
13
12 Alternate 2-D Array Definition n Think of the 2-D array as an array of arrays n Example Each element of renters is an array of rent_pmts Each element of renters is an array of rent_pmts typedef float rent_pmts [12]; rent_pmts renters [6]; 01…501…5 0 1 … 10 11
14
13 Multidimensional Arrays n C++ arrays not limited to two dimensions –limitation is amount of memory const int NUM_BLDGS = 10; const int APTS_PER_BLDG = 12; float apt_pmts [NUM_BLDGS][6][APTS_PER_BLDG];
15
14 Multidimensional Arrays n C++ arrays not limited to two dimensions –limitation is amount of memory const int NUM_BLDGS = 10; const int APTS_PER_BLDG = 12; float apt_pmts [NUM_BLDGS][6][APTS_PER_BLDG]; This gives a 3-D array with 720 items apt_pmt [bldg][tennant][apt]
16
15 Testing and Debugging n Initialize all components of an array –no guarantee of what is there to start with n Use the same number of indeces as the declaration of array n Make sure indeces are in order –don’t reverse row and column references
17
16 Testing and Debugging n Use meaningful identifiers for the array name and indeces n Double check upper and lower bounds on indeces –don’t walk off the edge of the array n When declaring multidimensional array as a formal parameter –must state sizes of all but first dimension
18
17 Testing and Debugging n When calling function with array as parameter –sizes of multi-dim actual parameter must match exactly sizes of formal n Use typedef statement to define multi- dimensional array type –use this for actual and formal parameter declaration
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.