Skip to main content

Print a given matrix in waveform

The problem is to print a given matrix in waveform, which means displaying its elements in a zigzag manner. The waveform view starts from the top-left corner and goes down the first column, then up the second column, and continues this zigzag traversal until all elements are printed.

Example

Consider the following 7x6 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

The waveform view of this matrix would be:

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

Idea to Solve the Problem

To print the matrix in waveform view, we can follow these steps:

  1. Traverse the matrix using a loop.
  2. Start from the first column (index 0) and move from top to bottom (i.e., row-wise).
  3. Print all elements in the current column from top to bottom.
  4. Move to the next column (index 1) and move from bottom to top (i.e., reverse row-wise).
  5. Print all elements in the current column from bottom to top.
  6. Continue this process until all elements in the matrix are printed.

Algorithm

  1. Create a function wave_view(matrix) to perform the waveform printing.
  2. Initialize row and col as the number of rows and columns in the matrix.
  3. Initialize i = 0, j = 0, and c = 0 (to keep track of the current column).
  4. Loop through the matrix using the variable i from 0 to col, and increment c by 1 in each iteration.
  5. In each iteration of the loop, print all elements in the current column from top to bottom and then from bottom to top.
  6. Continue this process until all elements in the matrix are printed.

Pseudocode

wave_view(matrix):
    row = number of rows in matrix
    col = number of columns in matrix
    i = 0, j = 0, c = 0
    for i from 0 to col and increment c by 1 in each iteration:
        print all elements in the current column from top to bottom
        print all elements in the current column from bottom to top

Code Solution

/*
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

Output Explanation

The given Java code implements the above algorithm to print the matrix in waveform view. It prints the elements in the waveform traversal, as described in the output.

Time Complexity

The time complexity of the provided solution is O(M * N), where M is the number of rows and N is the number of columns in the matrix. The function traverses the entire matrix once, and in each iteration, it performs constant time operations to print the elements. Therefore, the overall time complexity is O(M * N).





Comment

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