Presentation is loading. Please wait.

Presentation is loading. Please wait.

Engineering Problem Solving with C++, Etter/Ingber

Similar presentations


Presentation on theme: "Engineering Problem Solving with C++, Etter/Ingber"— Presentation transcript:

1 Engineering Problem Solving with C++, Etter/Ingber
Chapter 7 Two-Dimensional Arrays and Matrices Engineering Problem Solving with C++, second edition, J. Ingber

2 Two Dimensional Arrays
Declaration and Initialization Computation and Output Function Arguments Engineering Problem Solving with C++, second edition, J. Ingber

3 Declaration and initialization
Engineering Problem Solving with C++, second edition, J. Ingber

4 Declaration and Initialization
The declaration of a two-dimensional array requires a row size and a column size. A consecutive block of (row size)(*column size) memory locations are allocated. The name of the array holds the address of the first byte of memory. Engineering Problem Solving with C++, second edition, J. Ingber

5 Engineering Problem Solving with C++, second edition, J. Ingber
Example: //Declaration int data[2][3]; Memory Snapshot data ? ? ? ? ? ? Engineering Problem Solving with C++, second edition, J. Ingber

6 Engineering Problem Solving with C++, second edition, J. Ingber
Example: //Declaration int data[2][3]; row/column form: col 0 col 1 col 2 ? row 0 row 1 Engineering Problem Solving with C++, second edition, J. Ingber

7 Engineering Problem Solving with C++, second edition, J. Ingber
Example: //Declaration and Initialization double t[2][2] = { {0.1,0.2}{1.1, 1.2} }; Memory Snapshot t 0.1 0.2 1.1 1.2 row 0 0.1 0.2 1.1 1.2 row 1 col 0 col 1 Engineering Problem Solving with C++, second edition, J. Ingber

8 Two-Dimensional Arrays
A two dimensional array stores data as a collection of rows and columns. Each element of a two-dimensional array has a row position and a column position. To access an element in a two-dimensional array, you must specify the name of the array followed by: a row offset a column offset Engineering Problem Solving with C++, second edition, J. Ingber

9 Example: Input Using cin
Nested for loops are often used when inputting and assigning values to a two-dimensional array. Example: double table[RSIZE][CSIZE]; for (int i=0; i<RSIZE; ++i) //every row for (int j=0; j<CSIZE; ++j )//every col cin >> table[i][j]; Engineering Problem Solving with C++, second edition, J. Ingber

10 Engineering Problem Solving with C++, second edition, J. Ingber
Example: Assignment Example: int v[RSIZE][CSIZE]; for (int i=0; i<RSIZE; ++i) //every row for (int j=0; j<CSIZE; ++j )//every col v[i][j] = i+j; V 1 2 3 Engineering Problem Solving with C++, second edition, J. Ingber

11 Computation and output
computing an average formatted output function arguments Computation and output Engineering Problem Solving with C++, second edition, J. Ingber

12 Computation and Output
The for statement is useful when performing computations on arrays. The index variable of the for statement can be used as an array offset. Computations can be easily performed on an entire two-dimensional array, or on a sub set of the array. Engineering Problem Solving with C++, second edition, J. Ingber

13 Engineering Problem Solving with C++, second edition, J. Ingber
Computations Compute the average of an array with n rows and m columns. for(int i=0; i<n; ++i) { for(int j=0; j<m; ++j) sum+=array[i][j]; } average = sum/(n*m); Engineering Problem Solving with C++, second edition, J. Ingber

14 Engineering Problem Solving with C++, second edition, J. Ingber
Computations Compute the average of the nth row of a two-dimensional array with r rows and c columns. for(int col=0; col < c; ++col) { sum += array[n][col]; } rowAverage = sum/c; Engineering Problem Solving with C++, second edition, J. Ingber

15 Engineering Problem Solving with C++, second edition, J. Ingber
Modify! Modify the C++ statements on the previous slide to compute the average of the nth column. Engineering Problem Solving with C++, second edition, J. Ingber

16 Engineering Problem Solving with C++, second edition, J. Ingber
Output Two dimensional arrays are often printed in a row by row format, using nested for statements. When printing the row values of an array, be sure to print: whitespace between the values in a row. a newline character at the end of each row. Engineering Problem Solving with C++, second edition, J. Ingber

17 Engineering Problem Solving with C++, second edition, J. Ingber
Printing 2-D Array for(int r=0; r<NROWS; ++r) { for(int c=0; c<NCOLS; ++c) cout << twoDArr[r][c] << " "; } cout << endl; Engineering Problem Solving with C++, second edition, J. Ingber

18 Engineering Problem Solving with C++, second edition, J. Ingber
Functions Arguments 2-D arrays are always passed by reference. All dimensions of the array, except the leftmost one (row), must be specified. The leftmost one may be empty [] Function prototype example: int rowAverage(int Arr[][COLSIZE], int whichRow); Array declaration in main: int table [ROWSIZE][COLSIZE]; Function invocation example: avg = rowAverage(table, 3); Engineering Problem Solving with C++, second edition, J. Ingber


Download ppt "Engineering Problem Solving with C++, Etter/Ingber"

Similar presentations


Ads by Google