C Program to interchange diagonals of matrix

April 15, 2022, Learn eTutorial
1645

What is 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 their index(position values).

What is the diagonal of a matrix?

The diagonal of a matrix means, the elements lie diagonally in 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'

How to interchange the diagonals of a matrix using the C program?

In this C program, we need to interchange the diagonals of a square matrix - a matrix with an equal number of rows and columns. Interchanging the diagonals will exchange the elements on the diagonals. The logic used in this C program is to swap the position of the elements of the matrix to get the desired results.

As a first step, we accept the order of a matrix into m and n. Then we have to check '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, it's a square matrix so 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.

STEP 2: Declare the integer variables i, j, m, n, val, 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 display the given order is not of a Square Matrix.

STEP 5: Read the coefficients of the Matrix from the user using nested `for loop` with conditions i<m and j<n and save the numbers to ma[i][j]

STEP 6: Display the given matrix using another nested `for loop`.

STEP 7: Use a `for loop` with condition i<m do step 8

STEP 8: Swap elements on diagonals as follows,

1. val = ma[j][i]
2. ma[i][i] = ma[i][m-i-1]
3. ma[i][m-i-1] = val

STEP 9: Display the Matrix after changing the diagonals as the elements in ma[i][j] using nested `for loop`.

To interchange the diagonals of a matrix we have to use the below C topics, please refer these topics to get a better understanding

C Source Code

```                                          ```#include <stdio.h>

void main() {
static int ma[10][10];
int i, j, m, n, val;
printf("Enter the order of the matrix \n");
scanf("%d %d", & m, & n);
if (m == n) {
printf("Enter the elements 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 input 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) {
val = ma[i][i];
ma[i][i] = ma[i][m - i - 1]; /* interchanging the elements of diagonals inside a matrix */
ma[i][m - i - 1] = val;
}
printf("The matrix after changing the diagonals\n");
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
printf(" %d", ma[i][j]); /* displaying the output matrix */
}
printf("\n");
}
} else
printf("The given order is not of a square matrix\n");
} /* end of main() */```
```

OUTPUT

```Enter the order of the matrix
3 3

Enter the elements 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 diagonals
3 2 1
4 5 6
9 8 7```

VIEW ALL