Introduction
In this lab, you will learn how to perform various Matrix operations such as Addition, Subtraction, and Multiplication on a given 2-D Array using C++ Programming. You will also learn how to manipulate and use arrays effectively.
In this lab, you will learn how to perform various Matrix operations such as Addition, Subtraction, and Multiplication on a given 2-D Array using C++ Programming. You will also learn how to manipulate and use arrays effectively.
Create a new file named matrix_operations.cpp
in the ~/project
directory. This can be done using the following command in the terminal.
touch ~/project/matrix_operations.cpp
The first step in our C++ program is to include the libraries required for Input/ Output operations and clearing the console. After that, set up the main function for your C++ program. You can use the following code as a template.
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
// Your code here
getch();
return 0;
}
In this step, we will create a case to perform Addition of 2 Matrices. The C++ program will ask the user to enter the order or dimensions of the two matrices (must be m*n
and o*p
).
If the dimensions of the matrices are not the same, the program will return an error message. Then enters the elements of the matrices and add them. The final code should look like the following.
//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;
In this step, we will create a case to perform Subtraction of 2 Matrices. The C++ program will ask the user to enter the order or dimensions of the two matrices (must be m*n
and o*p
).
If the dimensions of the matrices are not the same, the program will return an error message. Then enters the elements of the matrices and subtract them. The final code should look like the following.
//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;
In this step, we will create a case to perform Multiplication of 2 Matrices. The C++ program will ask the user to enter the order or dimensions of the two matrices (must be m*n
and o*p
).
If the dimensions of the matrices are not compatible, the program will return an error message. Then enters the elements of the matrices and multiply them. The final code should look like the following.
//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;
You can execute the matrix_operations.cpp
file by compiling it with the following command in the terminal:
g++ ~/project/matrix_operations.cpp -o matrix && ./matrix
You might have to replace matrix_operations.cpp
with the name you used for your C++ program file.
Assuming your code is giving expected output, you should be able to see a prompt asking for a case number (1 for Addition, 2 for Subtraction, and 3 for Multiplication). After you enter your choice, the program will ask you to input matrices based on the chosen operation.
Here is the complete code for our matrix_operations.cpp
program file.
#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;
}
In this lab, you have learned how to perform various Matrix operations on a given 2-D Array using C++ Programming. You have also learned how to manipulate and use arrays. You should now have a good understanding of how Matrices work in C++ Programming.