Pattern wave print row wise

what is wrong with my code------

import java.util.*;

public class Main {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
int m = cin.nextInt(),n = cin.nextInt() ,arr[][] = new int[m][n],status = 0;
for(int i = 0; i < arr.length; i++){
for(int j = 0; j<arr[i].length;j++){
arr[i][j] = cin.nextInt();
}
}
for(int i = 0; i < arr.length; i++){
if(status==1){
for(int j = arr[i].length - 1; j>-1;j–){
if(i==m-1&& j==n-1){
System.out.print(+arr[i][j]+","+" “+“END”);
}else{
System.out.print(+arr[i][j]+”,"+" “);
}
}
status =0;
}else{
for(int j = 0; j<arr[i].length;j++){
if(i==m-1&& j==n-1){
System.out.print(+arr[i][j]+”,"+" “+“END”);
}else{
System.out.print(+arr[i][j]+”,"+" ");
}
}
status = 1;
}
}
cin.close();
}
}

If the matrix has even number of rows, then the last row will be printed backwards. But in your code, you are printing END as soon as the last element of the matrix is encountered. Taking a sample test case :

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

Here output should be : 11, 12, 13, 14, 24, 23, 22, 21, 31, 32, 33, 34, 44, 43, 42, 41, END
but your output is : 11, 12, 13, 14, 24, 23, 22, 21, 31, 32, 33, 34, 44, END 43, 42, 41,

You can correct your code by printing END when all elements have printed by keeping a count variable, which should be equal to n*m in the end.

i have teird ur soution but it is not working

You don’t have to add ‘+’ in the beginning of the print statement. It should be

System.out.print(arr[i][j] + ”," + " ");

And please share your updated code so I can see where it is going wrong. Paste it on ide.codingblocks.com, save it and share the link here