C Program to interchange diagonals of matrix

For a better understanding, we always recommend you to learn the basic topics of C programming listed below:

What is the matrix?

Here you will learn how to program to interchange diagonals of the matrix. A matrix is a collection of elements of the same data type. A matrix can be defined as an array of arrays which means the elements are arranged in rows and columns format. We can access the elements in a matrix by an index variable.

What is the diagonal of a matrix?

The diagonal of a matrix means, the elements lie in the diagonal of a matrix. That is elements from the top left to the bottom right where the row number and the column number of the element of the matrix are equal. For example, the diagonal elements of the matrix which is shown below are '1,5,9' and '3,5,7'

[1 2 3
4 5 6
3 7 9]

In this c program, we need to interchange the diagonal of a matrix, which means the diagonals of a matrix need to change each other. The logic used in this c program is to interchange the position of the elements of the matrix to get the desired results.

As a first step, we accept the order of matrix into m and n. Then we have to check the 'm' and 'n' are equal. That is, the number of rows and number of columns of the matrix are equal or not. If it's equal then we will read the elements of the matrix and display them, then we interchange the diagonal elements by using for loop. And we will display the interchanged matrix. If 'm' and 'n' are not equal then, we will display the order of the matrix is not a square matrix.

ALGORITHM

STEP 1: Include the header files to use the built-in Functions in the C program.

STEP 2: Declare the integer variables i, j, m, n, a, and Matrix ma[][].

STEP 3: Read the Order of the Matrix into 'm' and 'n'.

STEP 4: Then check if m = n, if true then do step 5.Else do step 14.

STEP 5: Read the coefficients of the Matrix from the user using nested for loop and saved it into ma[i][j].

STEP 6: Display the given matrix as the elements in the matrix using for loop.

STEP 7: Set i=0.

STEP 8: Check the condition i

STEP 9: Assign a=ma[j][i],ma[i][i]   = ma[i][m-i-1],ma[i][m-i-1] = a.

STEP 10: Increment 'i' by 1 and do step 8.

STEP 11: Display the Matrix after changing the main diagonal and secondary diagonal as the elements in ma[i[j] using nested for loop.

STEP 12: Read the coefficients of the Matrix from the user using nested for loop and saved it into ma[i][j].

STEP 13: Display the given matrix as the elements in the matrix using for loop.

STEP 14: Display The given order is not a Square Matrix.

C Source Code

                                          #include <stdio.h>

void main() {
    static int ma[10][10];
    int i, j, m, n, a;
    printf("Enter the order of the matrix \n");
    scanf("%d %d", & m, & n);
    if (m == n) {
      printf("Enter the co-efficient s of the matrix\n");
      for (i = 0; i < m; ++i) {
        for (j = 0; j < n; ++j) {
          scanf("%d", & ma[i][j]);
        }
      }    
        printf("The given matrix is \n"); /* prints the output matrix */
        for (i = 0; i < m; ++i) {
          for (j = 0; j < n; ++j) {
            printf(" %d", ma[i][j]);
          }
          printf("\n");
        }
        for (i = 0; i < m; ++i) {
          a = ma[i][i];
          ma[i][i] = ma[i][m - i - 1]; /* interchanging the elements inside a matrix */
          ma[i][m - i - 1] = a;
        }
        printf("The matrix after changing the \n");
        printf("main diagonal & secondary diagonal\n");
        for (i = 0; i < m; ++i) {
          for (j = 0; j < n; ++j) {
            printf(" %d", ma[i][j]); /* displaying the output of the matrix */
          }
          printf("\n");
        }
      } else
        printf("The given order is not square matrix\n");
    } /* end of main() */
                                      

OUTPUT

Enter the order of the matrix
3 3

Enter the co-efficient s of the matrix
1 2 3
4 5 6
7 8 9

The given matrix is
 1 2 3
 4 5 6
 7 8 9

The matrix after changing the
main diagonal & secondary diagonal
 3 2 1
 4 5 6
 9 8 7