0% found this document useful (0 votes)
115 views5 pages

Matrix Multiplication in C

The document describes matrix multiplication through an algorithm, pseudocode, and C program. It explains the steps to check that the number of columns in the first matrix equals the number of rows in the second matrix. It then shows how to read the matrix values, initialize the result matrix to 0, and calculate the result of multiplying each row of the first matrix with each column of the second and storing in the result. The program takes input matrices, performs the multiplication, and outputs the result matrix.

Uploaded by

userscrybd
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
115 views5 pages

Matrix Multiplication in C

The document describes matrix multiplication through an algorithm, pseudocode, and C program. It explains the steps to check that the number of columns in the first matrix equals the number of rows in the second matrix. It then shows how to read the matrix values, initialize the result matrix to 0, and calculate the result of multiplying each row of the first matrix with each column of the second and storing in the result. The program takes input matrices, performs the multiplication, and outputs the result matrix.

Uploaded by

userscrybd
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

MATRIX MULTIPLICATION

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

Read p,q for i=0;i<m; i++ Read m,n

for j=0; j<n; j++

d[i][j] = 0 if p==n M for (k = 0; k<p; k++)

d[i][j] = d[i][j] + a[i][j] * b[k][j] for i=0; i<p; i++

for j=0; j<q;j++

Read a[i][j] for i=0; i<m; i++

for j=0; j<n; j++

for i=0; i<m; i++

print d[i][j]

for l=0; j<n; j++ Courtesy Ms.P.Chitra, Assistant Professor Read b[i][j] Page 2 M

Print the no of rows

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

RESULT: The program is executed and the output is verified.

Courtesy Ms.P.Chitra, Assistant Professor

Page 4

MATRIX MULTIPLICATION

Courtesy Ms.P.Chitra, Assistant Professor

Page 5

You might also like