Print Matrix in Wave Form
Given a matrix mat[][], print it in Wave Form.
Input: mat[][] = {{ 1, 2, 3, 4}
{ 5, 6, 7, 8}
{ 9, 10, 11, 12}
{13, 14, 15, 16}
{17, 18, 19, 20}}
Output: 1 5 9 13 17 18 14 10 6 2 3 7 11 15 19 20 16 12 8 4
Explanation: Output is printed in wave form.
Input: mat[][] = {{1, 9, 4, 10}
{ 3, 6, 90, 11}
{ 2, 30, 85, 72}
{ 6, 31, 99, 15}}
Output: 1 3 2 6 31 30 6 9 4 90 85 99 15 72 11 10
Approach: This problem is implementation-based and has a similar approach as discussed in this article. To get the desired waveform for a given matrix, first, print the elements of the first column of the matrix in the downward direction and then print the elements of the 2nd column in the upward direction, then print the elements in the third column in the downward direction and so on.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define R 5
#define C 4
void WavePrint( int m, int n, int arr[R][C])
{
for ( int j = 0; j < n; j++) {
if (j % 2 == 0) {
for ( int i = 0; i < m; i++) {
cout << arr[i][j] << " " ;
}
}
else {
for ( int i = m - 1; i >= 0; i--) {
cout << arr[i][j] << " " ;
}
}
}
}
int main()
{
int arr[R][C] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 },
{ 17, 18, 19, 20 } };
WavePrint(R, C, arr);
return 0;
}
|
C
#include <stdio.h>
#define R 5
#define C 4
void WavePrint( int m, int n, int arr[R][C])
{
for ( int j = 0; j < n; j++) {
if (j % 2 == 0) {
for ( int i = 0; i < m; i++) {
printf ( "%d " , arr[i][j]);
}
}
else {
for ( int i = m - 1; i >= 0; i--) {
printf ( "%d " , arr[i][j]);
}
}
}
}
int main()
{
int arr[R][C] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 },
{ 13, 14, 15, 16 } };
WavePrint(R, C, arr);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int R = 5 ;
public static int C = 4 ;
public static void WavePrint( int m, int n, int [][] arr)
{
for ( int j = 0 ; j < n; j++) {
if (j % 2 == 0 ) {
for ( int i = 0 ; i < m; i++) {
System.out.print(arr[i][j] + " " );
}
}
else {
for ( int i = m - 1 ; i >= 0 ; i--) {
System.out.print(arr[i][j] + " " );
}
}
}
}
public static void main (String[] args)
{
int [][] arr = { { 1 , 2 , 3 , 4 },
{ 5 , 6 , 7 , 8 },
{ 9 , 10 , 11 , 12 },
{ 13 , 14 , 15 , 16 },
{ 17 , 18 , 19 , 20 } };
WavePrint(R, C, arr);
}
}
|
Python3
R = 5
C = 4
def WavePrint(m, n, arr):
for j in range (n):
if (j % 2 = = 0 ):
for i in range (m):
print (arr[i][j], end = " " )
else :
for i in range (m - 1 , - 1 , - 1 ):
print (arr[i][j], end = " " )
arr = [[ 1 , 2 , 3 , 4 ], [ 5 , 6 , 7 , 8 ], [ 9 , 10 , 11 , 12 ], [ 13 , 14 , 15 , 16 ], [ 17 , 18 , 19 , 20 ]]
WavePrint(R, C, arr)
|
C#
using System;
class GFG{
public static int R = 5;
public static int C = 4;
public static void WavePrint( int m, int n, int [,] arr)
{
for ( int j = 0; j < n; j++)
{
if (j % 2 == 0)
{
for ( int i = 0; i < m; i++)
{
Console.Write(arr[i, j] + " " );
}
}
else
{
for ( int i = m - 1; i >= 0; i--)
{
Console.Write(arr[i, j] + " " );
}
}
}
}
public static void Main ()
{
int [,] arr = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 },
{ 17, 18, 19, 20 } };
WavePrint(R, C, arr);
}
}
|
Javascript
<script>
let R = 5
let C = 4
function WavePrint(m, n, arr)
{
for (let j = 0; j < n; j++) {
if (j % 2 == 0) {
for (let i = 0; i < m; i++) {
document.write(arr[i][j] + " " )
}
}
else {
for (let i = m - 1; i >= 0; i--) {
document.write(arr[i][j] + " " )
}
}
}
}
let arr = [[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
[17, 18, 19, 20]];
WavePrint(R, C, arr);
</script>
|
Output
1 5 9 13 17 18 14 10 6 2 3 7 11 15 19 20 16 12 8 4
Time Complexity: O(N2)
Auxiliary Space: O(1)
Last Updated :
31 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...