C Program to Interchange Diagonals of Matrix

This is a program to accept a matrix of order MxN & interchange the diagonals.

Problem Description

This C Program accepts matrix of order MxN & interchange the diagonals. This program first accepts the matrix. Then exchange diagonals of the matrix.

Problem Solution

1. Create a matrix of some order and define its elements using for loop.
2. Now run a for loop till the number of rows times.
3. The element at first row and first column is exchanged with element of first row and last column.
4. Change element at second row and second column is exchanged with element of second row and last second column.
5. Continue with the pattern, at the end we will get diagonals exchanged.

Program/Source Code

Here is source code of the C program to accept a matrix of order MxN & interchange the diagonals. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

  1.    /*
  2.      * C program to accept a matrix of order M x N and store its elements
  3.      * and interchange the main diagonal elements of the matrix
  4.      * with that of the secondary diagonal elements
  5.      */
  6.  
  7.     #include <stdio.h>
  8.     void main ()
  9.     {
  10.  
  11.         static int array[10][10];
  12.         int i, j, m, n, a;
  13.  
  14.         printf("Enter the order of the matix \n");
  15.         scanf("%d %d", &m, &n);
  16.  
  17.         if (m == n)
  18.         {
  19.             printf("Enter the co-efficients of the matrix\n");
  20.             for (i = 0; i < m; ++i) 
  21.             {
  22.                 for (j = 0; j < n; ++j) 
  23.                 {
  24.                     scanf("%dx%d", &array[i][j]);
  25.                 }
  26.             }
  27.  
  28.             printf("The given matrix is \n");
  29.             for (i = 0; i < m; ++i) 
  30.             {
  31.                 for (j = 0; j < n; ++j) 
  32.                 {
  33.                     printf(" %d", array[i][j]);
  34.                 }
  35.                 printf("\n");
  36.             }
  37.  
  38.             for (i = 0; i < m; ++i) 
  39.             {
  40.                 a = array[i][i];
  41.                 array[i][i] = array[i][m - i - 1];
  42.                 array[i][m - i - 1] = a;
  43.             }
  44.  
  45.             printf("The matrix after changing the \n");
  46.             printf("main diagonal & secondary diagonal\n");
  47.             for (i = 0; i < m; ++i)
  48.             {
  49.                 for (j = 0; j < n; ++j) 
  50.                 {
  51.                     printf(" %d", array[i][j]);
  52.  
  53.                 }
  54.  
  55.                 printf("\n");
  56.  
  57.             }
  58.  
  59.         }
  60.  
  61.         else
  62.             printf("The given order is not square matrix\n");
  63.  
  64.     }
Program Explanation

1. Declare a 2D array of some capacity.
2. Take its order as input from users and define its elements according to the order and using for loop.
3. Now print the matrix.
4. Run a for loop the number of rows in the matrix times.
5. The row index of the both the elements which are meant to interchange remains same for same row. But the element of first row gets exchanged with last row, then second column element gets interchanged with second last column of the matrix and so on.
6. After the completion of for loop, print the matrix with diagonal elements being interchanged

advertisement
advertisement
Runtime Test Cases
Enetr the order of the matix
2 2
Enter the co-efficients of the matrix
25 30
78 43
The given matrix is
 25 30
 78 43
The matrix after changing the
main diagonal & secondary diagonal
 30 25
 43 78

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

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.