Lecture Programming in C++ - Chapter 10: Multi-dimensional numeric arrays. On completion of this chapter students will know how to: Initialize multidimensional arrays, print multidimensional arrays, read a file for array element values, visualize storage, pass an array to a function.
Chapter 10 –MultiDimensional Numeric Arrays Multidimensional Arrays Visual image of matrix or table for two dimensional array Subscript for each dimension Declared with value for each dimension int b[2] [3]; Number of elements Name of array 2*3 = 6 Two sets of [ ] so two dimensional Type of array Lesson 10.1 Initializing Initialized by row Example: int b[2] [3] ={51, 52, 53, 54, 55, 56}; Elements would be: b[0] [0] = 51 b[0] [1] = 52 b[0] [2] = 53 b[1] [0] = 54 b[1] [1] = 55 b[1] [2] = 56 Remember – subscripts begin at zero! Lesson 10.1 Initializing Can use braces to separate rows int c [4] [3] = {{ 1, 2, 3}, { 4, 5, 6}, { 7, 8, 9}, {10, 11, 12}}; Advantage is visual table! Lesson 10.1 Initializing int c[4] [3] = {{1,2}, int c[ ] [3] = {{1, 2, 3}, {4, 5, 6}, {4, 5, 6}, {7}, {7, 8, 9}, If far left dimension If values left out {10,11,12}}; size left blank, it is of row, implicitly {10,11,12}}; declared implicitly initialized to zero by values given – in this case 4 rows c[2] [2] = 0 Lesson 10.1 Printing for (j = 0; j