TK1914: C++ Programming Array II
Objective In this chapter you will explore how to manipulate data in a two-dimensional array. 2FTSM :: TK1914,
Two-Dimensional Arrays A collection of a fixed number of components arranged in two dimensions –All components are of the same type Declaration syntax: dataType arrayName[intexp1][intexp2]; where intexp1 and intexp2 are expressions yielding positive integer values, which specify number of rows and number of columns respectively Sometimes called matrices or tables 3FTSM :: TK1914,
mark [0] [1] [2] [3] [0][1][2][3][4] Example: int mark[4][5]; 4FTSM :: TK1914,
Initialization Like one-dimensional arrays –Two-dimensional arrays can be initialized when they are declared Example: –int mark [][] = { {87, 86, 88, 82, 89}, {64, 69, 60, 63, 66}, {91, 90, 94, 98, 93}, {77, 74, 75, 72, 70} }; mark [0] [1] [2] [3] [0][1][2][3][4] inner braces can be omitted 5FTSM :: TK1914,
Accessing Array Components The syntax to access a component of a two- dimensional array is: arrayName[indexexp1][indexexp2] where indexexp1 and indexexp2 are expressions yielding nonnegative integer values indexexp1 specifies the row position and indexexp2 specifies the column position 6FTSM :: TK1914,
Accessing Array Components Example: temp = mark[2][4] Example: find sum of elements in second row. row = 1; sumRow = 0; for (int col = 0; col < 5; col++) sumRow = sumRow + mark[row][col]; mark [0] [1] [2] [3] [0][1][2][3][4] FTSM :: TK1914,
Processing Two-Dimensional Arrays A two-dimensional array can be processed in three different ways: 1.Process the entire array 2.Process a particular row of the array, called row processing 3.Process a particular column of the array, called column processing 8FTSM :: TK1914,
Processing Two-Dimensional Arrays (continued) Each row and each column of a two-dimensional array is a one-dimensional array When processing a particular row or column of a two-dimensional array –we use algorithms similar to processing one- dimensional arrays 9FTSM :: TK1914,
prog09.9.cpp 10FTSM :: TK1914,
11FTSM :: TK1914,
12FTSM :: TK1914,
13FTSM :: TK1914,
Passing Two-Dimensional Arrays as Parameters to Functions Two-dimensional arrays can be passed as parameters to a function By default, arrays are passed by reference The base address, which is the address of the first component of the actual parameter, is passed to the formal parameter When declaring a two-dimensional array as a formal parameter –can omit size of first dimension, but not the second Number of columns must be specified 14FTSM :: TK1914,
15 #include using namespace std; const int NUMBER_OF_ROWS = 6; const int NUMBER_OF_COLUMNS = 5; void printMatrix(int matrix[][NUMBER_OF_COLUMNS], int NUMBER_OF_ROWS); void sumRows(int matrix[][NUMBER_OF_COLUMNS], int NUMBER_OF_ROWS); void largestInRows(int matrix[][NUMBER_OF_COLUMNS], int NUMBER_OF_ROWS); #include using namespace std; const int NUMBER_OF_ROWS = 6; const int NUMBER_OF_COLUMNS = 5; void printMatrix(int matrix[][NUMBER_OF_COLUMNS], int NUMBER_OF_ROWS); void sumRows(int matrix[][NUMBER_OF_COLUMNS], int NUMBER_OF_ROWS); void largestInRows(int matrix[][NUMBER_OF_COLUMNS], int NUMBER_OF_ROWS); Example 9-10 (pg 528) prog09.10.cpp FTSM :: TK1914,
16 int main() { int board[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS] = {{23, 5, 6, 15, 18}, {4, 16, 24, 67, 10}, {12, 54, 23, 76, 11}, {1, 12, 34, 22, 8}, {81, 54, 32, 67, 33}, {12, 34, 76, 78, 9}}; //Line 1 printMatrix(board, NUMBER_OF_ROWS); //Line 2 cout << endl; //Line 3 sumRows(board, NUMBER_OF_ROWS); //Line 4 cout << endl; //Line 5 largestInRows(board, NUMBER_OF_ROWS); //Line 6 } int main() { int board[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS] = {{23, 5, 6, 15, 18}, {4, 16, 24, 67, 10}, {12, 54, 23, 76, 11}, {1, 12, 34, 22, 8}, {81, 54, 32, 67, 33}, {12, 34, 76, 78, 9}}; //Line 1 printMatrix(board, NUMBER_OF_ROWS); //Line 2 cout << endl; //Line 3 sumRows(board, NUMBER_OF_ROWS); //Line 4 cout << endl; //Line 5 largestInRows(board, NUMBER_OF_ROWS); //Line 6 } FTSM :: TK1914,
Example: Matrix manipulation #include using namespace std; const int N = 3; void inputMatrix(int mat[][N], int n); void addMatrix(int mat1[][N], int mat2[][N], int mat3[][N], int n); void outputMatrix(int mat[][N], int n); void main() { int matrix1[N][N], matrix2[N][N], output[N][N]; cout << "Input matrix 1: " << endl; inputMatrix(matrix1, N); cout << "Input matrix 2: " << endl; inputMatrix(matrix2, N); addMatrix(matrix1, matrix2, output, N); cout << "Answer for matrix1 add matrix2: " << endl; outputMatrix(output, N); } #include using namespace std; const int N = 3; void inputMatrix(int mat[][N], int n); void addMatrix(int mat1[][N], int mat2[][N], int mat3[][N], int n); void outputMatrix(int mat[][N], int n); void main() { int matrix1[N][N], matrix2[N][N], output[N][N]; cout << "Input matrix 1: " << endl; inputMatrix(matrix1, N); cout << "Input matrix 2: " << endl; inputMatrix(matrix2, N); addMatrix(matrix1, matrix2, output, N); cout << "Answer for matrix1 add matrix2: " << endl; outputMatrix(output, N); } 17FTSM :: TK1914,
void inputMatrix(int mat[][N], int size) { int i, j; for (i = 0; i < size; i++) for (j = 0; j < size; j++) cin >> mat[i][j]; } void addMatrix(int mat1[][N], int mat2[][N], int mat3[][N], int size) { int i, j; for (i = 0; i < size; i++) for (j = 0; j < size; j++) mat3[i][j] = mat1[i][j] + mat2[i][j]; } void outputMatrix(int mat[][N], int size) { int i, j; for (i = 0; i < size; i++) { for (j = 0; j < size; j++) cout << mat[i][j]; cout << endl; } void inputMatrix(int mat[][N], int size) { int i, j; for (i = 0; i < size; i++) for (j = 0; j < size; j++) cin >> mat[i][j]; } void addMatrix(int mat1[][N], int mat2[][N], int mat3[][N], int size) { int i, j; for (i = 0; i < size; i++) for (j = 0; j < size; j++) mat3[i][j] = mat1[i][j] + mat2[i][j]; } void outputMatrix(int mat[][N], int size) { int i, j; for (i = 0; i < size; i++) { for (j = 0; j < size; j++) cout << mat[i][j]; cout << endl; } 18FTSM :: TK1914,
Exercise Write matrix manipulation functions as listed below: –substract two matrices –multiply two matrices –to calculate transpose of a matrix Write function calls in the main program to test your matrix manipulation functions 19FTSM :: TK1914,
Summary In a two-dimensional array, the elements are arranged in a table form To access an element of a two-dimensional array, you need a pair of indices: one for the row position and one for the column position In row processing, a two-dimensional array is processed one row at a time In column processing, a two-dimensional array is processed one column at a time 20FTSM :: TK1914,
Summary (continued) In row processing, a two-dimensional array is processed one row at a time In column processing, a two-dimensional array is processed one column at a time 21FTSM :: TK1914,