Presentation is loading. Please wait.

Presentation is loading. Please wait.

Engineering Problem Solving with C++ An Object Based Approach Chapter 7 Two-Dimensional Arrays and Matrices.

Similar presentations


Presentation on theme: "Engineering Problem Solving with C++ An Object Based Approach Chapter 7 Two-Dimensional Arrays and Matrices."— Presentation transcript:

1

2 Engineering Problem Solving with C++ An Object Based Approach Chapter 7 Two-Dimensional Arrays and Matrices

3 Two-Dimensional Arrays 6154 1192 7131 5812 row 0 row 1 row 2 row 3 Access elements using two offsets or subscripts, the first for the row and the second for the column Example: int twoDArr[4][3];// declares a 2-D array with 4 //rows and 3 columns cout << twoDArr[0][2]; // prints the value in row 0, col 2 // The value 4 in this example col 0col 1col 2

4 Assigning data to 2-D Arrays Initialization: char table[2][3]= { {'a','g','k'}, {'c', 'm', 'h'}}; Assignment using nested for loops for (int i=0; i<2; i++)// for every row for (int j=0; j > table[i][j]; 'a''g''k' 'c''m''h'

5 Printing 2-D Array for(int r=0; r<NROWS; r++) { for(int c=0; c<NCOLS; c++){ cout << twoDArr[r][c] << ‘ ‘;} cout << endl; } Note: prints space after each element and newline after each row.

6 Printing 2-D Array(formatted) for(int r=0; r<NROWS; r++) { cout << "--------------------------------\n| "; for(int c=0; c<NCOLS; c++) cout << twoDArr[r][c] << " | "; cout << endl; } cout << "--------------------------------\n| ";

7 Functions and 2-D Arrays Default is call-by reference when passing 2-D array to a function All sizes of the array, except the leftmost one, must be specified. The leftmost one may be [] Function prototype example: –int rowAverage(int Arr[][4], int whichRow); Array declaration in main: –int table [5][4]; Function invocation example: –Ave = rowAverage(table, 3);

8 Matrices Mathematical Matrix –can be implemented as 2-D array –be careful translating between C++ offsets and matrix subscripts.

9 Determinant Used in computing inverses of matrices and solving systems of simultaneous equations. Determinant of 2x2 matrix: Determinant of 3x3 matrix:

10 Transpose of a matrix Rows of original matrix become columns in the transposed matrix.

11 Matrix Addition and Subtraction Matrix addition (or subtraction) adds (or subtracts) corresponding elements of the two matrices producing a third matrix of the same size. The original two matrices must also be of the same size.

12 Practice! Given the following matrices, what is A+B

13 Practice! Given the following matrices, what is A+B

14 Matrix Multiplication A*B The number of columns in A must be the same as the number of rows in B. The resulting matrix has the number of rows in A and the number of columns in B. Element c i,j, where C=A*B, is:

15 Practice! Given the following matrices, what is A*B

16 Practice! Given the following matrices, what is A*B


Download ppt "Engineering Problem Solving with C++ An Object Based Approach Chapter 7 Two-Dimensional Arrays and Matrices."

Similar presentations


Ads by Google