Lecture132 2D array - Adding Matrices 12 45 78 01 3 6 9 2...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture 1 -- 1 Computer Science I - Martin Hardwick Adding Matrices 1 2 3 4 5 6 7 8 9 0 1 2 1 1 1 2 2 2 3 3 3 4 4 4 2 3 4 5 7 8 10 11 12 4 5 6 + = Matrix 1 Matrix 2 Sum of Matrices 1 and 2 Add corresponding elements of Matrix 1 and Matrix 2 to produce the Sum matrix. To add two matrices, they must have the same number of rows and columns.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Lecture 1 -- 2 Computer Science I - Martin Hardwick Adding Matrices (1) #include <iostream> using namespace std; // Function prototypes void readMatrix(int matrix[ ][100],  int rows, int cols); void addMatrix( int mat1[ ][100],  int mat2[ ][100],  int sum[ ][100],       int rows, int cols); int main() //PURPOSE: test readMatrix &  addMatrix //PRECONDITIONS:  none //POSTCONDITIONS: ret 0 if success { // First matrix int  matrix1[100][100]; // Second matrix int  matrix2[100][100];  We will represent a table / matrix / spreadsheet in a C++ program as a two-dimensional array. A two-dimensional array is declared similarly to a one- dimensional array, but you must specify the maximum number of rows and columns. In this program, we read two matrices and compute their sum in a third matrix. We need two functions: readMatrix – prompt the  user for the data to put in a  matrix addMatrix – sum two  matrices putting the result  in a third Remember: arrays are always passed by reference (you don’t
Background image of page 2
Lecture 1 -- 3 Computer Science I - Martin Hardwick Two-Dimensional Arrays Example: int tab[3][4]; The first subscript is the row number, starting at 0. The second subscript is the column number, starting at 0. All the elements of a two-dimensional array are the same type. An element in a two-dimensional array can be used anywhere an ordinary variable can be used. The subscripts can be expressions whose values are used to identify a particular array element. tab[0][3] tab[1][3] tab[2][3]
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Lecture 1 -- 4 Computer Science I - Martin Hardwick Adding Matrices (2) // Sum of matrices int  sum[100][100]; // Matrix size int rows, cols;  // Loop variables int i,j; // Read size of matrices cout << "Enter number of rows: "; cin >> rows; cout<<"Enter number of columns: "; cin >> cols; // Read matrices readMatrix(matrix1, rows, cols); readMatrix(matrix2, rows, cols); We may not use the entire array each time the program runs. we use the upper left  corner of the array variable rows tells us  how many rows we are  using variable cols tells us  how many columns we  are using When passing a two- dimensional array to a function, just use the name of the array without subscripts as the argument.
Background image of page 4
Lecture 1 -- 5 Computer Science I - Martin Hardwick Adding Matrices (3) // Add matrices addMatrix(matrix1, matrix2, sum, rows, cols); // Display sum of matrices cout << endl << "Sum of the matrices:" << endl; for (i=0; i<rows; i=i+1) { for (j=0; j<cols; j=j+1) { cout << "\t" << sum[i][j]; } cout << endl; } return 0; } When using two-dimensional arrays, it is common to use nested for loops to process all the elements in an array.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 22

Lecture132 2D array - Adding Matrices 12 45 78 01 3 6 9 2...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online