# Lecture32 - Todays Agenda Derived Data types Arrays Two...

This preview shows pages 1–7. Sign up to view the full content.

1 Computer Programming I TA C162 . 31 Mar 2006 Today’s Agenda Derived Data types Arrays Two dimensional Arrays Examples Matrix Addition and Subtraction Matrix Multiplication Strings Declaration Initialization Printing Reading

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

View Full Document
2 Computer Programming I TA C162 . 31 Mar 2006 Working with two dimensional Arrays Matrix Addition and Subtraction Let A[m][n] and B[p][q] are two matrices. Precondition : m equals to p and n equals to q Algorithm Steps : 1. Read two matrices A and B and initialize C matrix to zero 2. Repeat (3) for i=0 to m-1 3. Repeat (3. a) for j=0 to n-1 3. a) C[i][j] = A[i][j] + B[i][j] 4. Display C matrix.
3 Computer Programming I TA C162 . 31 Mar 2006 Implementation #define ROW 10 #define COL 10 int main() { int M1[ROW][COL],M2[ROW][COL],M3[ROW][COL],i,j; int row1,col1,row2,col2; printf(“Enter row value for M1\n”); scanf(“%d”,&row1); printf(“Enter column value for M1\n”); scanf(“%d”,&col1); printf(“Enter row value for M2\n”); scanf(“%d”,&row2) printf(“Enter column value for M2\n”); scanf(“%d”,&col2)

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

View Full Document
4 Computer Programming I TA C162 . 31 Mar 2006 if(row1!=row2 || col1!=col2) { printf(“Invalid Input! Addition is not possible”); return; } printf(“Enter data for Matrix M1\n”); for(i=0;i<row1;i++) { for(j=0;j<col1;j++) { scanf(“%d”,&M1[i][j]); } printf(“\n”); }
5 Computer Programming I TA C162 . 31 Mar 2006 printf(“Enter data for Matrix M2\n”); for(i=0;i<row2;i++) { for(j=0;j<col2;j++) { scanf(“%d”,&M2[i][j]); } printf(“\n”); } printf(“Addition of Matrices is as follows\n”); for(i=0;i<row2;i++) for(j=0;j<col2;j++) M3[i][j]= M1[i][j] + M2[i][j]);

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

View Full Document
6 Computer Programming I TA C162 .
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 24

Lecture32 - Todays Agenda Derived Data types Arrays Two...

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

View Full Document
Ask a homework question - tutors are online