Wave Print Row Wise [codingblocks]

Take as input a two-d array. Wave print it row-wise.


Input Format
Two integers M(row) and N(column) and further M * N integers(2-d array numbers).

Constraints
Both M and N are between 1 to 10.

Output Format
All M * N integers are seperated by commas with 'END' written in the end(as shown in example).

Sample Input
4 4
11 12 13 14
21 22 23 24
31 32 33 34
41 42 43 44

Sample Output
11, 12, 13, 14, 24, 23, 22, 21, 31, 32, 33, 34, 44, 43, 42, 41, END


Java Code:



/*
Amit Kumar
04-12-2020
IDE Code: https://ide.geeksforgeeks.org/EiHE96DfbN
*/

package array;
import java.util.Scanner;

public class Array_WavePrintRowWise {
    public static Scanner scanner = new Scanner(System.in);
    public static void main(String [] args) {
        int row = scanner.nextInt();
        int col = scanner.nextInt();
        int [][] twoDarray = new int[row][col];

        for (int i=0; i<row; i++) {
            for (int j=0; j<col; j++) {
                twoDarray[i][j] = scanner.nextInt();
            }
        }

        wavePrintRowWise(twoDarray);
    }

    private static void wavePrintRowWise(int[][] twoDarray) {
        for (int row=0; row<twoDarray.length; row++) {
            for (int col=0; col<twoDarray[row].length; col++) {
                if (row % 2 == 0) {
                    System.out.print(twoDarray[row][col]);
                } else {
                    System.out.print(twoDarray[row][twoDarray[row].length - col - 1]);
                }
                System.out.print(", ");
            }
        }
        System.out.println("END");
    }
}

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )