SlideShare a Scribd company logo
3
Most read
5
Most read
7
Most read
21CSC201J
DATA STRUCTURES AND
ALGORITHMS
UNIT-1
Topic : MATRIX
MULTIPLICATION
Matrix Multiplication
• A matrix is a grid that is used to store data in a structured format. It is often used with
a table, where the data is represented in horizontal rows and vertical columns. Matrices are
often used in programming languages and are used to represent the data in a graphical
structure.
• Once the order of the matrix is declared for the first and second matrix, then the elements
(input) for the matrices are needed to be entered by the user. If the order of the matrix is not
proportionate to each other, then the error message will be displayed which is implanted by
a programmer in the condition statement.
• In C programming matrix multiplications are done by using arrays, functions, pointers.
Therefore we are going to discuss an algorithm for Matrix multiplication along with the
flowchart, which can be used to write programming code for 3×3 matrix multiplication in a
high-level language. This detailed explanation will help you to analyze the working
mechanism of matrix multiplication and will help to understand how to write code.
Definition
Definition
The definition of matrix multiplication indicates a
row-by-column multiplication, where the entry in the ith row and jth
column of the product AB is obtained by multiplying the entries in the
ith row A of by the corresponding entries in the jth column of B and
then adding the results.
The general pattern for matrix multiplication is as follows.
Algorithm
Step 1: Start the Program.
Step 2: Enter the row and column of the first (a) matrix.
Step 3: Enter the row and column of the second (b) matrix
Step 4: Enter the elements of the first (a) matrix.
Step 5: Enter the elements of the second (b) matrix.
Step 6: Print the elements of the first (a) matrix in matrix form.
Step 7: Print the elements of the second (b) matrix in matrix form.
Step 8: Set a loop up to row.
Algorithm
Step 9: Set an inner loop up to the column.
Step 10: Set another inner loop up to the column.
Step 11: Multiply the first (a) and second (b) matrix and store the
element in the third matrix (c)
Step 12: Print the final matrix.
Step 13: Stop the Program.
Program for Matrix multiplication
#include<stdio.h>
#include<stdlib.h>
int main(){
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
system("cls");
printf("enter the number of row=");
scanf("%d",&r);
printf("enter the number of column=");
scanf("%d",&c);
printf("enter the first matrix element=n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
Program for Matrix multiplication (contd…)
printf("enter the second matrix element=n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("multiply of the matrix=n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
Program for Matrix multiplication (contd…)
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
//for printing result
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%dt",mul[i][j]);
}
printf("n");
}
return 0;
Output
Matrix multiplication using Dynamic Memory
Allocation
#include <stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int **a,**b,**c;
//int c[3][3];
int a_r,a_c,b_r,b_c;
int i,j,k;
again:
printf("nenter rows and columns for matrix one:");
scanf("%d%d",&a_r,&a_c);printf("nenter rows and columns for matrix two:");
scanf("%d%d",&b_r,&b_c);
if(a_c!=b_r )
{
printf("ncan not multiply");
goto again;
}/* allocate memory for matrix one */
a=(int **)malloc(sizeof(int *)*a_r);
for( i=0;i<a_r;i++)
{
a[i]=(int *) malloc(sizeof(int*)*a_c);
} /* allocate memory for matrix two */
b=(int **) malloc(sizeof(int)*b_r);
for( i=0;i<b_r;i++)
{
b[i]=(int *) malloc(sizeof(int*)*b_c);
} /* allocate memory for sum matrix*/
c=(int **) malloc(sizeof(int *)*a_r);
for( i=0;i<a_r;i++)
{
c[i]=(int *) malloc(sizeof(int)*b_c);
}
printf("n enter matrix one %d by %dn",a_r,a_c);
for(i=0;i<a_r;i++)
{
for(j=0;j<a_c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("n enter matrix two %d by %dn",b_r,b_c);
for(i=0;i<b_r;i++)
{
for(j=0;j<b_c;j++)
{
scanf("%d",&b[i][j]);
}
} /*initialize product matrix */
for(i=0;i<a_r;i++)
{
for(j=0;j<a_c;j++)
{
c[i][j]=0;
}
} /* multiply matrix one and matrix two */
for(i=0;i<a_r;i++)
{
for(j=0;j<a_c;j++)
{
for(k=0;k<a_r;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
} /* display result */
printf("n Product of matrix one and two isn");
for(i=0;i<a_r;i++)
{
for(j=0;j<a_c;j++)
{
printf("%dt",c[i][j]);
}
printf("n");
}
}
Output
enter rows and columns for matrix one: 3
3
enter rows and columns for matrix two:3
3
Enter matrix one 3 by 3
1 2 3
4 5 6
7 8 9
Output
Enter matrix two 3 by 3
1 2 3
4 5 6
7 8 9
Product of matrix one and two:
30 36 42
66 81 96
102 126 150

More Related Content

PPTX
Introduction of Xgboost
PPTX
Ensemble learning
PDF
Performance Evaluation for Classifiers tutorial
PPTX
Bioinformatics and Artificial Intelligence (AI) the interrelation between the...
PPTX
Planning in AI(Partial order planning)
PDF
Applications of stack
PDF
Topological Sorting (Decrease and Conquer)
Introduction of Xgboost
Ensemble learning
Performance Evaluation for Classifiers tutorial
Bioinformatics and Artificial Intelligence (AI) the interrelation between the...
Planning in AI(Partial order planning)
Applications of stack
Topological Sorting (Decrease and Conquer)

What's hot (20)

PPTX
Scan line method
PDF
A* Search Algorithm
PDF
Clocked Sequential circuit analysis and design
PPTX
Breadth First Search & Depth First Search
PPTX
Naive Bayes
PPT
Assembler design option
PPTX
Region based segmentation
PPT
Visible surface detection in computer graphic
PPTX
Graph traversals in Data Structures
PDF
All About Decoders DLD.
PPTX
PPTX
Image Sampling and Quantization.pptx
PPTX
Computer Graphics - Windowing and Clipping
PPTX
Nand and nor as a universal gates
PPT
Morphology gonzalez-woods
PPTX
XgBoost.pptx
PDF
I.BEST FIRST SEARCH IN AI
PPTX
Introdution and designing a learning system
PPTX
K-Nearest Neighbor(KNN)
PPT
Machine learning classification ppt.ppt
Scan line method
A* Search Algorithm
Clocked Sequential circuit analysis and design
Breadth First Search & Depth First Search
Naive Bayes
Assembler design option
Region based segmentation
Visible surface detection in computer graphic
Graph traversals in Data Structures
All About Decoders DLD.
Image Sampling and Quantization.pptx
Computer Graphics - Windowing and Clipping
Nand and nor as a universal gates
Morphology gonzalez-woods
XgBoost.pptx
I.BEST FIRST SEARCH IN AI
Introdution and designing a learning system
K-Nearest Neighbor(KNN)
Machine learning classification ppt.ppt
Ad

Similar to Data Structure & Algorithms - Matrix Multiplication (20)

PDF
Computer Science CS Project Matrix CBSE Class 12th XII .pdf
PDF
IRJET- Matrix Multiplication using Strassen’s Method
PPT
Matlab Basic Tutorial
DOCX
B61301007 matlab documentation
PDF
Introduction to Data Science With R Lab Record
PDF
2D array
PPTX
Arrays 2d Arrays 2d Arrays 2d Arrrays 2d
PPT
Matlab introduction
PPTX
MATLAB : Introduction , Features , Display Windows, Syntax, Operators, Graph...
PPT
Matlab1
PDF
Mmt 001
PPTX
Matrix introduction jsssssssssssssssssjsj
PPTX
class-1ggggggggggggggggggggggggggg7.pptx
PPTX
Yasar University Linear Algebra Calculator
PDF
CP PPT_Unit IV computer programming in c.pdf
PPTX
Matrix operations in MATLAB
PDF
CP Handout#9
PPTX
Matlab ppt
DOC
C lab-programs
Computer Science CS Project Matrix CBSE Class 12th XII .pdf
IRJET- Matrix Multiplication using Strassen’s Method
Matlab Basic Tutorial
B61301007 matlab documentation
Introduction to Data Science With R Lab Record
2D array
Arrays 2d Arrays 2d Arrays 2d Arrrays 2d
Matlab introduction
MATLAB : Introduction , Features , Display Windows, Syntax, Operators, Graph...
Matlab1
Mmt 001
Matrix introduction jsssssssssssssssssjsj
class-1ggggggggggggggggggggggggggg7.pptx
Yasar University Linear Algebra Calculator
CP PPT_Unit IV computer programming in c.pdf
Matrix operations in MATLAB
CP Handout#9
Matlab ppt
C lab-programs
Ad

More from babuk110 (7)

PPTX
Deployment Models-Internet of things Materials
PDF
Data Structure & Algorithms - Operations
PDF
Data Structure & Algorithms - Mathematical
PDF
Data Structure - Dynamic Memory Allocation
PDF
Data Structure & Algorithms - Introduction
PDF
Data structure & Algorithms - Programming in C
PDF
Data Structure & Algorithm - Self Referential
Deployment Models-Internet of things Materials
Data Structure & Algorithms - Operations
Data Structure & Algorithms - Mathematical
Data Structure - Dynamic Memory Allocation
Data Structure & Algorithms - Introduction
Data structure & Algorithms - Programming in C
Data Structure & Algorithm - Self Referential

Recently uploaded (20)

PPTX
Sustainable Sites - Green Building Construction
PPTX
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
Welding lecture in detail for understanding
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
Geodesy 1.pptx...............................................
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPT
Project quality management in manufacturing
PDF
ETO & MEO Certificate of Competency Questions and Answers
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
PDF
Structs to JSON How Go Powers REST APIs.pdf
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
Well-logging-methods_new................
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Sustainable Sites - Green Building Construction
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
CH1 Production IntroductoryConcepts.pptx
Welding lecture in detail for understanding
Operating System & Kernel Study Guide-1 - converted.pdf
Geodesy 1.pptx...............................................
OOP with Java - Java Introduction (Basics)
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
bas. eng. economics group 4 presentation 1.pptx
Project quality management in manufacturing
ETO & MEO Certificate of Competency Questions and Answers
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
Structs to JSON How Go Powers REST APIs.pdf
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Well-logging-methods_new................
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...

Data Structure & Algorithms - Matrix Multiplication

  • 2. Matrix Multiplication • A matrix is a grid that is used to store data in a structured format. It is often used with a table, where the data is represented in horizontal rows and vertical columns. Matrices are often used in programming languages and are used to represent the data in a graphical structure. • Once the order of the matrix is declared for the first and second matrix, then the elements (input) for the matrices are needed to be entered by the user. If the order of the matrix is not proportionate to each other, then the error message will be displayed which is implanted by a programmer in the condition statement. • In C programming matrix multiplications are done by using arrays, functions, pointers. Therefore we are going to discuss an algorithm for Matrix multiplication along with the flowchart, which can be used to write programming code for 3×3 matrix multiplication in a high-level language. This detailed explanation will help you to analyze the working mechanism of matrix multiplication and will help to understand how to write code.
  • 4. Definition The definition of matrix multiplication indicates a row-by-column multiplication, where the entry in the ith row and jth column of the product AB is obtained by multiplying the entries in the ith row A of by the corresponding entries in the jth column of B and then adding the results. The general pattern for matrix multiplication is as follows.
  • 5. Algorithm Step 1: Start the Program. Step 2: Enter the row and column of the first (a) matrix. Step 3: Enter the row and column of the second (b) matrix Step 4: Enter the elements of the first (a) matrix. Step 5: Enter the elements of the second (b) matrix. Step 6: Print the elements of the first (a) matrix in matrix form. Step 7: Print the elements of the second (b) matrix in matrix form. Step 8: Set a loop up to row.
  • 6. Algorithm Step 9: Set an inner loop up to the column. Step 10: Set another inner loop up to the column. Step 11: Multiply the first (a) and second (b) matrix and store the element in the third matrix (c) Step 12: Print the final matrix. Step 13: Stop the Program.
  • 7. Program for Matrix multiplication #include<stdio.h> #include<stdlib.h> int main(){ int a[10][10],b[10][10],mul[10][10],r,c,i,j,k; system("cls"); printf("enter the number of row="); scanf("%d",&r); printf("enter the number of column="); scanf("%d",&c); printf("enter the first matrix element=n"); for(i=0;i<r;i++) { for(j=0;j<c;j++) { scanf("%d",&a[i][j]); } }
  • 8. Program for Matrix multiplication (contd…) printf("enter the second matrix element=n"); for(i=0;i<r;i++) { for(j=0;j<c;j++) { scanf("%d",&b[i][j]); } } printf("multiply of the matrix=n"); for(i=0;i<r;i++) { for(j=0;j<c;j++) { mul[i][j]=0;
  • 9. Program for Matrix multiplication (contd…) for(k=0;k<c;k++) { mul[i][j]+=a[i][k]*b[k][j]; } } } //for printing result for(i=0;i<r;i++) { for(j=0;j<c;j++) { printf("%dt",mul[i][j]); } printf("n"); } return 0;
  • 11. Matrix multiplication using Dynamic Memory Allocation #include <stdio.h> #include<conio.h> #include<stdlib.h> void main() { int **a,**b,**c; //int c[3][3]; int a_r,a_c,b_r,b_c; int i,j,k; again: printf("nenter rows and columns for matrix one:");
  • 12. scanf("%d%d",&a_r,&a_c);printf("nenter rows and columns for matrix two:"); scanf("%d%d",&b_r,&b_c); if(a_c!=b_r ) { printf("ncan not multiply"); goto again; }/* allocate memory for matrix one */ a=(int **)malloc(sizeof(int *)*a_r); for( i=0;i<a_r;i++) { a[i]=(int *) malloc(sizeof(int*)*a_c); } /* allocate memory for matrix two */
  • 13. b=(int **) malloc(sizeof(int)*b_r); for( i=0;i<b_r;i++) { b[i]=(int *) malloc(sizeof(int*)*b_c); } /* allocate memory for sum matrix*/ c=(int **) malloc(sizeof(int *)*a_r); for( i=0;i<a_r;i++) { c[i]=(int *) malloc(sizeof(int)*b_c); } printf("n enter matrix one %d by %dn",a_r,a_c); for(i=0;i<a_r;i++) { for(j=0;j<a_c;j++) {
  • 14. scanf("%d",&a[i][j]); } } printf("n enter matrix two %d by %dn",b_r,b_c); for(i=0;i<b_r;i++) { for(j=0;j<b_c;j++) { scanf("%d",&b[i][j]); } } /*initialize product matrix */
  • 15. for(i=0;i<a_r;i++) { for(j=0;j<a_c;j++) { c[i][j]=0; } } /* multiply matrix one and matrix two */ for(i=0;i<a_r;i++) { for(j=0;j<a_c;j++) { for(k=0;k<a_r;k++) { c[i][j]=c[i][j]+a[i][k]*b[k][j]; } } } /* display result */
  • 16. printf("n Product of matrix one and two isn"); for(i=0;i<a_r;i++) { for(j=0;j<a_c;j++) { printf("%dt",c[i][j]); } printf("n"); } }
  • 17. Output enter rows and columns for matrix one: 3 3 enter rows and columns for matrix two:3 3 Enter matrix one 3 by 3 1 2 3 4 5 6 7 8 9
  • 18. Output Enter matrix two 3 by 3 1 2 3 4 5 6 7 8 9 Product of matrix one and two: 30 36 42 66 81 96 102 126 150