Here is the C program to swap major diagonal and minor diagonal of a square matrix.
For Example:
Input Matrix 1 2 3 4 5 6 7 8 9 Output Matrix 3 2 1 4 5 6 9 8 7
Required Knowledge
Algorithm to swap major and minor diagonal elements of a square matrix
Let inputMatrix is a square matrix of row and column dimension N.
Let inputMatrix is a square matrix of row and column dimension N.
- For every row, we will swap the elements of major and minor diagonals.
- In any row R, the major diagonal element will be at inputMatrix[R][R] and minor diagonal element will be at inputMatrix[R][COLS-R-1] where COLS is the total number of columns in square matrix inputMatrix.
C program to sort an array in increasing order using bubble sort
#include <stdio.h> int main(){ int rows, cols, row, col, temp; int matrix[50][50]; printf("Enter Rows and Columns of Square Matrix\n"); scanf("%d %d", &rows, &cols); printf("Enter Matrix of size %dX%d\n", rows, cols); for(row = 0; row < rows; row++){ for(col = 0; col < cols; col++){ scanf("%d", &matrix[row][col]); } } /* Interchange Major and Minor diagonals of Matrix */ for(row = 0; row < rows; row++) { col = row; temp = matrix[row][col]; matrix[row][col] = matrix[row][(cols-col)-1]; matrix[row][(cols-col)-1] = temp; } printf("Matrix After Swapping Diagonals\n"); for(row = 0; row < rows; row++){ for(col = 0; col < cols; col++){ printf("%d ", matrix[row][col]); } printf("\n"); } return 0; }Output
Enter Rows and Columns of Square Matrix 3 3 Enter Matrix of size 3X3 1 2 3 4 5 6 7 8 9 Matrix After Swapping Diagonals 3 2 1 4 5 6 9 8 7
Related Topics