This is a C++ Program to Find if a Matrix is Orthogonal.
The program takes a matrix and checks if it is orthogonal. A square matrix is defined as an orthogonal matrix if its transpose matrix is same as its inverse matrix.
1. The program takes a square matrix.
2. The transpose of the matrix is found.
3. The product of the matrix and transpose are calculated and stored.
4. If the product is an identity matrix, then it is orthogonal.
5. Else it is not an orthogonal matrix.
6. The result is printed.
7. Exit.
Here is the source code of C++ Program to Find if a Matrix is Orthogonal. The program output is shown below.
#include<iostream>
using namespace std;
int main ()
{
int m, n, p, i, j, k;
int A[10][10], T[10][10], P[10][10];
cout << "Enter number of rows and columns : ";
cin >> m >> n;
if (m != n)
{
cout << "Matrix is not a square matrix!";
exit(0);
}
cout << "Enter elements of matrix : ";
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
cin >> A[i][j];
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
T[j][i] = A[i][j];
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
int sum=0;
for ( k = 0 ; k < n ; k++ )
sum += A[i][k] * T[k][j];
P[i][j] = sum;
}
}
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
if (i == j)
{
if (P[i][j] != 1 )
break;
}
else
{
if (P[i][j] != 0 )
break;
}
}
if (j != m)
break;
}
if (i != m)
cout << "Matrix is not orthogonal.\n ";
else
cout << "Matrix is orthogonal\nPrinting Original Matrix:\n ";
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
cout << A[i][j] << " ";
cout << "\n ";
}
return 0;
}
1. The user is asked to enter the number of rows and columns of the matrix. If they are not equal, program is exited.
2. The elements of the matrix are asked to enter and stored in ‘A’.
3. Transpose of the matrix is stored in the array ‘T’.
4. The product of the matrix and its transpose is stored in the matrix ‘P’.
5. If P is an identity matrix, then the matrix is orthogonal, else not.
6. The result is then printed.
Case 1 : Enter number of rows and columns : 2 2 Enter elements of matrix : 0 1 1 0 Matrix is orthogonal. 0 1 1 0 Case 2 : Enter number of rows and columns : 3 3 Enter elements of matrix : -2 1 1 1 -2 1 1 1 -2 Matrix is not orthogonal. -2 1 1 1 -2 1 1 1 -2 Case 3 : Enter number of rows and columns : 3 3 Enter elements of matrix : 1 0 0 0 1 0 0 0 1 Matrix is orthogonal. 1 0 0 0 1 0 0 0 1
Sanfoundry Global Education & Learning Series – C++ Programs.
To practice all C++ programs, here is complete set of 1000+ C++ Programming examples.
- Check C++ Books
- Practice Computer Science MCQs
- Apply for Computer Science Internship
- Check Programming Books
- Practice Programming MCQs