Lecture32 - ComputerProgrammingITAC162. Todays Agenda...

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

View Full Document Right Arrow Icon
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
Background image of page 1

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

View Full DocumentRight Arrow Icon
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.
Background image of page 2
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)
Background image of page 3

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

View Full DocumentRight Arrow Icon
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”); }
Background image of page 4
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]);
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 Computer Programming I TA C162 .
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 24

Lecture32 - ComputerProgrammingITAC162. Todays Agenda...

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

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