Swap the elements of lower and upper triangular matrix

Swap lower and upper triangular matrix element

Here given code implementation process.

/*
  C Program 
  Swap the elements of lower and upper triangular matrix
*/
#include<stdio.h>
#define ROW 4
#define COL 4
//interchange the elements of lower and upper triangular matrix
void swap_element(int arr[ROW][COL])
{
  int temp=0;
  for (int i = 0; i < COL ; ++i)
  {
    for (int j = i+1; j < ROW ; ++j)
    {
      //swap the element
      temp=arr[i][j];
      arr[i][j]=arr[j][i];
      arr[j][i]=temp;
    }
    
  }

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

  int arr[ROW][COL]=  { 
    { 6, 1, 5, 3 }, 
    { 0, 3, 2, 6 }, 
    { 4, 0, 4, 7 },
    { 1, 2, 9, 1 }  }; 
  show_data(arr);
  swap_element(arr);
  show_data(arr);
  return 0;
}

Output

  6  1  5  3
  0  3  2  6
  4  0  4  7
  1  2  9  1

  6  0  4  1
  1  3  0  2
  5  2  4  9
  3  6  7  1
/*
  C++ Program
  Swap the elements of lower and upper triangular matrix
*/
#include<iostream>
#define SIZE 4
using namespace std;

class MyMatrix {
	public:
		int rows;
	int cols;
	MyMatrix() {
		//Get the size of array
		//Assuming this is a square matrix of NXN
		this->rows = SIZE;
		this->cols = SIZE;
	}
	//interchange the elements of lower and upper triangular matrix
	void swap_element(int matrix[][SIZE]) {
		int temp = 0;
		for (int i = 0; i < this->cols; ++i) {
			for (int j = i + 1; j < this->rows; ++j) {
				//swap the element
				temp = matrix[i][j];
				matrix[i][j] = matrix[j][i];
				matrix[j][i] = temp;
			}
		}
	}
	//Display matrix elements
	void show_data(int matrix[][SIZE]) {
		for (int i = 0; i < this->rows; ++i) {
			for (int j = 0; j < this->cols; ++j) {
				cout << " " << matrix[i][j];
			}
			cout << "\n";
		}
		cout << "\n";
	}
};
int main() {
	int matrix[][SIZE] = {
		{
			6,
			1,
			5,
			3
		},
		{
			0,
			3,
			2,
			6
		},
		{
			4,
			0,
			4,
			7
		},
		{
			1,
			2,
			9,
			1
		}
	};
	MyMatrix obj = MyMatrix();
	obj.show_data(matrix);
	obj.swap_element(matrix);
	obj.show_data(matrix);
	return 0;
}

Output

 6 1 5 3
 0 3 2 6
 4 0 4 7
 1 2 9 1

 6 0 4 1
 1 3 0 2
 5 2 4 9
 3 6 7 1
/*
  Java Program
  Swap the elements of lower and upper triangular matrix
*/

public class MyMatrix 
{
  public int rows;
  public int cols;
  public MyMatrix(int [][]matrix)
  {
    //Get the size of array
    //Assuming this is a square matrix of NXN
    rows= matrix.length;
    cols= rows;
  }
  //interchange the elements of lower and upper triangular matrix
  public void swap_element(int [][]matrix)
  {
    int temp=0;
    for (int i = 0; i < cols ; ++i)
    {
      for (int j = i+1; j < rows ; ++j)
      {
        //swap the element
        temp=matrix[i][j];
        matrix[i][j]=matrix[j][i];
        matrix[j][i]=temp;
      }
      
    }

  }
  //Display matrix elements
  void show_data(int [][]matrix)
  {
    for (int i = 0; i < rows; ++i)
    {
      for (int j = 0; j < cols; ++j)
      {
        System.out.print("  "+matrix[i][j] );
      }
      System.out.print("\n");
    }
    System.out.print("\n");
  }
  public static void main(String[] args) {
    //matrix elements
    int [][]matrix =  { 
      { 6, 1, 5, 3 }, 
      { 0, 3, 2, 6 }, 
      { 4, 0, 4, 7 },
      { 1, 2, 9, 1 }  
    }; 
    MyMatrix obj = new MyMatrix(matrix);
    obj.show_data(matrix);
    obj.swap_element(matrix);
    obj.show_data(matrix);

  }
}

Output

 6 1 5 3
 0 3 2 6
 4 0 4 7
 1 2 9 1

 6 0 4 1
 1 3 0 2
 5 2 4 9
 3 6 7 1
using System;

/*
  C# Program
  Swap the elements of lower and upper triangular matrix
*/

public class MyMatrix {
	int rows;
	int cols;
	MyMatrix(int[,] matrix) {
		//Get the size of array
		//Assuming this is a square matrix of NXN
		rows = matrix.GetLength(0);
		cols = rows;
	}
	//interchange the elements of lower and upper triangular matrix
	public void swap_element(int[,] matrix) {
		int temp = 0;
		for (int i = 0; i < cols; ++i) {
			for (int j = i + 1; j < rows; ++j) {
				//swap the element
				temp = matrix[i,j];
				matrix[i,j] = matrix[j,i];
				matrix[j,i] = temp;
			}
		}
	}
	//Display matrix elements
	void show_data(int[,] matrix) {
		for (int i = 0; i < rows; ++i) {
			for (int j = 0; j < cols; ++j) {
				Console.Write(" " + matrix[i,j]);
			}
			Console.Write("\n");
		}
		Console.Write("\n");
	}
	public static void Main(String[] args) {
		int[,]
		//matrix elements
		matrix = {
			{
				6,
				1,
				5,
				3
			},
			{
				0,
				3,
				2,
				6
			},
			{
				4,
				0,
				4,
				7
			},
			{
				1,
				2,
				9,
				1
			}
		};
		MyMatrix obj = new MyMatrix(matrix);
		obj.show_data(matrix);
		obj.swap_element(matrix);
		obj.show_data(matrix);
	}
}

Output

 6 1 5 3
 0 3 2 6
 4 0 4 7
 1 2 9 1

 6 0 4 1
 1 3 0 2
 5 2 4 9
 3 6 7 1
<?php
/*
  Php Program
  Swap the elements of lower and upper triangular matrix
*/
class MyMatrix {
	public $rows;
	public $cols;

	function __construct($matrix) {
		//Get the size of array
		//Assuming this is a square matrix of NXN
		$this->rows = count($matrix);
		$this->cols = $this->rows;
	}
	//interchange the elements of lower and upper triangular matrix

	public 	function swap_element(&$matrix) {
		$temp = 0;
		for ($i = 0; $i < $this->cols; ++$i) {
			for ($j = $i + 1; $j < $this->rows; ++$j) {
				//swap the element
				$temp = $matrix[$i][$j];
				$matrix[$i][$j] = $matrix[$j][$i];
				$matrix[$j][$i] = $temp;
			}
		}
	}
	//Display matrix elements
	function show_data($matrix) {
		for ($i = 0; $i < $this->rows; ++$i) {
			for ($j = 0; $j < $this->cols; ++$j) {
				echo(" ". $matrix[$i][$j]);
			}
			echo("\n");
		}
		echo("\n");
	}
}

function main() {
	//matrix elements
	$matrix = array(
      array(6, 1, 5, 3), 
      array(0, 3, 2, 6), 
      array(4, 0, 4, 7), 
      array(1, 2, 9, 1));
	$obj = new MyMatrix($matrix);
	$obj->show_data($matrix);
	$obj->swap_element($matrix);
	$obj->show_data($matrix);

}
main();

Output

 6 1 5 3
 0 3 2 6
 4 0 4 7
 1 2 9 1

 6 0 4 1
 1 3 0 2
 5 2 4 9
 3 6 7 1
/*
  Node Js Program
  Swap the elements of lower and upper triangular matrix
*/
class MyMatrix {
	
	constructor(matrix) {
		//Get the size of array
		//Assuming this is a square matrix of NXN
		this.rows = matrix.length;
		this.cols = this.rows;
	}

	//interchange the elements of lower and upper triangular matrix
	swap_element(matrix) {
		var temp = 0;
		for (var i = 0; i < this.cols; ++i) {
			for (var j = i + 1; j < this.rows; ++j) {
				//swap the element
				temp = matrix[i][j];
				matrix[i][j] = matrix[j][i];
				matrix[j][i] = temp;
			}
		}
	}

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

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

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

function main(args) {
	//matrix elements
	var matrix = [
		[6, 1, 5, 3],
		[0, 3, 2, 6],
		[4, 0, 4, 7],
		[1, 2, 9, 1]
	];
	var obj = new MyMatrix(matrix);
	obj.show_data(matrix);
	obj.swap_element(matrix);
	obj.show_data(matrix);
}

main();

Output

 6 1 5 3
 0 3 2 6
 4 0 4 7
 1 2 9 1

 6 0 4 1
 1 3 0 2
 5 2 4 9
 3 6 7 1
# Python 3 Program
# Swap the elements of lower and upper triangular matrix
class MyMatrix :
	
	def __init__(self, matrix) :
		# Get the size of array
		# Assuming this is a square matrix of NXN
		self.rows = len(matrix)
		self.cols = self.rows
	
	# interchange the elements of lower and upper triangular matrix
	def swap_element(self, matrix) :
		temp = 0
		i = 0
		while (i < self.cols) :
			j = i + 1
			while (j < self.rows) :
				# swap the element
				temp = matrix[i][j]
				matrix[i][j] = matrix[j][i]
				matrix[j][i] = temp
				j += 1
			
			i += 1
		
	
	def show_data(self, matrix) :
		i = 0
		while (i < self.rows) :
			j = 0
			while (j < self.cols) :
				print(" ", matrix[i][j], end = "")
				j += 1
			
			print("\n", end = "")
			i += 1
		
		print("\n", end = "")
	

def main() :
	matrix = [
		[6, 1, 5, 3],
		[0, 3, 2, 6],
		[4, 0, 4, 7],
		[1, 2, 9, 1]
	]
	obj = MyMatrix(matrix)
	obj.show_data(matrix)
	obj.swap_element(matrix)
	obj.show_data(matrix)


if __name__ == "__main__":
	main()

Output

  6  1  5  3
  0  3  2  6
  4  0  4  7
  1  2  9  1

  6  0  4  1
  1  3  0  2
  5  2  4  9
  3  6  7  1
#   Ruby Program
#   Swap the elements of lower and upper triangular matrix
class MyMatrix 
	# Define the accessor and reader of class MyMatrix
    attr_reader :rows, :cols
    attr_accessor :rows, :cols
	def initialize(matrix) 
		# Get the size of array
		# Assuming this is a square matrix of NXN
		@rows = matrix.length
		@cols = @rows
	end
	# interchange the elements of lower and upper triangular matrix
	def swap_element(matrix) 
		temp = 0
		i = 0
		while (i < @cols) 
			j = i + 1
			while (j < @rows) 
				# swap the element
				temp = matrix[i][j]
				matrix[i][j] = matrix[j][i]
				matrix[j][i] = temp
				j += 1
			end
			i += 1
		end
	end
	def show_data(matrix) 
		i = 0
		while (i < @rows) 
			j = 0
			while (j < @cols) 
				print(" ", matrix[i][j])
				j += 1
			end
			print("\n")
			i += 1
		end
		print("\n")
	end
end
def main() 
	matrix = [
		[6, 1, 5, 3],
		[0, 3, 2, 6],
		[4, 0, 4, 7],
		[1, 2, 9, 1]
	]
	obj = MyMatrix.new(matrix)
	obj.show_data(matrix)
	obj.swap_element(matrix)
	obj.show_data(matrix)
end


main()

Output

 6 1 5 3
 0 3 2 6
 4 0 4 7
 1 2 9 1

 6 0 4 1
 1 3 0 2
 5 2 4 9
 3 6 7 1

/*
  Scala Program
  Swap the elements of lower and upper triangular matrix
*/
class MyMatrix(var rows: Int,
		var cols: Int) {
	

	def this(matrix: Array[Array[Int]]) {
		//Get the size of array
		//Assuming this is a square matrix of NXN
		this(matrix.length,matrix.length);
	}
	//interchange the elements of lower and upper triangular matrix
	def swap_element(matrix: Array[Array[Int]]): Unit = {
		var temp: Int = 0;
		var i: Int = 0;
		while (i < this.cols) {
			var j: Int = i + 1;
			while (j < this.rows) {
				//swap the element
				temp = matrix(i)(j);
				matrix(i)(j) = matrix(j)(i);
				matrix(j)(i) = temp;
				j += 1;
			}
			i += 1;
		}
	}
	def show_data(matrix: Array[Array[Int]]): Unit = {
		var i: Int = 0;
		while (i < this.rows) {
			var j: Int = 0;
			while (j < this.cols) {
				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(6, 1, 5, 3),
			Array(0, 3, 2, 6),
			Array(4, 0, 4, 7),
			Array(1, 2, 9, 1));
		val obj: MyMatrix = new MyMatrix(matrix);
		obj.show_data(matrix);
		obj.swap_element(matrix);
		obj.show_data(matrix);
	}
}

Output

 6 1 5 3
 0 3 2 6
 4 0 4 7
 1 2 9 1

 6 0 4 1
 1 3 0 2
 5 2 4 9
 3 6 7 1
/*
  Swift Program
  Swap the elements of lower and upper triangular matrix
*/
class MyMatrix {
	var rows: Int;
	var cols: Int;
	init(_ matrix: [
		[Int]
	]) {
		//Get the size of array
		//Assuming this is a square matrix of NXN
		self.rows = matrix.count;
		self.cols = self.rows;
	}
	//interchange the elements of lower and upper triangular matrix
	func swap_element(_ matrix: inout [
		[Int]
	]) {
		var temp: Int = 0;
		var i: Int = 0;
		while (i < self.cols) {
			var j: Int = i + 1;
			while (j < self.rows) {
				//swap the element
				temp = matrix[i][j];
				matrix[i][j] = matrix[j][i];
				matrix[j][i] = temp;
				j += 1;
			}
			i += 1;
		}
	}
	func show_data(_ matrix: [
		[Int]
	]) {
		var i: Int = 0;
		while (i < self.rows) {
			var j: Int = 0;
			while (j < self.cols) {
				print(" ", matrix[i][j], terminator: "");
				j += 1;
			}
			print("\n", terminator: "");
			i += 1;
		}
		print("\n", terminator: "");
	}
}
func main() {
	var matrix: [
		[Int]
	] = [
		[6, 1, 5, 3],
		[0, 3, 2, 6],
		[4, 0, 4, 7],
		[1, 2, 9, 1]
	];
	let obj: MyMatrix = MyMatrix(matrix);
	obj.show_data(matrix);
	obj.swap_element(&matrix);
	obj.show_data(matrix);
}
main();

Output

  6  1  5  3
  0  3  2  6
  4  0  4  7
  1  2  9  1

  6  0  4  1
  1  3  0  2
  5  2  4  9
  3  6  7  1


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