Posted on by Kalkicode
Code Matrix

Print a given matrix in reverse waveform

The problem is to print a given matrix in reverse waveform pattern. In the reverse waveform pattern, the elements are printed in a specific order: first, the elements of the last column from top to bottom, then the elements of the second-to-last column from bottom to top, and so on, alternating between top-to-bottom and bottom-to-top for each column.

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

To print this matrix in reverse waveform, we start with the last column and move towards the first column. For each column, we print the elements in alternating top-to-bottom and bottom-to-top order.

Idea to Solve the Problem

To print a given matrix in reverse waveform pattern, we can follow these steps:

  1. Get the size of the matrix (number of rows and columns).
  2. Initialize variables i, j, and c, where i is the loop variable for columns, j is the loop variable for rows, and c represents the column index from which we start printing.
  3. Loop through the matrix columns in reverse order: a. Print the elements of the current column from top to bottom. b. Decrement c to move to the next column. c. If there are more columns remaining, print the elements of the current column from bottom to top. d. Decrement c again to move to the next column.
  4. Repeat the above steps until all columns have been processed.

Algorithm

  1. Create a function wave_view(matrix) to print the given matrix in reverse waveform pattern.
  2. Get the number of rows row and number of columns col in the matrix.
  3. Initialize i, j, and c to 0.
  4. Loop through the columns in reverse order (i from col - 1 to 0): a. Loop through the rows (j from 0 to row - 1): i. Print the element matrix[j][c]. b. Decrement c. c. If c is greater than or equal to 0, repeat the following steps: i. Loop through the rows (j from row - 1 to 0): - Print the element matrix[j][c]. ii. Decrement c.
  5. Print a newline to separate rows.

Pseudocode

wave_view(matrix):
    row = number of rows in matrix
    col = number of columns in matrix
    i = col - 1
    j = 0
    c = col - 1
    while i >= 0:
        for j from 0 to row - 1:
            print matrix[j][c]
        c = c - 1
        if c >= 0:
            for j from row - 1 to 0:
                print matrix[j][c]
            c = c - 1
        i = i - 1
        print newline
/*
C Program 
Print a given matrix in reverse waveform
*/
#include <stdio.h>

#define ROW 7
#define COL 6
void wave_view(int matrix[ROW][COL])
{
  int i = 0, j = 0, c = COL - 1;
  for (i = 0; i < COL && c >= 0; ++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 >= 0; --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

1 2 3 4 5 6 8 9 10 11 12 13 14 15 16 18 19 20 21 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 55 58 60
/*
  Java Program
  Print a given matrix in reverse 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 = col - 1;
    for (i = 0; i < col && c >= 0; ++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 >= 0; --j)
      {
        System.out.print(" " + 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

 1 2 3 4 5 6 8 9 10 11 12 13 14 15 16 18 19 20 21 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 55 58 60
/*
  C++ Program
  Print a given matrix in reverse 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 = col - 1;
    for (i = 0; i < col && c >= 0; ++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 >= 0; --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

 1 2 3 4 5 6 8 9 10 11 12 13 14 15 16 18 19 20 21 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 55 58 60
/*
  C# Program
  Print a given matrix in reverse 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 = col - 1;
    for (i = 0; i < col && c >= 0; 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 >= 0; 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

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

function main()
{
  $obj = new MyMatrix();
  $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

 1 2 3 4 5 6 8 9 10 11 12 13 14 15 16 18 19 20 21 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 55 58 60
/*
  Node Js Program
  Print a given matrix in reverse 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 = col - 1;
    for (i = 0; i < col && c >= 0; ++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 >= 0; --j)
      {
        process.stdout.write(" " + matrix[j][c]);
      }
      c--;
    }
  }
}

function main(args)
{
  var obj = new MyMatrix();
  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

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

class MyMatrix :
  def wave_view(self, matrix) :
    #  Get the size
    row = len(matrix)
    col = len(matrix[0])
    i = 0
    j = 0
    c = col - 1
    while (i < col and c >= 0) :
      # 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 >= 0) :
        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

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

class MyMatrix

  def wave_view(matrix)
  
    #  Get the size
    row = matrix.length
    col = matrix[0].length
    i = 0
    j = 0
    c = col - 1
    while (i < col && c >= 0)
    
      # 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 >= 0)
      
        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

 1 2 3 4 5 6 8 9 10 11 12 13 14 15 16 18 19 20 21 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 55 58 60
/*
  Scala Program
  Print a given matrix in reverse 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 = col - 1;
    while (i < col && c >= 0)
    {
      //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 >= 0)
      {
        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

 1 2 3 4 5 6 8 9 10 11 12 13 14 15 16 18 19 20 21 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 55 58 60
/*
  Swift Program
  Print a given matrix in reverse 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 = col - 1;
    while (i < col && c >= 0)
    {
      //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 >= 0)
      {
        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

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

Output Explanation

The above Java code implements the algorithm to print the matrix in reverse waveform pattern. It starts from the last column and alternates between printing elements top-to-bottom and bottom-to-top for each column. The output matches the expected reverse waveform pattern of the given matrix.

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 iterates through each column and for each column, it iterates through each row. Each element is printed exactly once. 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