C++| adjoint of 2X2 and 3X3 matrix
Hello peeps! Lord Hypersonic greets you. Today I am presenting a new c++ program that can calculate adjoint of both types of the matrix (i.e 2x2 and 3x3 matrix).
It uses a simple technique to calculate the adjoint, which we use in maths to calculate adjoints. Header files included:-
- iostream: For using input and output stream (cout as output stream OR for printing output on the screen, and cin as input stream OR for taking data from user).
- conio.h: For using _getch() function (it is a function to take input from the user as a single character).
Variables used:-
- A2 (a 2D array of 2x2) of integer data-type.
- AD2 (a 2D array of 2x2) of integer data-type.
- C2 ( a 2D array of 2x2) of integer data-type.
- A3 (a 2D array of 3x3) of integer data-type.
- AD3 (a 2D array of 3x3) of integer data-type.
- C3 (a 2D array of 3x3) of integer data-type.
- ch of integer data-type.
- i of integer data-type (for different for loops, the scope is different).
Working:-
- First, the user is asked to provide the order of the matrix, for which he/she want to find adjoint. The input for this given by the user is stored in ch.
- If the input provided by the user is 1 (which is for the 2x2 matrix), the user is asked to provide elements of the 2x2 matrix. The elements of the matrix provided by the user are stored in A2.
- After taking elements of the matrix, cofactors of a given matrix is calculated. For a 2x2 matrix, cofactors can be calculated like this:-
for example, the given matrix is :
A = | 1 2 |
| 3 4 |
Then cofactors (C) will be :
C= | 4 -3 |
| -2 1 |
For common, cofactors for 2x2 matrix is calculated as, C[i][j]=(-1)(i+j)*(element of A at another end of diagonal). The cofactos calculated is stored in C2. - After cofactors are calculated, we calculate the adjoint of a matrix. Adjoint is actually a matrix in which the elements in the rows of cofactors will be in the stored in adjoint matrix AD2 as columns. For example:-
C = | 4 -3 |
| -2 1 |
then adjoint is:-
J = | 4 -2 |
| -3 1 | - After calculating Adjoint AD2, it prints the elements of AD2.
- This is how we calculate adjoint of a 2x2 matrix. But if the user enters 2 instead of 1 in ch, which means he wants to find the adjoint of a 3x3 matrix, then calculations will be like this.
- Before proceeding, I am assuming you know how to find minors of a 3x3 matrix and how to find cofactors using minors.
- First, the user will be asked to enter the elements of the 3x3 matrix for which they want to find adjoint. Input will be stored in A3.
- After taking input, minors are calculated. After finding minors, cofactors are calculated in C3. Formula to calculating cofactors is Cij = (-1)(i+j)* Mij
In this program, I am directly using this formula to find cofactors in both cases. - After calculating cofactors, We find adjoint in the same manner as we have calculated in a 2x2 matrix and will be stored in AD3.
- After adjoint AD3 is calculated. Print the elements of AD3.
Source Code:-
/*
Program: an Adjoint calculator for 2X2 and 3X3 square matrix.
Author: Lord Hypersonic
Email: lordhypersonic.522@gmail.com
Website: www.lordhypersonic.blogspot.in
*/
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int ch,A2[2][2],AD2[2][2],C2[2][2],A3[3][3],AD3[3][3],C3[3][3];
cout<<"Find adjoint of :- \n1. 2x2 matrix \n2. 3x3 matrix \n>"; cin>>ch;
if (ch==1) //if user want to find the adjoint of matrix of order 2x2
{
cout<<"Enter elements of A matrix: \n";
//loop for entering elements of matrix A of order 2x2.
for (int i=0; i<2; i++)
{
for (int j=0; j<2; j++)
{
cout<<"A["<<i+1<<"]["<<j+1<<"] : "; cin>>A2[i][j];
}
}
//Calculating cofactors of a matrix of order 2x2
C2[0][0]=A2[1][1]; C2[0][1]=-A2[1][0]; C2[1][0]=-A2[0][1]; C2[1][1]=A2[0][0];
//calculating adjoint of the matrix of order 2x2
AD2[0][0]=C2[0][0]; AD2[0][1]=C2[1][0]; AD2[1][0]=C2[0][1]; AD2[1][1]=C2[1][1];
cout<<"\n\nAdjoint of A is :- \n\n";
cout<<"|\t"<<AD2[0][0]<<"\t"<<AD2[0][1]<<"\t|\n|\t"<<AD2[1][0]<<"\t"<<AD2[1][1]<<"\t|\n";
}
else if (ch==2) //if user wants to calculate the adjoint of a matrix of order 3x3
{
cout<<"Enter elements of A matrix: \n";
//loop for entering elements of matrix A of order 3x3.
for (int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
cout<<"A["<<i+1<<"]["<<j+1<<"] : "; cin>>A3[i][j];
}
}
//calculating cofactors of matrix A of order 3x3
C3[0][0]=((A3[1][1]*A3[2][2])-(A3[2][1]*A3[1][2])); C3[0][1]=-((A3[1][0]*A3[2][2])-(A3[2][0]*A3[1][2]));
C3[0][2]=((A3[1][0]*A3[2][1])-(A3[2][0]*A3[1][1]));
C3[1][0]=-((A3[0][1]*A3[2][2])-(A3[2][1]*A3[0][2])); C3[1][1]=((A3[0][0]*A3[2][2])-(A3[2][0]*A3[0][2])); C3[1][2]=-((A3[0][0]*A3[2][1])-(A3[2][0]*A3[0][1]));
C3[2][0]=((A3[0][1]*A3[1][2])-(A3[1][1]*A3[0][2])); C3[2][1]=-((A3[0][0]*A3[1][2])-(A3[1][0]*A3[0][2]));
C3[2][2]=((A3[0][0]*A3[1][1])-(A3[1][0]*A3[0][1]));
// calculating adjoint of matrix A of order 3x3
AD3[0][0]=C3[0][0]; AD3[0][1]=C3[1][0]; AD3[0][2]=C3[2][0];
AD3[1][0]=C3[0][1]; AD3[1][1]=C3[1][1]; AD3[1][2]=C3[2][1];
AD3[2][0]=C3[0][2]; AD3[2][1]=C3[1][2]; AD3[2][2]=C3[2][2];
//printing values of adjoint A of order 3x3.
cout<<"Adjoint A is:-\n\n";
cout<<"|\t"<<AD3[0][0]<<"\t"<<AD3[0][1]<<"\t"<<AD3[0][2]<<"\t|\n";
cout<<"|\t"<<AD3[1][0]<<"\t"<<AD3[1][1]<<"\t"<<AD3[1][2]<<"\t|\n";
cout<<"|\t"<<AD3[2][0]<<"\t"<<AD3[2][1]<<"\t"<<AD3[2][2]<<"\t|\n";
}
/*
| 00 01 02 |
| 10 11 12 |
| 20 21 22 |
*/
else cout<<"No such choice exist ...... "<<endl; //if the user enters another key other than 1 and 2.
_getch(); //waiting for the user to press any key to exit.
return 0;
}
Working of the program is given below.Program: an Adjoint calculator for 2X2 and 3X3 square matrix.
Author: Lord Hypersonic
Email: lordhypersonic.522@gmail.com
Website: www.lordhypersonic.blogspot.in
*/
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int ch,A2[2][2],AD2[2][2],C2[2][2],A3[3][3],AD3[3][3],C3[3][3];
cout<<"Find adjoint of :- \n1. 2x2 matrix \n2. 3x3 matrix \n>"; cin>>ch;
if (ch==1) //if user want to find the adjoint of matrix of order 2x2
{
cout<<"Enter elements of A matrix: \n";
//loop for entering elements of matrix A of order 2x2.
for (int i=0; i<2; i++)
{
for (int j=0; j<2; j++)
{
cout<<"A["<<i+1<<"]["<<j+1<<"] : "; cin>>A2[i][j];
}
}
//Calculating cofactors of a matrix of order 2x2
C2[0][0]=A2[1][1]; C2[0][1]=-A2[1][0]; C2[1][0]=-A2[0][1]; C2[1][1]=A2[0][0];
//calculating adjoint of the matrix of order 2x2
AD2[0][0]=C2[0][0]; AD2[0][1]=C2[1][0]; AD2[1][0]=C2[0][1]; AD2[1][1]=C2[1][1];
cout<<"\n\nAdjoint of A is :- \n\n";
cout<<"|\t"<<AD2[0][0]<<"\t"<<AD2[0][1]<<"\t|\n|\t"<<AD2[1][0]<<"\t"<<AD2[1][1]<<"\t|\n";
}
else if (ch==2) //if user wants to calculate the adjoint of a matrix of order 3x3
{
cout<<"Enter elements of A matrix: \n";
//loop for entering elements of matrix A of order 3x3.
for (int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
cout<<"A["<<i+1<<"]["<<j+1<<"] : "; cin>>A3[i][j];
}
}
//calculating cofactors of matrix A of order 3x3
C3[0][0]=((A3[1][1]*A3[2][2])-(A3[2][1]*A3[1][2])); C3[0][1]=-((A3[1][0]*A3[2][2])-(A3[2][0]*A3[1][2]));
C3[0][2]=((A3[1][0]*A3[2][1])-(A3[2][0]*A3[1][1]));
C3[1][0]=-((A3[0][1]*A3[2][2])-(A3[2][1]*A3[0][2])); C3[1][1]=((A3[0][0]*A3[2][2])-(A3[2][0]*A3[0][2])); C3[1][2]=-((A3[0][0]*A3[2][1])-(A3[2][0]*A3[0][1]));
C3[2][0]=((A3[0][1]*A3[1][2])-(A3[1][1]*A3[0][2])); C3[2][1]=-((A3[0][0]*A3[1][2])-(A3[1][0]*A3[0][2]));
C3[2][2]=((A3[0][0]*A3[1][1])-(A3[1][0]*A3[0][1]));
// calculating adjoint of matrix A of order 3x3
AD3[0][0]=C3[0][0]; AD3[0][1]=C3[1][0]; AD3[0][2]=C3[2][0];
AD3[1][0]=C3[0][1]; AD3[1][1]=C3[1][1]; AD3[1][2]=C3[2][1];
AD3[2][0]=C3[0][2]; AD3[2][1]=C3[1][2]; AD3[2][2]=C3[2][2];
//printing values of adjoint A of order 3x3.
cout<<"Adjoint A is:-\n\n";
cout<<"|\t"<<AD3[0][0]<<"\t"<<AD3[0][1]<<"\t"<<AD3[0][2]<<"\t|\n";
cout<<"|\t"<<AD3[1][0]<<"\t"<<AD3[1][1]<<"\t"<<AD3[1][2]<<"\t|\n";
cout<<"|\t"<<AD3[2][0]<<"\t"<<AD3[2][1]<<"\t"<<AD3[2][2]<<"\t|\n";
}
/*
| 00 01 02 |
| 10 11 12 |
| 20 21 22 |
*/
else cout<<"No such choice exist ...... "<<endl; //if the user enters another key other than 1 and 2.
_getch(); //waiting for the user to press any key to exit.
return 0;
}
ENJOY 😉
Post a Comment