Interchange the diagonal elements of the matrix
C program to interchange the diagonal elements of the matrix.
Program
#include<stdio.h>
#include<conio.h>
#define MAX 10
void read_matrix(int mat[MAX][MAX], int row, int col)
{
int i, j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("Element [%d][%d]\t:",i,j);
scanf("%d",&mat[i][j]);
}
}
}
void print_matrix(int mat[MAX][MAX], int row, int col)
{
int i, j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d\t",mat[i][j]);
}
printf("\n");
}
}
void interchange_diag(int mat[MAX][MAX], int row, int col)
{
int i, j, temp;
for(i=0;i<row;i++)
{
temp = mat[i][i];
mat[i][i] = mat[i][row-i-1];
mat[i][row-i-1] = temp;
}
}
void main()
{
int mat[MAX][MAX];
int row, col;
printf("Enter the number of rows or columns of the square matrix\t:");
scanf("%d",&row);
col = row;
printf("Enter the elements of the matrix\n");
read_matrix(mat, row, col);
printf("\nMatrix entered is\n");
print_matrix(mat, row, col);
interchange_diag(mat, row, col);
printf("\nMatrix after interchanging of diagonals\n");
print_matrix(mat, row, col);
getch();
}
Output
Enter the number of rows or columns of the square matrix :3
Enter the elements of the matrix
Element [0][0] :1
Element [0][1] :2
Element [0][2] :3
Element [1][0] :4
Element [1][1] :5
Element [1][2] :6
Element [2][0] :7
Element [2][1] :8
Element [2][2] :9
Matrix entered is
1 2 3
4 5 6
7 8 9
Matrix after interchanging of diagonals
3 2 1
4 5 6
9 8 7