Studytonight is now part of the GUVI universe. Explore GUVI →
🤩 New Cool Developer Tools for you. Explore →
FREE JavaScript Video Series Start Learning →
Signup/Sign In
Tests
MCQs to test your knowledge.
Compilers
Compilers to execute code in browser.
Index
LAST UPDATED: DECEMBER 4, 2020

Program to perform various Matrix operation on a given 2-D Array

Following is the program to perform various Matrix operation on a given 2-D Array.

#include<iostream.h>
#include<conio.h>
int main()
{
 clrscr();
 int a[50][50],b[50][50],c[50][50],i,j,m,n,o,p,l=1,r;
 cout<<"Enter case:\n";
 cin>>r;
 switch(r)
 {
 //ADDITION
 case 1:
 cout<<"\nEnter the order of matrix a (must be m*n): "<<endl;
 cin>>m;
 cout<<"* \n";
 cin>>n;
 cout<<"Enter the order of matrix b (must be o*p): "<<endl;
 cin>>o;
 cout<<"* \n";
 cin>>p;
 if (m==o&&n==p)
 {
 cout<<"Addition possible "<<endl;
 }
 else
 {
 cout<<"Addition not possible ";
 l=0;
 }
 
 if(l)
 {
 cout<<"\n\nEnter the elements of matrix 1: "<<endl;
 for (i=1;i<=m;i++)
 {
 for (j=1;j<=n;j++)
 cin>>a[i][j];
 }
 cout<<"Elements of matrix 1 are: ";
 for (i=1;i<=m;i++)
 {
 cout<<endl;
 for (j=1;j<=n;j++)
 cout<<a[i][j]<<" ";
 }
 cout<<"\nEnter the elements of matrix 2: "<<endl;
 for (i=1;i<=o;i++)
 {
 for (j=1;j<=p;j++)
 cin>>b[i][j];
 }
 cout<<"Elements of matrix 2 are: ";
 for (i=1;i<=o;i++)
 {
 cout<<endl;
 for (j=1;j<=p;j++)
 cout<<b[i][j]<<" ";
 }
 cout<<"\n\n\nAddition:\nc=a+b=";
 for (i=1;i<=m;i++)
 {
 for (j=1;j<=n;j++)
 {
 c[i][j]=a[i][j]+b[i][j];
 }
 }
 for (i=1;i<=m;i++)
 {
 cout<<endl;
 for (j=1;j<=n;j++)
 cout<<c[i][j]<<" ";
 }
 }
 break;
 
 //SUBTRACTION
 case 2:
 cout<<"\nEnter the order of matrix a (must be m*n): "<<endl;
 cin>>m;
 cout<<"* \n";
 cin>>n;
 cout<<"Enter the order of matrix b (must be o*p): "<<endl;
 cin>>o;
 cout<<"* \n";
 cin>>p;
 if (m==o&&n==p)
 {
 cout<<"Subtracion possible "<<endl;
 }
 else
 {
 cout<<"Subtraction not possible ";
 l=0;
 }
 
 if(l)
 {
 cout<<"\n\nEnter the elements of matrix 1: "<<endl;
 for (i=1;i<=m;i++)
 {
 for (j=1;j<=n;j++)
 cin>>a[i][j];
 }
 cout<<"Elements of matrix 1 are: ";
 for (i=1;i<=m;i++)
 {
 cout<<endl;
 for (j=1;j<=n;j++)
 cout<<a[i][j]<<" ";
 }
 cout<<"\nEnter the elements of matrix 2: "<<endl;
 for (i=1;i<=o;i++)
 {
 for (j=1;j<=p;j++)
 cin>>b[i][j];
 }
 cout<<"Elements of matrix 2 are: ";
 for (i=1;i<=o;i++)
 {
 cout<<endl;
 for (j=1;j<=p;j++)
 cout<<b[i][j]<<" ";
 }
 cout<<"\n\n\nSubtraction:\nc=a-b=";
 for (i=1;i<=m;i++)
 {
 for (j=1;j<=n;j++)
 {
 c[i][j]=a[i][j]-b[i][j];
 }
 }
 for (i=1;i<=m;i++)
 {
 cout<<endl;
 for (j=1;j<=n;j++)
 cout<<c[i][j]<<" ";
 }
 }
 break;
 
 //MULTIPLICATION
 case 3:
 cout<<"\nEnter the order of matrix a (must be m*n): "<<endl;
 cin>>m;
 cout<<"* \n";
 cin>>n;
 cout<<"Enter the order of matrix b (must be o*p): "<<endl;
 cin>>o;
 cout<<"* \n";
 cin>>p;
 if (n==o)
 {
 cout<<"Multiplication possible "<<endl;
 }
 else
 {
 cout<<"Multiplication not possible ";
 l=0;
 }
 
 if(l)
 {
 cout<<"\n\nEnter the elements of matrix 1: "<<endl;
 for (i=1;i<=m;i++)
 {
 for (j=1;j<=n;j++)
 cin>>a[i][j];
 }
 cout<<"Elements of matrix 1 are: ";
 for (i=1;i<=m;i++)
 {
 cout<<endl;
 for (j=1;j<=n;j++)
 cout<<a[i][j]<<" ";
 }
 cout<<"\nEnter the elements of matrix 2: "<<endl;
 for (i=1;i<=o;i++)
 {
 for (j=1;j<=p;j++)
 cin>>b[i][j];
 }
 cout<<"Elements of matrix 2 are: ";
 for (i=1;i<=o;i++)
 {
 cout<<endl;
 for (j=1;j<=p;j++)
 cout<<b[i][j]<<" ";
 }
 cout<<"\n\n\nMultiplication:\nc=aXb=";
 for (i=1;i<=m;i++)
 {
 for (j=1;j<=p;j++)
 {
 c[i][j]=0;
 for (int k=1;k<=n;k++)
 {
 c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
 }
 }
 }
 for (i=1;i<=m;i++)
 {
 cout<<endl;
 for (j=1;j<=p;j++)
 cout<<c[i][j]<<" ";
 }
 }
 break;
 // default case
 default:
 cout<<"Wrong choice";
 }
 getch();
 return 0;
}


Enter case:
3
Enter the order of matrix a (must be m*n):
2
*
3
Enter the order of matrix b (must be o*p):
3
*
4
Multiplication possible
Enter the elements of matrix 1:
1 2 3 4 5 6
Elements of matrix 1 are:
1 2 3
4 5 6
Enter the elements of matrix 2:
1 2 3 4 5 6 7 8 9 10 11 12
Elements of matrix 2 are:
1 2 3 4
5 6 7 8
9 10 11 12
Multiplication:
c=aXb=
38 44 50 56
83 98 113 128



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.

Learn to Code
Learn and practice coding side-by-side.
NEW
C language Course
115+ coding exercises
Javascript Course
85+ coding exercises

AltStyle によって変換されたページ (->オリジナル) /