Latest Post

C++ | Multiplication of Matrices using 2d Arrays


Introduction
 

This program is to multiply two matrices. The program below is given. The program is extendable. Look at the extending it section in this post. Go enjoy the program.

Program to multiply two matrices :


 #include<iostream.h>  
 #include<conio.h>  
 void main()  
 {  
 //clear the screen.  
 clrscr();  
 //declare variable type int  
 int a[3][3],b[3][3],i,j,k,s;  
 //Input the numbers of first matix  
 cout<<"First Matrix"<<endl;  
 for(i=0;i<3;i++)  
 {  
 for(j=0;j<3;j++)  
 {  
 cout<<"Enter number :";  
 cin>>a[i][j];  
 }  
 }  
 //Input the numbers of second matix  
 cout<<"Second Matrix"<<endl;  
 for(i=0;i<3;i++)  
 {  
 for(j=0;j<3;j++)  
 {  
 cout<<"Enter number :";  
 cin>>b[i][j];  
 }  
 }  
 //display the multipication of matrices  
 cout<<"Multiplication is"<<endl;  
 for(i=0;i<3;i++)  
 {  
 for(j=0;j<3;j++)  
 {  
 for(k=0;k<3;k++)  
 s=s+a[i][k]*b[k][j];  
 cout<<s<<"/t";  
 s=0;  
 }  
 cout<<endl;  
 }  
 //get character  
 getch();  
 }  

Output :

First Matrix

Enter the number
1
Enter the number
1
Enter the number
1
Enter the number
1
Enter the number
1
Enter the number
1
Enter the number
1
Enter the number
1
Enter the number
1

Second Matrix

Enter the number
1
Enter the number
1
Enter the number
1
Enter the number
1
Enter the number
1
Enter the number
1
Enter the number
1
Enter the number
1
Enter the number
1

Multiplication is
3 3 3
3 3 3
3 3 3

How does it work
  1. You enter the number.
  2. The number is saved in respective array for first matrix.
  3. The number is saved in respective array for second matrix.
  4. Then numbers are multiplied and printed to form of matrix.
Extending it

The program can be extended by using more numbers and making the matrix more bigger. Go extend it.

Explanation.
  1. Include ‘iostream.h’ and ‘conio.h’ files.
  2. Add void main.
  3. Start program by first clearing the screen.
  4. Declare the variables as int (name them as you want.)
  5. Add the cout and cin of array.
  6. Add for loop to print the multiplication of matrices.
At the end

You learn creating the C++ program of Multiplication of matrices using 2d array. So now enjoy the program.

Please comment on the post and share it.
And like it if you liked.

SHARE THIS POST

Author: Robin Saxena
I m computer science student and i Interested in cs, c/c++ programming, java, html ,Photography , Music , and generally connecting with others.

0 comments :