SlideShare a Scribd company logo
1 of 18
Download to read offline
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

Similar to Data Structure & Algorithms - Matrix Multiplication

Yasar University Linear Algebra Calculator
Yasar University Linear Algebra CalculatorYasar University Linear Algebra Calculator
Yasar University Linear Algebra CalculatorYusuf Yasin Kumbul
 
Matrix operations in MATLAB
Matrix operations in MATLABMatrix operations in MATLAB
Matrix operations in MATLABSaloni Singhal
 
Cse 121 presentation on matrix [autosaved]
Cse 121 presentation on matrix [autosaved]Cse 121 presentation on matrix [autosaved]
Cse 121 presentation on matrix [autosaved]Kanis Fatema Shanta
 
Data Visualization 2020_21
Data Visualization 2020_21Data Visualization 2020_21
Data Visualization 2020_21Sangita Panchal
 
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)Make Mannan
 
Arrays with Numpy, Computer Graphics
Arrays with Numpy, Computer GraphicsArrays with Numpy, Computer Graphics
Arrays with Numpy, Computer GraphicsPrabu U
 
R Programming.pptx
R Programming.pptxR Programming.pptx
R Programming.pptxkalai75
 
Raushan's MATLB PPT..pptx
Raushan's MATLB PPT..pptxRaushan's MATLB PPT..pptx
Raushan's MATLB PPT..pptxhmghj
 
Matlab for beginners, Introduction, signal processing
Matlab for beginners, Introduction, signal processingMatlab for beginners, Introduction, signal processing
Matlab for beginners, Introduction, signal processingDr. Manjunatha. P
 

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

Matlab1
Matlab1Matlab1
Matlab1
 
Mmt 001
Mmt 001Mmt 001
Mmt 001
 
Yasar University Linear Algebra Calculator
Yasar University Linear Algebra CalculatorYasar University Linear Algebra Calculator
Yasar University Linear Algebra Calculator
 
Matrix operations in MATLAB
Matrix operations in MATLABMatrix operations in MATLAB
Matrix operations in MATLAB
 
CP Handout#9
CP Handout#9CP Handout#9
CP Handout#9
 
Matlab ppt
Matlab pptMatlab ppt
Matlab ppt
 
C lab-programs
C lab-programsC lab-programs
C lab-programs
 
Python Manuel-R2021.pdf
Python Manuel-R2021.pdfPython Manuel-R2021.pdf
Python Manuel-R2021.pdf
 
Cse 121 presentation on matrix [autosaved]
Cse 121 presentation on matrix [autosaved]Cse 121 presentation on matrix [autosaved]
Cse 121 presentation on matrix [autosaved]
 
Data Visualization 2020_21
Data Visualization 2020_21Data Visualization 2020_21
Data Visualization 2020_21
 
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
 
Arrays with Numpy, Computer Graphics
Arrays with Numpy, Computer GraphicsArrays with Numpy, Computer Graphics
Arrays with Numpy, Computer Graphics
 
R Programming.pptx
R Programming.pptxR Programming.pptx
R Programming.pptx
 
Basic concepts in_matlab
Basic concepts in_matlabBasic concepts in_matlab
Basic concepts in_matlab
 
Raushan's MATLB PPT..pptx
Raushan's MATLB PPT..pptxRaushan's MATLB PPT..pptx
Raushan's MATLB PPT..pptx
 
Matlab for beginners, Introduction, signal processing
Matlab for beginners, Introduction, signal processingMatlab for beginners, Introduction, signal processing
Matlab for beginners, Introduction, signal processing
 
C Programming
C ProgrammingC Programming
C Programming
 
A02
A02A02
A02
 
R Programming Intro
R Programming IntroR Programming Intro
R Programming Intro
 
Basic Analysis using Python
Basic Analysis using PythonBasic Analysis using Python
Basic Analysis using Python
 

More from babuk110

Deployment Models-Internet of things Materials
Deployment Models-Internet of things MaterialsDeployment Models-Internet of things Materials
Deployment Models-Internet of things Materialsbabuk110
 
Data Structure & Algorithms - Operations
Data Structure & Algorithms - OperationsData Structure & Algorithms - Operations
Data Structure & Algorithms - Operationsbabuk110
 
Data Structure & Algorithms - Mathematical
Data Structure & Algorithms - MathematicalData Structure & Algorithms - Mathematical
Data Structure & Algorithms - Mathematicalbabuk110
 
Data Structure - Dynamic Memory Allocation
Data Structure - Dynamic Memory AllocationData Structure - Dynamic Memory Allocation
Data Structure - Dynamic Memory Allocationbabuk110
 
Data Structure & Algorithms - Introduction
Data Structure & Algorithms - IntroductionData Structure & Algorithms - Introduction
Data Structure & Algorithms - Introductionbabuk110
 
Data structure & Algorithms - Programming in C
Data structure & Algorithms - Programming in CData structure & Algorithms - Programming in C
Data structure & Algorithms - Programming in Cbabuk110
 
Data Structure & Algorithm - Self Referential
Data Structure & Algorithm - Self ReferentialData Structure & Algorithm - Self Referential
Data Structure & Algorithm - Self Referentialbabuk110
 

More from babuk110 (7)

Deployment Models-Internet of things Materials
Deployment Models-Internet of things MaterialsDeployment Models-Internet of things Materials
Deployment Models-Internet of things Materials
 
Data Structure & Algorithms - Operations
Data Structure & Algorithms - OperationsData Structure & Algorithms - Operations
Data Structure & Algorithms - Operations
 
Data Structure & Algorithms - Mathematical
Data Structure & Algorithms - MathematicalData Structure & Algorithms - Mathematical
Data Structure & Algorithms - Mathematical
 
Data Structure - Dynamic Memory Allocation
Data Structure - Dynamic Memory AllocationData Structure - Dynamic Memory Allocation
Data Structure - Dynamic Memory Allocation
 
Data Structure & Algorithms - Introduction
Data Structure & Algorithms - IntroductionData Structure & Algorithms - Introduction
Data Structure & Algorithms - Introduction
 
Data structure & Algorithms - Programming in C
Data structure & Algorithms - Programming in CData structure & Algorithms - Programming in C
Data structure & Algorithms - Programming in C
 
Data Structure & Algorithm - Self Referential
Data Structure & Algorithm - Self ReferentialData Structure & Algorithm - Self Referential
Data Structure & Algorithm - Self Referential
 

Recently uploaded

VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAbhinavSharma374939
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 

Recently uploaded (20)

VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog Converter
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 

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