Print a given matrix in waveform

Here given code implementation process.

/*
C Program 
Print a given matrix in waveform
*/
#include <stdio.h>
#define ROW 7
#define COL 6
void wave_view(int matrix[ROW][COL])
{
	int i = 0, j = 0, c = 0;
	for (i = 0; i < COL && c < COL; ++i)
	{
		//Display top to bottom element
		for (j = 0; j < ROW; ++j)
		{
			printf("%d ", matrix[j][c]);
		}
		c++;
		//Display bottom to top element
		for (j = ROW - 1; j >= 0 && c < COL; --j)
		{
			printf("%d ", matrix[j][c]);
		}
		c++;
	}
}
int main()
{
	int matrix[ROW][COL] = {
      {60,40,39,16,15,1},
      {58,41,38,18,14,2},
      {55,42,37,19,13,3},
      {50,43,36,20,12,4},
      {49,44,35,21,11,5},
      {48,45,34,31,10,6},
      {47,46,33,32,9, 8}
	};
	wave_view(matrix);
	return 0;
}

Output

60 58 55 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 21 20 19 18 16 15 14 13 12 11 10 9 8 6 5 4 3 2 1
/*
Java Program
Print a given matrix in waveform
*/
class MyMatrix
{
	public void wave_view(int[][] matrix)
	{
		// Get the size
		int row = matrix.length;
		int col = matrix[0].length;
		int i = 0, j = 0, c = 0;
		for (i = 0; i < col && c < col; ++i)
		{
			//Display top to bottom element
			for (j = 0; j < row; ++j)
			{
				System.out.print(" " + matrix[j][c]);
			}
			c++;
			//Display bottom to top element
			for (j = row - 1; j >= 0 && c < col; --j)
			{
				System.out.print(" " + matrix[j][c]);
			}
			c++;
		}
	}
	public static void main(String[] args)
	{
		MyMatrix obj = new MyMatrix();
		//Define matrix elements
		int[][] matrix = {
          {60,40,39,16,15,1},
          {58,41,38,18,14,2},
          {55,42,37,19,13,3},
          {50,43,36,20,12,4},
          {49,44,35,21,11,5},
          {48,45,34,31,10,6},
          {47,46,33,32,9, 8}
		};
		obj.wave_view(matrix);
	}
}

Output

 60 58 55 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 21 20 19 18 16 15 14 13 12 11 10 9 8 6 5 4 3 2 1
/*
C++ Program
Print a given matrix in waveform
*/
#include<iostream>
#define ROW 7
#define COL 6
using namespace std;
class MyMatrix
{
	public: void wave_view(int matrix[ROW][COL])
	{
		// Get the size
		int row = ROW;
		int col = COL;
		int i = 0, j = 0, c = 0;
		for (i = 0; i < col && c < col; ++i)
		{
			//Display top to bottom element
			for (j = 0; j < row; ++j)
			{
				cout << " " << matrix[j][c];
			}
			c++;
			//Display bottom to top element
			for (j = row - 1; j >= 0 && c < col; --j)
			{
				cout << " " << matrix[j][c];
			}
			c++;
		}
	}
};
int main()
{
	MyMatrix obj = MyMatrix();
	int matrix[ROW][COL] = {
		{
			60,
			40,
			39,
			16,
			15,
			1
		},
		{
			58,
			41,
			38,
			18,
			14,
			2
		},
		{
			55,
			42,
			37,
			19,
			13,
			3
		},
		{
			50,
			43,
			36,
			20,
			12,
			4
		},
		{
			49,
			44,
			35,
			21,
			11,
			5
		},
		{
			48,
			45,
			34,
			31,
			10,
			6
		},
		{
			47,
			46,
			33,
			32,
			9,
			8
		}
	};
	obj.wave_view(matrix);
	return 0;
}

Output

 60 58 55 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 21 20 19 18 16 15 14 13 12 11 10 9 8 6 5 4 3 2 1
/*
C# Program
Print a given matrix in waveform
*/
using System;
class MyMatrix
{
	public void wave_view(int[,] matrix)
	{
		// Get the size
		int row = matrix.GetLength(0);
		int col = matrix.GetLength(1);
		int i = 0, j = 0, c = 0;
		for (i = 0; i < col && c < col; i++)
		{
			//Display top to bottom element
			for (j = 0; j < row; j++)
			{
				Console.Write(" " + matrix[j,c]);
			}
			c++;
			//Display bottom to top element
			for (j = row - 1; j >= 0 && c < col; j--)
			{
				Console.Write(" " + matrix[j,c]);
			}
			c++;
		}
	}
	public static void Main(String[] args)
	{
		MyMatrix obj = new MyMatrix();
		int[,] matrix = {
			{
				60,
				40,
				39,
				16,
				15,
				1
			},
			{
				58,
				41,
				38,
				18,
				14,
				2
			},
			{
				55,
				42,
				37,
				19,
				13,
				3
			},
			{
				50,
				43,
				36,
				20,
				12,
				4
			},
			{
				49,
				44,
				35,
				21,
				11,
				5
			},
			{
				48,
				45,
				34,
				31,
				10,
				6
			},
			{
				47,
				46,
				33,
				32,
				9,
				8
			}
		};
		obj.wave_view(matrix);
	}
}

Output

 60 58 55 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 21 20 19 18 16 15 14 13 12 11 10 9 8 6 5 4 3 2 1
<?php
/*
Php Program
Print a given matrix in waveform
*/
class MyMatrix
{
	public 	function wave_view( & $matrix)
	{
		// Get the size
		$row = count($matrix);
		$col = count($matrix[0]);
		$i = 0;
		$j = 0;
		$c = 0;
		for ($i = 0; $i < $col && $c < $col; ++$i)
		{
			//Display top to bottom element
			for ($j = 0; $j < $row; ++$j)
			{
				echo(" ". $matrix[$j][$c]);
			}
			$c++;
			//Display bottom to top element
			for ($j = $row - 1; $j >= 0 && $c < $col; --$j)
			{
				echo(" ". $matrix[$j][$c]);
			}
			$c++;
		}
	}
}

function main()
{
	$obj = new MyMatrix();
	//Define matrix elements
	$matrix = array(
      array(60, 40, 39, 16, 15, 1), 
      array(58, 41, 38, 18, 14, 2), 
      array(55, 42, 37, 19, 13, 3), 
      array(50, 43, 36, 20, 12, 4), 
      array(49, 44, 35, 21, 11, 5), 
      array(48, 45, 34, 31, 10, 6), 
      array(47, 46, 33, 32, 9, 8));
	$obj->wave_view($matrix);
}
main();

Output

 60 58 55 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 21 20 19 18 16 15 14 13 12 11 10 9 8 6 5 4 3 2 1
/*
Node Js Program
Print a given matrix in waveform
*/
class MyMatrix
{
	wave_view(matrix)
	{
		// Get the size
		var row = matrix.length;
		var col = matrix[0].length;
		var i = 0;
		var j = 0;
		var c = 0;
		for (i = 0; i < col && c < col; ++i)
		{
			//Display top to bottom element
			for (j = 0; j < row; ++j)
			{
				process.stdout.write(" " + matrix[j][c]);
			}
			c++;
			//Display bottom to top element
			for (j = row - 1; j >= 0 && c < col; --j)
			{
				process.stdout.write(" " + matrix[j][c]);
			}
			c++;
		}
	}
}

function main(args)
{
	var obj = new MyMatrix();
	//Define matrix elements
	var matrix = [
		[60, 40, 39, 16, 15, 1],
		[58, 41, 38, 18, 14, 2],
		[55, 42, 37, 19, 13, 3],
		[50, 43, 36, 20, 12, 4],
		[49, 44, 35, 21, 11, 5],
		[48, 45, 34, 31, 10, 6],
		[47, 46, 33, 32, 9, 8]
	];
	obj.wave_view(matrix);
}
main();

Output

 60 58 55 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 21 20 19 18 16 15 14 13 12 11 10 9 8 6 5 4 3 2 1
# 
# Python 3 Program
# Print a given matrix in waveform

class MyMatrix :
	def wave_view(self, matrix) :
		#  Get the size
		row = len(matrix)
		col = len(matrix[0])
		i = 0
		j = 0
		c = 0
		while (i < col and c < col) :
			# Display top to bottom element
			j = 0
			while (j < row) :
				print(" ", matrix[j][c], end = "")
				j += 1
			
			c += 1
			# Display bottom to top element
			j = row - 1
			while (j >= 0 and c < col) :
				print(" ", matrix[j][c], end = "")
				j -= 1
			
			c += 1
			i += 1
		
	

def main() :
	obj = MyMatrix()
	matrix = [
		[60, 40, 39, 16, 15, 1],
		[58, 41, 38, 18, 14, 2],
		[55, 42, 37, 19, 13, 3],
		[50, 43, 36, 20, 12, 4],
		[49, 44, 35, 21, 11, 5],
		[48, 45, 34, 31, 10, 6],
		[47, 46, 33, 32, 9, 8]
	]
	obj.wave_view(matrix)


if __name__ == "__main__": main()

Output

  60  58  55  50  49  48  47  46  45  44  43  42  41  40  39  38  37  36  35  34  33  32  31  21  20  19  18  16  15  14  13  12  11  10  9  8  6  5  4  3  2  1
# Ruby Program
# Print a given matrix in waveform

class MyMatrix

	def wave_view(matrix)
	
		#  Get the size
		row = matrix.length
		col = matrix[0].length
		i = 0
		j = 0
		c = 0
		while (i < col && c < col)
		
			# Display top to bottom element
			j = 0
			while (j < row)
			
				print(" ", matrix[j][c])
				j += 1
			end
			c += 1
			# Display bottom to top element
			j = row - 1
			while (j >= 0 && c < col)
			
				print(" ", matrix[j][c])
				j -= 1
			end
			c += 1
			i += 1
		end
	end
end
def main()

	obj = MyMatrix.new()
	matrix = [
		[60, 40, 39, 16, 15, 1],
		[58, 41, 38, 18, 14, 2],
		[55, 42, 37, 19, 13, 3],
		[50, 43, 36, 20, 12, 4],
		[49, 44, 35, 21, 11, 5],
		[48, 45, 34, 31, 10, 6],
		[47, 46, 33, 32, 9, 8]
	]
	obj.wave_view(matrix)
end
main()

Output

 60 58 55 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 21 20 19 18 16 15 14 13 12 11 10 9 8 6 5 4 3 2 1
/*
Scala Program
Print a given matrix in waveform
*/
class MyMatrix
{
	def wave_view(matrix: Array[Array[Int]]): Unit = {
		// Get the size
		var row: Int = matrix.length;
		var col: Int = matrix(0).length;
		var i: Int = 0;
		var j: Int = 0;
		var c: Int = 0;
		while (i < col && c < col)
		{
			//Display top to bottom element
			j = 0;
			while (j < row)
			{
				print(" " + matrix(j)(c));
				j += 1;
			}
			c += 1;
			//Display bottom to top element
			j = row - 1;
			while (j >= 0 && c < col)
			{
				print(" " + matrix(j)(c));
				j -= 1;
			}
			c += 1;
			i += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyMatrix = new MyMatrix();
		var matrix: Array[Array[Int]] = Array(
          Array(60, 40, 39, 16, 15, 1), 
          Array(58, 41, 38, 18, 14, 2), 
          Array(55, 42, 37, 19, 13, 3), 
          Array(50, 43, 36, 20, 12, 4), 
          Array(49, 44, 35, 21, 11, 5), 
          Array(48, 45, 34, 31, 10, 6), 
          Array(47, 46, 33, 32, 9, 8));
		obj.wave_view(matrix);
	}
}

Output

 60 58 55 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 21 20 19 18 16 15 14 13 12 11 10 9 8 6 5 4 3 2 1
/*
Swift Program
Print a given matrix in waveform
*/
class MyMatrix
{
	func wave_view(_ matrix: [
		[Int]
	])
	{
		// Get the size
		let row: Int = matrix.count;
		let col: Int = matrix[0].count;
		var i: Int = 0;
		var j: Int = 0;
		var c: Int = 0;
		while (i < col && c < col)
		{
			//Display top to bottom element
			j = 0;
			while (j < row)
			{
				print(" ", matrix[j][c], terminator: "");
				j += 1;
			}
			c += 1;
			//Display bottom to top element
			j = row - 1;
			while (j >= 0 && c < col)
			{
				print(" ", matrix[j][c], terminator: "");
				j -= 1;
			}
			c += 1;
			i += 1;
		}
	}
}
func main()
{
	let obj: MyMatrix = MyMatrix();
	let matrix: [
		[Int]
	] = [
		[60, 40, 39, 16, 15, 1],
		[58, 41, 38, 18, 14, 2],
		[55, 42, 37, 19, 13, 3],
		[50, 43, 36, 20, 12, 4],
		[49, 44, 35, 21, 11, 5],
		[48, 45, 34, 31, 10, 6],
		[47, 46, 33, 32, 9, 8]
	];
	obj.wave_view(matrix);
}
main();

Output

  60  58  55  50  49  48  47  46  45  44  43  42  41  40  39  38  37  36  35  34  33  32  31  21  20  19  18  16  15  14  13  12  11  10  9  8  6  5  4  3  2  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