Swap major and minor diagonals of a square matrix

Given a square matrix, swap the element of major and minor diagonals.

Major Diagonal Elements of a Matrix :
The Major Diagonal Elements are the ones that occur from Top Left of Matrix Down To Bottom Right Corner. The Major Diagonal is also known as Main Diagonal or Primary Diagonal.

Minor Diagonal Elements of a Matrix :
The Minor Diagonal Elements are the ones that occur from Top Right of Matrix Down To Bottom Left Corner. Also known as Secondary Diagonal.

Example :

Input : 0 1 2
        3 4 5
        6 7 8

Output : 2 1 0
         3 4 5
         8 7 6



Approach :
The Simple thing one should know is that the indexes of Primary or Major diagonal are same i.e. lets say A is matrix then A[1][1] will be a Major Diagonal element and sum of indexes of Minor Diagonal is equal to size of Matrix. Lets say A is a matrix of size 3 then A[1][2] will be Minor Diagonal element.

Below is the implementation of above approach :

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP Program to swap diagonal of a matrix
#include <bits/stdc++.h>
using namespace std;
  
// size of square matrix
#define N 3
  
// Function to swap diagonal of matrix
void swapDiagonal(int matrix[][N]) {
  for (int i = 0; i < N; i++)
    swap(matrix[i][i], matrix[i][N - i - 1]);
}
  
// Driver Code
int main() {
  int matrix[N][N] = {{0, 1, 2}, 
                      {3, 4, 5}, 
                      {6, 7, 8}};
  
  swapDiagonal(matrix);
  
  // Displaying modified matrix
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++)
      cout << matrix[i][j] << " ";
    cout << endl;
  }
  
  return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to swap
// diagonal of a matrix
import java.io.*;
  
class Gfg {
static int N = 3;
  
// Function to swap diagonal of matrix
static void swapDiagonal(int matrix[][]) {
    for (int i = 0; i < N; i++) {
    int temp = matrix[i][i];
    matrix[i][i] = matrix[i][N - i - 1];
    matrix[i][N - i - 1] = temp;
    }
}
  
// Driver function
public static void main(String arg[]) {
    int matrix[][] = {{0, 1, 2}, 
                      {3, 4, 5}, 
                      {6, 7, 8}};
  
    swapDiagonal(matrix);
  
    // Displaying modified matrix
    for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++)
        System.out.print(matrix[i][j] + " ");
    System.out.println();
    }
}
}
  
// This code is contributed by Anant Agarwal.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 Program to swap diagonal of a matrix
  
# size of square matrix
N = 3
  
# Function to swap diagonal of matrix
def swapDiagonal(matrix):
      
    for i in range(N):
          
        matrix[i][i], matrix[i][N-i-1] = \
            matrix[i][N-i-1], matrix[i][i]
  
  
# Driver Code
matrix = [[0, 1, 2],
          [3, 4, 5],
          [6, 7, 8]]
  
# swap diagonals of matrix
swapDiagonal(matrix);
  
# Displaying modified matrix
for i in range(N):    
    for j in range(N):        
        print(matrix[i][j], end = ' ')        
    print()

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation to swap
// diagonal of a matrix
using System;
  
class Gfg {
      
    static int N = 3;
      
    // Function to swap diagonal of matrix
    static void swapDiagonal(int [,]matrix) {
        for (int i = 0; i < N; i++) {
        int temp = matrix[i,i];
        matrix[i,i] = matrix[i,N - i - 1];
        matrix[i,N - i - 1] = temp;
        }
    }
      
    // Driver function
    public static void Main() {
        int [,]matrix = {{0, 1, 2}, 
                         {3, 4, 5}, 
                         {6, 7, 8}};
      
        swapDiagonal(matrix);
      
        // Displaying modified matrix
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++)
                Console.Write(matrix[i,j] + " ");
            Console.WriteLine();
        }
    }
}
  
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP Program to swap 
// diagonal of a matrix
  
// size of square matrix
$N = 3;
  
// Function to swap 
// diagonal of matrix
function swapDiagonal($matrix
{
    global $N;
    for ($i = 0; $i < $N; $i++)
    {
        $tmp=$matrix[$i][$i];
        $matrix[$i][$i] = $matrix[$i][$N - $i - 1];
        $matrix[$i][$N - $i - 1] = $tmp;
    }
      
// Displaying modified matrix
for ($i = 0; $i < $N; $i++) 
{
    for ($j = 0; $j < $N; $j++)
        echo $matrix[$i][$j] . " ";
    echo "\n";
}
}
  
// Driver Code
$matrix = array(array(0, 1, 2), 
                array(3, 4, 5), 
                array(6, 7, 8));
  
swapDiagonal($matrix);
  
// This code is contributed by mits
?>

chevron_right



Output :

2 1 0
3 4 5
8 7 6


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Mithun Kumar



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.