Program to Interchange Diagonals of Matrix in C program


In this tutorial, we will be discussing a program to interchange the diagonals of a given matrix.

For this, we will be given with a square matrix of the order n*n. Our task is to interchange the elements in the two diagonals of the matrix and then return the new matrix.

Example

 Live Demo

#include<bits/stdc++.h>
using namespace std;
#define N 3
//interchanging the two diagonals
void int_diag(int array[][N]){
   for (int i = 0; i < N; ++i)
      if (i != N / 2)
   swap(array[i][i], array[i][N - i - 1]);
   for (int i = 0; i < N; ++i){
      for (int j = 0; j < N; ++j)
      printf(" %d", array[i][j]);
      printf("
");    } } int main(){    int array[N][N] = {24, 45, 64,    17, 21, 34,    75, 38, 98};    int_diag(array);    return 0; }

Output

64 45 24
17 21 34
98 38 75

Updated on: 19-Dec-2019

164 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements