Electrical Engineering - Chapter 10: Multi-Dimensional Numeric 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];

ppt13 trang | Chia sẻ: thuongdt324 | Lượt xem: 542 | Lượt tải: 0download
Bạn đang xem nội dung tài liệu Electrical Engineering - Chapter 10: Multi-Dimensional Numeric Arrays, để tải tài liệu về máy bạn click vào nút DOWNLOAD ở trên
Chapter 10 –Multi-Dimensional Numeric ArraysMultidimensional ArraysVisual image of matrix or table for two-dimensional arraySubscript for each dimensionDeclared with value for each dimension int b[2] [3];Lesson 10.1Name of arrayType of arrayTwo sets of [ ] so two dimensionalNumber of elements 2*3 = 6InitializingInitialized by rowExample: 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] = 56Lesson 10.1Remember – subscripts begin at zero!InitializingCan use braces to separate rowsLesson 10.1int c [4] [3] = {{ 1, 2, 3}, { 4, 5, 6}, { 7, 8, 9}, {10, 11, 12}};Advantage is visual table!InitializingLesson 10.1int 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] = 0int 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 loopTwo dimensions, two loopsFirst loop controls rowsSecond loop controls columnsLesson 10.1for (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 elementsStorage of Two-Dimensional ArraysFills row by rowIf have fewer elements, then storage to the right of those filled un-initialized elementsThree and greater dimensionsFar right subscript increments firstOther subscripts increment in order from right to leftLesson 10.2Storage* * * * * * * * ** * * * * * * * ** * * * * * * * ** * * * * * * * ** * * * * * * * *array[5] [9]1 2 3 4 * * * * *2 4 6 8 * * * * *3 5 7 9 * * * * ** * * * * * * * ** * * * * * * * *If file gave number of rows as 3 and number of columns as 4 then have non-contiguous storage.Lesson 10.2Storage LocationFormula to locate arrays element's position in memoryarray element, e[x] [y] [z]array size e[I] [J] [K]sequence location = x * (J * K) + y * (K) + z +1array element, e[0] [1] [2]array size e[2] [3] [4]sequence location = 0 * (3 *4) + 1 * (4) + 2+1 = 7Lesson 10.2Passing Array to FunctionLesson 10.2void funcName (type, type, type[ ] [max_cols]);funcName (rows, cols, arrayName);void funcName (type r, type c, type array[ ] [max_cols]);DeclarationCallHeaderconst QualifierPlace in front of array name in function declaration and headerAssures that array is not modified in functionLesson 10.2void funcName (type, type, const type [ ] [ col ]);DeclarationSummaryInitialize multidimensional arraysPrint multidimensional arraysRead a file for array element valuesVisualize storagePass an array to a functionLearned how to: