-moz-user-select:none; -webkit-user-select:none; -khtml-user-select:none; -ms-user-select:none; user-select:none;

Monday 17 August 2015

Matrix addition and subtraction in C++

You are well aware of the matrix addition and subtraction in Mathematics.

Matrix addition

Matrix addition

Matrix subtraction

Matrix subtraction

Now,we will perform the same operations in C++ using the concept of 2-d arrays.So,below is the source code of the program.

Source code

#include<iostream>
#include<conio.h>
using namespace std;
void addition();
void subtraction();

int mat1[2][2], mat2[2][2],mat3[2][2], i ,j, k, sum,ab,ch;
int main()
{
int choice;
{
x:
cout<<"\t"<<"\t"<<"MAIN MENU"<<"\t"<<"\t"<<endl<<endl<<endl;
cout<<"Press 1 for 2*2 matrix addition."<<endl<<endl;
cout<<"Press 2 for 2*2 matrix subtraction."<<endl<<endl;
cout<<"Press 3 to exit from the program."<<endl<<endl;
cout<<"Enter your choice:";
cin>>choice;
switch(choice)
{
case 1:
addition();
break;
case 2:
subtraction();
break;
case 3:
return 0;
default:
cout<<"Invalid choice !!"<<endl<<endl;
}

goto x;
}
}
void addition()
{

 cout<<"\nEnter values for first 2 x 2 matrix:\n";
           for ( i = 0 ; i <= 1 ; i++ )
                {
                   for (j = 0 ; j <= 1 ; j++ )
                        cin>>mat1 [i][j];
                }
                        cout<<"\n Enter values for second 2 x 2 matrix:\n";
                   for ( i = 0 ; i <= 1 ; i++ )
                        {
                            for ( j=0 ; j<=1 ; j++ )
                                   cin>>mat2[i][j];
                        }
                       cout<<"\n The first 2 x 2 matrix entered by you is:\n";
                            for ( i=0 ; i<=1 ; i++ )
                                 {
                                       for ( j=0 ; j<=1 ; j++ )
                                              cout<< mat1[i][j]<<"\t";
                                              cout<<endl<<endl;
                                 }
                                              cout<<"\n the second 2 x 2 matrix entered :\n";
                                       for ( i = 0 ; i <= 1 ; i++ )
                                            {
                                                for ( j = 0 ; j <= 1 ; j++ )
                                                       cout<< mat2[i][j]<<"\t";
                                                       cout<<endl<<endl;
                                            }
                                            for(i=0;i<=1;i++)
                                            {
                                            for(j=0;j<=1;j++)
                                            {
                                                                   mat3[i][j] = mat1[i][j] + mat2[i][j];
}
}
cout<<"The addition of matrices:"<<endl<<endl;
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
cout<<mat3[i][j]<<"\t";
cout<<endl<<endl;
}
}
void subtraction()
{
cout<<"\nEnter values for first 2 x 2 matrix:\n";
           for ( i = 0 ; i <= 1 ; i++ )
                {
                   for (j = 0 ; j <= 1 ; j++ )
                        cin>>mat1 [i][j];
                }
                        cout<<"\n Enter values for second 3 x 3 matrix:\n";
                   for ( i = 0 ; i <= 1 ; i++ )
                        {
                            for ( j = 0 ; j <= 1 ; j++ )
                                   cin>>mat2[i][j];
                        }
                       cout<<"\n The first 2 x 2 matrix entered by you is:\n"<<endl;
                            for ( i = 0 ; i <= 1 ; i++ )
                                 {
                                       for ( j = 0 ; j <= 1 ; j++ )
                                              cout<< mat1[i][j]<<"\t";
                                              cout<<endl<<endl;
                                 }
                                              cout<<"\n the second 2 x 2 matrix entered :\n"<<endl;
                                       for ( i = 0 ; i <= 1 ; i++ )
                                            {
                                                for ( j = 0 ; j <= 1 ; j++ )
                                                       cout<< mat2[i][j]<<"\t";
                                                       cout<<endl<<endl;
                                            }
                                            for(i=0;i<=1;i++)
                                            {
                                            for(j=0;j<=1;j++)
                                            mat3[i][j]=mat1[i][j]-mat2[i][j];
}
cout<<"The subtraction of matrices:"<<endl<<endl;
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
cout<<mat3[i][j]<<"\t";
cout<<endl<<endl;
}
}

Output

Matrix addition and subtraction


No comments:

Post a Comment