1 / 25

Multidimensional Arrays

Multidimensional Arrays. Array. Conceptual: List of objects indexed by a number What if we want more than one index??? Row / Col. Two-Dimensional Arrays. Two-Dimensional Array Logical matrix Declare: t ype identifier[ rows ][ cols ]; Rows then columns int scores[ 5 ][ 3 ];.

jaspero
Download Presentation

Multidimensional Arrays

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Multidimensional Arrays

  2. Array • Conceptual: • List of objects indexed by a number • What if we want more than one index??? • Row / Col

  3. Two-Dimensional Arrays • Two-Dimensional Array • Logical matrix • Declare: type identifier[rows][cols]; • Rows then columns int scores[5][3];

  4. Initialization • Initialize as list of lists: intnums[3][4] =  {{19,22,31,42}, {50,61,32,83},  {93,47,15,66}}; • Zero out entire array: intnums[3][4] =  {{0}};

  5. 2D Access • Access: scores[row][col] scores[4][0] = 10; cout << scores[2][1]; //outputs 93

  6. Storage • 2D arrays stored internally in row major order • First dimension is start address of row scores[1][??]

  7. Traversals • Traverse with nested loops • Row index/col index • Loop order matters

  8. Order: 0 0 0 1 0 2 1 0 1 1 1 2

  9. Loop Samples • i, j aren't great, but get used to them • Use constants for loop conditions

  10. Single Dimension Traversals • Traversing one row or column requires one loop, one hardcoded index

  11. Sum all columns • Sum all columns: • Column is main loop, rows second:

  12. Sum Rows And Columns • Use arrays of row totals / col totals to build all sums

  13. Sum Rows And Columns • Use arrays of row totals / col totals to build all sums

  14. Sum Rows And Columns • Use arrays of row totals / col totals to build all sums

  15. Sum Rows And Columns • Use arrays of row totals / col totals to build all sums

  16. Sum Rows And Columns • Use arrays of row totals / col totals to build all sums

  17. Passing Arrays • Must specify each dimension after first whenpassed as parameter

  18. Storage • 2D arrays stored internally in row major order • First dimension is start address of row scores[1][??]

  19. Passing Arrays • Compiles, but BAD

  20. Passing Arrays • Specify columns using global constant:

  21. Passing Arrays • Same using defined global constants: • Don't need to pass number rows • Still need to specify array second dimension

  22. Faking 2D • Can fake 2D with a 1D array • [row][col]  [colwidth*row][col] scores[1][2] scores[3*1 + 2]

  23. Multidimensional Arrays • Can have an arbitrary number of dimensions:

More Related