Skip to main content

Print a given matrix in reverse waveform

Here given code implementation process.

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




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