C Program to Accept a Matrix of Order MxN & Interchange the Diagonals

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

Here is source code of the C program to accept a matrix of order MxN & interchange the diagonals. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*

* C program to accept a matrix of order M x N and store its elements

* and interchange the main diagonal elements of the matrix

* with that of the secondary diagonal elements

*/

#include <stdio.h>

void main ()

{

staticint array[10][10];

int i, j, m, n, a;

printf("Enter the order of the matix \n");

scanf("%d %d",&m,&n);

if(m == n)

{

printf("Enter the co-efficients of the matrix\n");

for(i =0; i < m;++i)

{

for(j =0; j < n;++j)

{

scanf("%dx%d",&array[i][j]);

}

}

printf("The given matrix is \n");

for(i =0; i < m;++i)

{

for(j =0; j < n;++j)

{

printf(" %d", array[i][j]);

}

printf("\n");

}

for(i =0; i < m;++i)

{

a = array[i][i];

array[i][i]= array[i][m - i -1];

array[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", array[i][j]);

}

printf("\n");

}

}

else

printf("The given order is not square matrix\n");

}

advertisements

$ cc pgm72.c
$ a.out
Enetr the order of the matix
22
Enter the co-efficients of the matrix
25307843
The given matrix is
25307843
The matrix after changing the
main diagonal & secondary diagonal
30254378