tailieunhanh - Lecture Programming in C++ - Chapter 10: Multi-dimensional numeric arrays

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 –Multi-Dimensional 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]; Lesson Name of array Type of array Two sets of [ ] so two dimensional Number of elements 2*3 = 6 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 Lesson Remember – subscripts begin at zero! Initializing Can use braces to separate rows Lesson int c [4] [3] = {{ 1, 2, 3}, { 4, 5, 6}, { 7, 8, 9}, {10, 11, 12}}; Advantage is visual table! Initializing Lesson int c[4] [3] = {{1,2}, {4, 5, 6}, {7}, {10,11,12}}; If values left out of row, implicitly initialized to zero. c[2] [2] = 0 int c[ ] [3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10,11,12}}; If far left dimension size left blank, it is declared implicitly by values given – . | Chapter 10 –Multi-Dimensional 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]; Lesson Name of array Type of array Two sets of [ ] so two dimensional Number of elements 2*3 = 6 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 Lesson Remember – subscripts begin at zero! Initializing Can use braces to separate rows Lesson int c [4] [3] = {{ 1, 2, 3}, { 4, 5, 6}, { 7, 8, 9}, {10, 11, 12}}; Advantage is visual table! Initializing Lesson int c[4] [3] = {{1,2}, {4, 5, 6}, {7}, {10,11,12}}; If values left out of row, implicitly initialized to zero. c[2] [2] = 0 int c[ ] [3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10,11,12}}; If far left dimension size left blank, it is declared implicitly by values given – in this case 4 rows. Printing Usually use nested for loop Two dimensions, two loops First loop controls rows Second loop controls columns Lesson for (j = 0; j > num_rows >> num_cols; for (j = 0; j > a[j] [k]; } } Read number of columns and rows from file prior to array elements Storage of Two-Dimensional Arrays Fills row by row If have fewer elements, then storage to the right of those filled un-initialized elements Three and greater dimensions Far right subscript increments first Other subscripts increment in order from right to left Lesson Storage * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * array[5] [9] 1 2 3 4 * * * * * 2 4 6 8 * * * * * 3 5 7 9 * * * * * * * * * * * * * * * * * * * * * * * If file gave number of rows as 3 and .

TỪ KHÓA LIÊN QUAN