C++ Program to Interchange Diagonals of Matrix

This is a C++ Program to Accept a Matrix and Interchange the Diagonals.

Problem Description

The program takes a square matrix and interchanges the diagonals.

Problem Solution

1. The program takes a square matrix.
2. Using a for loop, the right and left diagonals are interchanged.
3. The result is printed.
4. Exit.

C++ Program/Source code

Here is the source code of C++ Program to Accept a Matrix and Interchange the Diagonals. The program output is shown below.

  1. #include<iostream>
  2. using namespace std;
  3. int main ()
  4. {
  5.     int i, j, m, n, x, A[10][10]; 
  6.     cout << "Enter number of rows and columns : ";
  7.     cin >> m >> n;
  8.     if (m != n)
  9.     {
  10.         cout << "Matrix is not a square matrix!";
  11.         exit(0);
  12.     }
  13.     cout << "Enter elements of matrix : ";
  14.     for (i = 0; i < m; i++)
  15.         for (j = 0; j < n; j++)	
  16.             cin >> A[i][j];
  17.     cout << "Matrix : \n ";
  18.     for (i = 0; i < m; i++)
  19.     {
  20.         for (j = 0; j < n; j++)
  21.             cout << A[i][j] << " ";
  22.         cout << "\n ";
  23.     }
  24.     for (i = 0; i < m; i++)
  25.     {
  26.         x = A[i][i];
  27.         A[i][i] = A[i][m - i - 1];
  28.         A[i][m - i - 1] = x;
  29.     }
  30.     cout << "\nMatrix after changing the diagonals : \n ";
  31.     for (i = 0; i < m; i++)
  32.     {
  33.         for (j = 0; j < n; j++)
  34.             cout << A[i][j] << " ";
  35.         cout << "\n ";
  36.     }
  37.     return 0;
  38. }
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. The entered matrix is displayed.
4. Using a for loop, elements of left and right diagonals are exchanged.
5. The resultant matrix is then printed.

advertisement
advertisement
Runtime Test Cases
Case 1 :
Enter number of rows and columns : 2 2
Enter elements of matrix : 1 2 3 4
Matrix :
 1 2
 3 4
Matrix after changing the diagonals :
 2 1 
 4 3
 
Case 2 :
Enter number of rows and columns : 2 3
Matrix is not a square matrix!
 
Case 3 :
Enter number of rows and columns : 3 3 
Enter elements of matrix : 9 8 7 6 5 4 3 2 1
Matrix :
 9 8 7
 6 5 4
 3 2 1
Matrix after changing the diagonals :
 7 8 9
 6 5 4
 1 2 3

Sanfoundry Global Education & Learning Series – C++ Programs.

To practice all C++ programs, here is complete set of 1000+ C++ Programming examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). 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!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.