Swap diagonal elements in matrix

Here given code implementation process.

/*
  C Program 
+ Interchange the diagonals of a matrix
*/
#include<stdio.h>
#define SIZE 4

//Method which are perform swap diagonal elements
void swap_diagonals(int data[SIZE][SIZE],int start_row,int end_row, int start_col,int end_col)
{

  if(start_row >= end_row || start_col >= end_col)
  {
    return ;
  }


  int temp=data[start_row][start_col];

  //Swap the element value
  data[start_row][start_col] = data[start_row][end_col];
  data[start_row][end_col] = temp;

  temp=data[end_row][start_col];
  //Swap the element value
  data[end_row][start_col] = data[end_row][end_col];
  data[end_row][end_col] = temp;

  swap_diagonals(data,start_row+1,end_row-1,start_col+1,end_col-1);
}
//
void interchange(int data[SIZE][SIZE])
{
  
  swap_diagonals(data,0,SIZE-1,0,SIZE-1);

}
//Display matrix elements
void show_data(int arr[][SIZE])
{
  for (int i = 0; i < SIZE; ++i)
  {
    for (int j = 0; j < SIZE; ++j)
    {
      printf("%3d",arr[i][j] );
    }
    printf("\n");
  }
  printf("\n");
}
int main(){

  int arr[SIZE][SIZE]= 
  {
    {1,  2,  3,  4},
    {5,  6,  7,  8},
    {9,  10, 11, 12},
    {13, 14, 15, 16}
  };

  show_data(arr);
  interchange(arr);
  show_data(arr);

  return 0;
}

Output

  1  2  3  4
  5  6  7  8
  9 10 11 12
 13 14 15 16

  4  2  3  1
  5  7  6  8
  9 11 10 12
 16 14 15 13
/*
  C++ Program
  Interchange the diagonals of a matrix
*/
#include<iostream>
#define SIZE 4
using namespace std;
class MyMatrix {
	public:
		int size;
	MyMatrix() {
		//Get the size of matrix
		//Assume that given matrix  size is NXN
		this->size = SIZE;
	}
	//Method which are perform swap diagonal elements
	void swap_diagonals(int matrix[][SIZE], int start_row, int end_row, int start_col, int end_col) {
		if (start_row >= end_row || start_col >= end_col) {
			return;
		}
		int temp = matrix[start_row][start_col];
		//Swap the element value
		matrix[start_row][start_col] = matrix[start_row][end_col];
		matrix[start_row][end_col] = temp;
		temp = matrix[end_row][start_col];
		//Swap the element value
		matrix[end_row][start_col] = matrix[end_row][end_col];
		matrix[end_row][end_col] = temp;
		this->swap_diagonals(matrix, start_row + 1, end_row - 1, start_col + 1, end_col - 1);
	}
	//
	void interchange(int matrix[][SIZE]) {
		this->swap_diagonals(matrix, 0, this->size - 1, 0, this->size - 1);
	}
	//Display matrix elements
	void show_matrix(int matrix[][SIZE]) {
		for (int i = 0; i < this->size; ++i) {
			for (int j = 0; j < this->size; ++j) {
				cout << " " << matrix[i][j];
			}
			cout << "\n";
		}
		cout << "\n";
	}
};
int main() {
  	MyMatrix obj ;
	int matrix[][SIZE] = {
		{
			1,
			2,
			3,
			4
		},
		{
			5,
			6,
			7,
			8
		},
		{
			9,
			10,
			11,
			12
		},
		{
			13,
			14,
			15,
			16
		}
	};
	
	obj.show_matrix(matrix);
	obj.interchange(matrix);
	obj.show_matrix(matrix);
	return 0;
}

Output

 1 2 3 4
 5 6 7 8
 9 10 11 12
 13 14 15 16

 4 2 3 1
 5 7 6 8
 9 11 10 12
 16 14 15 13
/*
  Java Program
  Interchange the diagonals of a matrix
*/
public class MyMatrix {

  public int size;

  public MyMatrix(int [][]matrix)
  {
    //Get the size of matrix
    //Assume that given matrix  size is NXN
    this.size = matrix.length;

  }
  //Method which are perform swap diagonal elements
  public void swap_diagonals(int [][]matrix,int start_row,int end_row, int start_col,int end_col)
  {

    if(start_row >= end_row || start_col >= end_col)
    {
      return ;
    }


    int temp=matrix[start_row][start_col];

    //Swap the element value
    matrix[start_row][start_col] = matrix[start_row][end_col];
    matrix[start_row][end_col] = temp;

    temp=matrix[end_row][start_col];
    //Swap the element value
    matrix[end_row][start_col] = matrix[end_row][end_col];
    matrix[end_row][end_col] = temp;

    swap_diagonals(matrix,start_row+1,end_row-1,start_col+1,end_col-1);
  }
  //
  public void interchange(int [][]matrix)
  {
    
    swap_diagonals(matrix,0,this.size-1,0,this.size-1);

  }
  //Display matrix elements
  public void show_matrix(int [][]matrix)
  {
    for (int i = 0; i < this.size; ++i)
    {
      for (int j = 0; j < this.size; ++j)
      {
        System.out.print("  "+matrix[i][j] );
      }
      System.out.print("\n");
    }
    System.out.print("\n");
  }

  public static void main(String[] args) 
  {
    //Define matrix element
    int [][]matrix =
    {
      {1,  2,  3,  4},
      {5,  6,  7,  8},
      {9,  10, 11, 12},
      {13, 14, 15, 16}
    };
    MyMatrix obj = new MyMatrix(matrix);
    obj.show_matrix(matrix);
    obj.interchange(matrix);
    obj.show_matrix(matrix);
  }
}

Output

 1 2 3 4
 5 6 7 8
 9 10 11 12
 13 14 15 16

 4 2 3 1
 5 7 6 8
 9 11 10 12
 16 14 15 13
/*
  C# Program
  Interchange the diagonals of a matrix
*/
using System;

public class MyMatrix {
	int size;
	MyMatrix(int[,] matrix) {
		//Get the size of matrix
		//Assume that given matrix  size is NXN
		this.size = matrix.GetLength(0);
	}
	//Method which are perform swap diagonal elements
	public void swap_diagonals(int[,] matrix, int start_row, int end_row, int start_col, int end_col) {
		if (start_row >= end_row || start_col >= end_col) {
			return;
		}
		int temp = matrix[start_row,start_col];
		//Swap the element value
		matrix[start_row,start_col] = matrix[start_row,end_col];
		matrix[start_row,end_col] = temp;
		temp = matrix[end_row,start_col];
		//Swap the element value
		matrix[end_row,start_col] = matrix[end_row,end_col];
		matrix[end_row,end_col] = temp;
		swap_diagonals(matrix, start_row + 1, end_row - 1, start_col + 1, end_col - 1);
	}
	//
	public void interchange(int[,] matrix) {
		swap_diagonals(matrix, 0, this.size - 1, 0, this.size - 1);
	}
	//Display matrix elements
	public void show_matrix(int[,] matrix) {
		for (int i = 0; i < this.size; ++i) {
			for (int j = 0; j < this.size; ++j) {
				Console.Write(" " + matrix[i,j]);
			}
			Console.Write("\n");
		}
		Console.Write("\n");
	}
	public static void Main(String[] args) {
		int[,]
		//Define matrix element
		matrix = {
			{
				1,
				2,
				3,
				4
			},
			{
				5,
				6,
				7,
				8
			},
			{
				9,
				10,
				11,
				12
			},
			{
				13,
				14,
				15,
				16
			}
		};
		MyMatrix obj = new MyMatrix(matrix);
		obj.show_matrix(matrix);
		obj.interchange(matrix);
		obj.show_matrix(matrix);
	}
}

Output

 1 2 3 4
 5 6 7 8
 9 10 11 12
 13 14 15 16

 4 2 3 1
 5 7 6 8
 9 11 10 12
 16 14 15 13
<?php
/*
  Php Program
  Interchange the diagonals of a matrix
*/
class MyMatrix {
	public $size;

	function __construct($matrix) {
		//Get the size of matrix
		//Assume that given matrix  size is NXN
		$this->size = count($matrix);
	}
	//Method which are perform swap diagonal elements

	public 	function swap_diagonals(&$matrix, $start_row, $end_row, $start_col, $end_col) {
		if ($start_row >= $end_row || $start_col >= $end_col) {
			return;
		}
		$temp = $matrix[$start_row][$start_col];
		//Swap the element value
		$matrix[$start_row][$start_col] = $matrix[$start_row][$end_col];
		$matrix[$start_row][$end_col] = $temp;
		$temp = $matrix[$end_row][$start_col];
		//Swap the element value
		$matrix[$end_row][$start_col] = $matrix[$end_row][$end_col];
		$matrix[$end_row][$end_col] = $temp;
		$this->swap_diagonals($matrix, $start_row + 1, $end_row - 1, $start_col + 1, $end_col - 1);
	}
	//

	public 	function interchange(&$matrix) {
		$this->swap_diagonals($matrix, 0, $this->size - 1, 0, $this->size - 1);
	}
	//Display matrix elements

	public 	function show_matrix($matrix) {
		for ($i = 0; $i < $this->size; ++$i) {
			for ($j = 0; $j < $this->size; ++$j) {
				echo(" ". $matrix[$i][$j]);
			}
			echo("\n");
		}
		echo("\n");
	}
}

function main() {
	//Define matrix element
	$matrix = array(
      array(1, 2, 3, 4), 
      array(5, 6, 7, 8), 
      array(9, 10, 11, 12), 
      array(13, 14, 15, 16)
    );
	$obj = new MyMatrix($matrix);
	$obj->show_matrix($matrix);
	$obj->interchange($matrix);
	$obj->show_matrix($matrix);

}
main();

Output

 1 2 3 4
 5 6 7 8
 9 10 11 12
 13 14 15 16

 4 2 3 1
 5 7 6 8
 9 11 10 12
 16 14 15 13
/*
  Node Js Program
  Interchange the diagonals of a matrix
*/
class MyMatrix {
	constructor(matrix) {
		//Get the size of matrix
		//Assume that given matrix  size is NXN
		this.size = matrix.length;
	}

	//Method which are perform swap diagonal elements
	swap_diagonals(matrix, start_row, end_row, start_col, end_col) {
		if (start_row >= end_row || start_col >= end_col) {
			return;
		}
		var temp = matrix[start_row][start_col];
		//Swap the element value
		matrix[start_row][start_col] = matrix[start_row][end_col];
		matrix[start_row][end_col] = temp;
		temp = matrix[end_row][start_col];
		//Swap the element value
		matrix[end_row][start_col] = matrix[end_row][end_col];
		matrix[end_row][end_col] = temp;
		this.swap_diagonals(matrix, start_row + 1, end_row - 1, start_col + 1, end_col - 1);
	}

	//
	interchange(matrix) {
		this.swap_diagonals(matrix, 0, this.size - 1, 0, this.size - 1);
	}

	//Display matrix elements
	show_matrix(matrix) {
		for (var i = 0; i < this.size; ++i) {
			for (var j = 0; j < this.size; ++j) {
				process.stdout.write(" " + matrix[i][j]);
			}

			process.stdout.write("\n");
		}

		process.stdout.write("\n");
	}
}

function main(args) {
	//Define matrix element
	var matrix = [
		[1, 2, 3, 4],
		[5, 6, 7, 8],
		[9, 10, 11, 12],
		[13, 14, 15, 16]
	];
	var obj = new MyMatrix(matrix);
	obj.show_matrix(matrix);
	obj.interchange(matrix);
	obj.show_matrix(matrix);
}

main();

Output

 1 2 3 4
 5 6 7 8
 9 10 11 12
 13 14 15 16

 4 2 3 1
 5 7 6 8
 9 11 10 12
 16 14 15 13
# Python 3 Program
# Interchange the diagonals of a matrix
class MyMatrix :
	
	def __init__(self, matrix) :
		# Get the size of matrix
		# Assume that given matrix  size is NXN
		self.size = len(matrix)
	
	# Method which are perform swap diagonal elements
	def swap_diagonals(self, matrix, start_row, end_row, start_col, end_col) :
		if (start_row >= end_row or start_col >= end_col) :
			return
		
		temp = matrix[start_row][start_col]
		# Swap the element value
		matrix[start_row][start_col] = matrix[start_row][end_col]
		matrix[start_row][end_col] = temp
		temp = matrix[end_row][start_col]
		# Swap the element value
		matrix[end_row][start_col] = matrix[end_row][end_col]
		matrix[end_row][end_col] = temp
		self.swap_diagonals(matrix, start_row + 1, end_row - 1, start_col + 1, end_col - 1)
	
	# 
	def interchange(self, matrix) :
		self.swap_diagonals(matrix, 0, self.size - 1, 0, self.size - 1)
	
	# Display matrix elements
	def show_matrix(self, matrix) :
		i = 0
		while (i < self.size) :
			j = 0
			while (j < self.size) :
				print(" ", matrix[i][j], end = "")
				j += 1
			
			print("\n", end = "")
			i += 1
		
		print("\n", end = "")
	

def main() :
	matrix = [
		[1, 2, 3, 4],
		[5, 6, 7, 8],
		[9, 10, 11, 12],
		[13, 14, 15, 16]
	]
	obj = MyMatrix(matrix)
	obj.show_matrix(matrix)
	obj.interchange(matrix)
	obj.show_matrix(matrix)


if __name__ == "__main__":
	main()

Output

  1  2  3  4
  5  6  7  8
  9  10  11  12
  13  14  15  16

  4  2  3  1
  5  7  6  8
  9  11  10  12
  16  14  15  13
# Ruby Program
# Interchange the diagonals of a matrix
class MyMatrix 
	# Define the accessor and reader of class MyMatrix
    attr_reader :size
    attr_accessor :size
	def initialize(matrix) 
		# Get the size of matrix
		# Assume that given matrix  size is NXN
		self.size = matrix.length
	end
	# Method which are perform swap diagonal elements
	def swap_diagonals(matrix, start_row, end_row, start_col, end_col) 
		if (start_row >= end_row || start_col >= end_col) 
			return
		end
		temp = matrix[start_row][start_col]
		# Swap the element value
		matrix[start_row][start_col] = matrix[start_row][end_col]
		matrix[start_row][end_col] = temp
		temp = matrix[end_row][start_col]
		# Swap the element value
		matrix[end_row][start_col] = matrix[end_row][end_col]
		matrix[end_row][end_col] = temp
		self.swap_diagonals(matrix, start_row + 1, end_row - 1, start_col + 1, end_col - 1)
	end
	# 
	def interchange(matrix) 
		self.swap_diagonals(matrix, 0, self.size - 1, 0, self.size - 1)
	end
	# Display matrix elements
	def show_matrix(matrix) 
		i = 0
		while (i < self.size) 
			j = 0
			while (j < self.size) 
				print(" ", matrix[i][j])
				j += 1
			end
			print("\n")
			i += 1
		end
		print("\n")
	end
end
def main() 
	matrix = [
		[1, 2, 3, 4],
		[5, 6, 7, 8],
		[9, 10, 11, 12],
		[13, 14, 15, 16]
	]
	obj = MyMatrix.new(matrix)
	obj.show_matrix(matrix)
	obj.interchange(matrix)
	obj.show_matrix(matrix)
end


main()

Output

 1 2 3 4
 5 6 7 8
 9 10 11 12
 13 14 15 16

 4 2 3 1
 5 7 6 8
 9 11 10 12
 16 14 15 13

/*
  Scala Program
  Interchange the diagonals of a matrix
*/
class MyMatrix (var size: Int){

	def this(matrix: Array[Array[Int]]) {
		//Get the size of matrix
		//Assume that given matrix  size is NXN
		this(matrix.length);
	}
	//Method which are perform swap diagonal elements
	def swap_diagonals(matrix: Array[Array[Int]], start_row: Int, end_row: Int, start_col: Int, end_col: Int): Unit = {
		if (start_row >= end_row || start_col >= end_col) {
			return;
		}
		var temp: Int = matrix(start_row)(start_col);

		//Swap the element value
		matrix(start_row)(start_col) = matrix(start_row)(end_col);
		matrix(start_row)(end_col) = temp;
		temp = matrix(end_row)(start_col);

		//Swap the element value
		matrix(end_row)(start_col) = matrix(end_row)(end_col);
		matrix(end_row)(end_col) = temp;
		this.swap_diagonals(matrix, start_row + 1, end_row - 1, start_col + 1, end_col - 1);
	}
	//
	def interchange(matrix: Array[Array[Int]]): Unit = {
		this.swap_diagonals(matrix, 0, this.size - 1, 0, this.size - 1);
	}
	//Display matrix elements
	def show_matrix(matrix: Array[Array[Int]]): Unit = {
		var i: Int = 0;
		while (i < this.size) {
			var j: Int = 0;
			while (j < this.size) {
				print(" " + matrix(i)(j));
				j += 1;
			}
			print("\n");
			i += 1;
		}
		print("\n");
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		var matrix: Array[Array[Int]] = Array(
			Array(1, 2, 3, 4),
			Array(5, 6, 7, 8),
			Array(9, 10, 11, 12),
			Array(13, 14, 15, 16));
		val obj: MyMatrix = new MyMatrix(matrix);
		obj.show_matrix(matrix);
		obj.interchange(matrix);
		obj.show_matrix(matrix);
	}
}

Output

 1 2 3 4
 5 6 7 8
 9 10 11 12
 13 14 15 16

 4 2 3 1
 5 7 6 8
 9 11 10 12
 16 14 15 13
/*
  Swift Program
  Interchange the diagonals of a matrix
*/
class MyMatrix {
	var size: Int;
	init(_ matrix: [[Int]]) {
		//Get the size of matrix
		//Assume that given matrix  size is NXN
		self.size = matrix.count;
	}
	//Method which are perform swap diagonal elements
	func swap_diagonals(_ matrix: inout [[Int]], _ start_row: Int, _ end_row: Int, _ start_col: Int, _ end_col: Int) {
		if (start_row >= end_row || start_col >= end_col) {
			return;
		}
		var temp: Int = matrix[start_row][start_col];
		//Swap the element value
		matrix[start_row][start_col] = matrix[start_row][end_col];
		matrix[start_row][end_col] = temp;
		temp = matrix[end_row][start_col];
		//Swap the element value
		matrix[end_row][start_col] = matrix[end_row][end_col];
		matrix[end_row][end_col] = temp;
		self.swap_diagonals(&matrix, start_row + 1, end_row - 1, start_col + 1, end_col - 1);
	}
	//
	func interchange(_ matrix: inout [[Int]]) {
		self.swap_diagonals(&matrix, 0, self.size - 1, 0, self.size - 1);
	}
	//Display matrix elements
	func show_matrix(_ matrix: [[Int]]) {
		var i: Int = 0;
		while (i < self.size) {
			var j: Int = 0;
			while (j < self.size) {
				print(" ", matrix[i][j], terminator: "");
				j += 1;
			}
			print("\n", terminator: "");
			i += 1;
		}
		print("\n", terminator: "");
	}
}
func main() {
	var matrix: [[Int]] = [
		[1, 2, 3, 4],
		[5, 6, 7, 8],
		[9, 10, 11, 12],
		[13, 14, 15, 16]
	];
	let obj: MyMatrix = MyMatrix(matrix);
	obj.show_matrix(matrix);
	obj.interchange(&matrix);
	obj.show_matrix(matrix);
}
main();

Output

  1  2  3  4
  5  6  7  8
  9  10  11  12
  13  14  15  16

  4  2  3  1
  5  7  6  8
  9  11  10  12
  16  14  15  13

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved