Multidimensional arrays
Download
1 / 17

Multidimensional Arrays - PowerPoint PPT Presentation


  • 340 Views
  • Updated On :

Multidimensional Arrays. Chapter 13. The plural of mon goose starts with a "p". Initializing a multidimensional array. 0 1 2 3 4. Processing by row. 0 1 2 3. Printing the contents of an array. Processing by column. Two dimensional Arrays.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Multidimensional Arrays' - Philip


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

The plural of mon goose starts with a p l.jpg
The plural of mongoose starts with a "p"

Initializing a multidimensionalarray

0 1 2 3 4

Processingby row

0

1

2

3

Printing thecontents ofan array

Processingby column


Two dimensional arrays l.jpg
Two dimensional Arrays

  • 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

0

1

2

3

Which cell isLocation (2,3) ?


Declaring two dimensional arrays l.jpg
Declaring Two Dimensional Arrays

0 1 2 3 4

0

1

2

3

  • Syntax: data_type array_name [row_dim][col_dim];

  • Example:

  • First element isint_table[0][0]

  • Last element isint_table[4][3]

int int_table [5][4];


Processing two d arrays l.jpg
Processing Two-D Arrays

  • Arrays processed in some pattern

    • random

    • along rows

    • along columns

    • whole array

  • We will use the declaration shown below:

int int_table [5][4];int row, col;


Processing two d arrays6 l.jpg
Processing Two-D Arrays

  • What does the routine below do with the array? What should we name the function?

total_a_row

0


Processing two d arrays7 l.jpg
Processing Two-D Arrays

  • What does this routine below do with the array? What should we name the function?

total_column

0


Processing two d arrays8 l.jpg
Processing Two-D Arrays

  • This function initializes an array.

  • Fill in the blanks with the correct identifiers

3

col

table

value


Printing a table l.jpg
Printing a Table

row

row

row

col

col

col

  • We must process each row, item by item

  • Which will be the inner loop?Which will be the outer loop?

  • The LCV of the inner loop must be the one which changes the most often

What goeshere?

endl


Passing arrays as parameters l.jpg
Passing Arrays as Parameters

  • Recall declaration for 1-D array

  • we didn’t specify the size in the brackets

    • we sent the size as a separate parameter

  • 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);


Passing 2 d arrays as parameters l.jpg
Passing 2-D Arrays as Parameters

  • For a 2-D array, declare

  • 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

  • Note that this could be for an array for ANY number of rows, but exactly 4 columns

  • 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);


Alternate 2 d array definition l.jpg
Alternate 2-D Array Definition

  • Think of the 2-D array as an array of arrays

  • Example

  • Each element of renters is an array of rent_pmts

typedef float rent_pmts [12];rent_pmts renters [6];

0 1 … 10 11

0

1

5


Multidimensional arrays13 l.jpg
Multidimensional Arrays

  • 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];


Multidimensional arrays14 l.jpg
Multidimensional Arrays

  • 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 itemsapt_pmt [bldg][tennant][apt]


Testing and debugging l.jpg
Testing and Debugging

  • Initialize all components of an array

    • no guarantee of what is there to start with

  • Use the same number of indeces as the declaration of array

  • Make sure indeces are in order

    • don’t reverse row and column references


Testing and debugging16 l.jpg
Testing and Debugging

  • Use meaningful identifiers for the array name and indeces

  • Double check upper and lower bounds on indeces

    • don’t walk off the edge of the array

  • When declaring multidimensional array as a formal parameter

    • must state sizes of all but first dimension


Testing and debugging17 l.jpg
Testing and Debugging

  • When calling function with array as parameter

    • sizes of multi-dim actual parameter must match exactly sizes of formal

  • Use typedef statement to define multi-dimensional array type

    • use this for actual and formal parameter declaration


ad