Q:

Write C Program to interchange diagonals of a matrix

0

Write C Program to interchange diagonals of a matrix

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <stdio.h>
 
void main ()
{
    static int 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");
}

Result:

Enter the order of the matix 

3

3

Enter the co-efficients 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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

<< Write C Program to Find the Frequency of Odd & Eve...