Matrix Multiplication in C
Matrix Multiplication in C
EX. NO : DATE : MATRIX MULTIPLICATION AIM: To write a program to perform matrix multiplication. ALGORITHM: Step-1 Start Step-2 Read rows and columns limits for matrices m,n,p,q Step-3 Check p is equal to n else go to step-12 Step-4 Set a loop to get A matrix values Step-5 Read matrix value a[i][j] Step-6 Set a loop to get B matrix values Step-7 Read matrix value b[i][j] Step-8 Repeat step-9 until i<m, j<n, k<p Step-9 d[i][j]=0: d[i][j]=d[i][j] + a[i][k] * b[k][j] Step-10 Set a loop to print matrix values Step-11 Print matrix values d[i][j] go to step-13 Step-12 Print the number of rowa and columns should not be equal. Step-13 Stop PSEUDOCODE: 1. BEGIN the program by reading the rows and columns limits of matrices m,n,p,q. 2. CHECK p is equal to n else PRINT the number of rows and columns should not be equal. 3. SET a loop to get A and B matrix values.
Courtesy Ms.P.Chitra, Assistant Professor Page 1
MATRIX MULTIPLICATION
4. READ matrix value a[i][j] and b[i][j]. 5. PRINT matrix value d[i][j]. 6. TERMINATE the program. FLOW CHART
start c
print d[i][j]
for l=0; j<n; j++ Courtesy Ms.P.Chitra, Assistant Professor Read b[i][j] Page 2 M
MATRIX MULTIPLICATION
PROGRAM: #include<stdio.> #include<conio.h> void main() { int a[10][10], b[10][10], d[10][10]; int i,j,p,q,m,n,k; clrscr(); printf(\n Enter the size of the A matrix: ); scanf(%d %d,&p,&q); printf(\n Enter the size of the B matrix:); scanf(%d %d ,&m,&n); if(p==n) { printf(\n enter the elements of A matrix.); for(i=0; i<p;i++) { for(j=0; j<q; j++) scanf(%d,&a[i][j]); } printf(\n enter the elements of B matrix.); for(i=0; i<m; i++) { for(j=0; j<n; j++) scanf(%d,&b[i][j]); } for(i=0; i<m; i++) { for(j=0; j<n; j++) { d[i][j]=0; for(k=0; k<p; k++) d[i][j] = d[i][j]+a[i][j] * b[k][j]); }} printf(\n Multiplication of A and B matrix: );
Courtesy Ms.P.Chitra, Assistant Professor Page 3
MATRIX MULTIPLICATION
for(i=0; i<m; i++) { for(j=0; j<n; j++) printf(%5d,d[i][j]); printf(\n); }} else printf(\n The no. rows and columns should not be equal); getch(); } OUTPUT: Enter size of A matrix : 2 2 Enter size of B matrix: 2 2 Enter the elements of A matrix. 1234 Enter the elements of B matrix. 1234 Multiplication of A and B matrix: 7 15 10 22
Page 4
MATRIX MULTIPLICATION
Page 5