In programming we can create matrix, and do operations on them. In this program we are going to do some Addition and Subtraction Operations. Before we start writing we need to know how to do that operations. Now for example we have two matrix. A and B. Now see how to do that operation.
Now as we can see in above picture that we take a third matrix C in which we are going to put our values. Same rule applied for Subtraction also. Now let’s write the program
Now as we can see in above picture that we take a third matrix C in which we are going to put our values. Same rule applied for Subtraction also. Now let’s write the program
#include<iostream>
using namespace std;
main()
{
int i,j,a[40][40],b[40][40],c[40][40],d[40][40],lim;
cout<<"Enter the order of the matrix";
cin>>lim;
cout<<"Enter the value in matrix A \n";
for(i=1;i<=lim;i++)
{
for(j=1;j<=lim;j++)
{
cout<<"Enter the value in A["<<i<<"],["<<j<<"] ";
cin>>a[i][j];
}
}
cout<<"Enter the value in matrix B \n";
for(i=1;i<=lim;i++)
{
for(j=1;j<=lim;j++)
{
cout<<"Enter the value in B["<<i<<"],["<<j<<"] ";
cin>>b[i][j];
}
}
for(i=1;i<=lim;i++)
{
for(j=1;j<=lim;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
cout<<"After adding two matrix \n";
for(i=1;i<=lim;i++)
{
for(j=1;j<=lim;j++)
{
cout<<c[i][j]<<"\t";
}
cout<<"\n\n";
}
for(i=1;i<=lim;i++)
{
for(j=1;j<=lim;j++)
{
d[i][j]=a[i][j]-b[i][j];
}
}
cout<<"After subtracting two matrix \n";
for(i=1;i<=lim;i++)
{
for(j=1;j<=lim;j++)
{
cout<<d[i][j]<<"\t";
}
cout<<"\n\n";
}
}
In above program we calculate addition using array C and subtraction using array D. Now let’s see the outputusing namespace std;
main()
{
int i,j,a[40][40],b[40][40],c[40][40],d[40][40],lim;
cout<<"Enter the order of the matrix";
cin>>lim;
cout<<"Enter the value in matrix A \n";
for(i=1;i<=lim;i++)
{
for(j=1;j<=lim;j++)
{
cout<<"Enter the value in A["<<i<<"],["<<j<<"] ";
cin>>a[i][j];
}
}
cout<<"Enter the value in matrix B \n";
for(i=1;i<=lim;i++)
{
for(j=1;j<=lim;j++)
{
cout<<"Enter the value in B["<<i<<"],["<<j<<"] ";
cin>>b[i][j];
}
}
for(i=1;i<=lim;i++)
{
for(j=1;j<=lim;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
cout<<"After adding two matrix \n";
for(i=1;i<=lim;i++)
{
for(j=1;j<=lim;j++)
{
cout<<c[i][j]<<"\t";
}
cout<<"\n\n";
}
for(i=1;i<=lim;i++)
{
for(j=1;j<=lim;j++)
{
d[i][j]=a[i][j]-b[i][j];
}
}
cout<<"After subtracting two matrix \n";
for(i=1;i<=lim;i++)
{
for(j=1;j<=lim;j++)
{
cout<<d[i][j]<<"\t";
}
cout<<"\n\n";
}
}
Enter the order of the matrix2
Enter the value in matrix A
Enter the value in A[1],[1] 2
Enter the value in A[1],[2] 3
Enter the value in A[2],[1] 5
Enter the value in A[2],[2] 2
Enter the value in matrix B
Enter the value in B[1],[1] 1
Enter the value in B[1],[2] 4
Enter the value in B[2],[1] 3
Enter the value in B[2],[2] 6
After adding two matrix
3 7
8 8
After subtracting two matrix
1 -1
2 -4
Enter the value in matrix A
Enter the value in A[1],[1] 2
Enter the value in A[1],[2] 3
Enter the value in A[2],[1] 5
Enter the value in A[2],[2] 2
Enter the value in matrix B
Enter the value in B[1],[1] 1
Enter the value in B[1],[2] 4
Enter the value in B[2],[1] 3
Enter the value in B[2],[2] 6
After adding two matrix
3 7
8 8
After subtracting two matrix
1 -1
2 -4


0 comments:
Post a Comment