C++ Program to Find if a Matrix is Orthogonal

«
»

This is a C++ Program to Find if a Matrix is Orthogonal.

Problem Description

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.

Problem Solution

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.

advertisement
C++ Program/Source code

Here is the source code of C++ Program to Find if a Matrix is Orthogonal. The program output is shown below.

  1. #include<iostream>
  2. using namespace std;
  3. int main ()
  4. {
  5.     int m, n, p, i, j, k, sum = 0;
  6.     int A[10][10], T[10][10], P[10][10];
  7.     cout << "Enter number of rows and columns : ";
  8.     cin >> m >> n;
  9.     if (m != n)
  10.     {
  11.         cout << "Matrix is not a square matrix!";
  12.         exit(0);
  13.     }
  14.     cout << "Enter elements of matrix : ";
  15.     for (i = 0; i < m; i++)
  16.         for (j = 0; j < n; j++)
  17.             cin >> A[i][j];
  18.  
  19.     for (i = 0; i < m; i++)
  20.         for (j = 0; j < n; j++)
  21.             T[j][i] = A[i][j];
  22.  
  23.     for (i = 0; i < m; i++)
  24.     {
  25.         for (j = 0; j < n; j++)
  26.         {
  27.             for ( k = 0 ; k < n ; k++ )
  28.                 sum = sum + A[i][k] * T[k][j];
  29.             P[i][j] = sum;
  30.             sum = 0;
  31.         }			
  32.     }
  33.     for (i = 0; i < m; i++)
  34.     {
  35.         for (j = 0; j < n; j++)
  36.         {
  37.             if (i == j)
  38.                 if (P[i][j] != 1 )
  39.                     break;
  40.             else
  41.             {
  42.                 if (P[i][j] != 0 )
  43.                     break;
  44.             }
  45.         }
  46.         if (j != m)
  47.             break;
  48.     }
  49.     if (i != m)
  50.         cout << "Matrix is not orthogonal.\n ";
  51.     else
  52.         cout << "Matrix is orthogonal.\n ";
  53. 	for (i = 0; i < m; i++)
  54.     {
  55.         for (j = 0; j < n; j++)	
  56.             cout << A[i][j] << " ";
  57.         cout << "\n ";
  58.     }    
  59.     return 0;
  60. }
Program Explanation

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.

advertisement
advertisement
Runtime Test Cases
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.

advertisement

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!
advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter