In: Computer Science
Java Program
// DO NOT USE BREAK OR CONTINUE :)
You are allowed to use the following methods from the Java API:
moveXDownLeft takes a char and a two dimensional array of char
as input and returns nothing:
The method should find the first occurrence of the char in
the array (searching from "top" to "bottom" and "left" to "right"),
and it should slide that character in the array down and to the
left as far as it can go. Any characters on that diagonal are slide
up and to the right to fill. Do not use Strings to solve this
problem.
> char[][] board = {{'a','b','c','X'},{'d'},{'e','f','g','h'},{'i','j','k'},{'l','m','n','o'}}; > HW2.moveXDownLeft('X', board) > board { { a, b, c, f }, { d }, { e, i, g, h }, { X, j, k }, { l, m, n, o } }
CODE IN JAVA:
Board.java file:
public class Board {
public static void moveXDownLeft(char ch , char[][]
board) {
int row=-1,col=-1;
for(int i=0;i<board.length;i++)
{
for(int
j=0;j<board[i].length;j++) {
if(board[i][j]==ch) {
if(row==-1) {
row = i
;
col = j
;
}
}
}
}
if(row!=-1) {
System.out.println("character is found at row = "+(row+1)+", column
= "+(col+1));
}
else {
System.out.println("Your character is not found....");
}
}
public static void main(String[] args) {
char[][] board =
{{'a','b','c','X'},{'d'},{'e','f','g','h'},{'i','j','k'},{'l','m','n','o'}};
moveXDownLeft('X', board);
}
}
OUTPUT: