How do I convert this while loop to tail recursion?
private void markForward (int row, int col, int distance,
int oldMarker,
int newMarker) {
while (col+ distance < BOARD_SIZE) {
if(board[row][col+ distance] ==
oldMarker ) {
board[row][col+
distance] = newMarker;
//markForward
(row, col, distance, oldMarker, newMarker);
}
if(row+distance < BOARD_SIZE
&& board[row+distance][col+distance] == oldMarker) {
board[row+distance][col+distance] = newMarker;
}
if(row-distance > 0 &&
board[row-distance][col+distance] == oldMarker) {
board[row-distance][col+distance] = newMarker;
//markForward
(row, col, distance, oldMarker, newMarker);
}
distance++;
Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate
the question. Thank You So Much.
private void markForward (int row, int col, int distance,
int oldMarker, int newMarker) {
if(col+ distance < BOARD_SIZE) {
if(board[row][col+ distance] == oldMarker ) {
board[row][col+ distance] = newMarker;
}
if(row+distance < BOARD_SIZE &&
board[row+distance][col+distance] == oldMarker) {
board[row+distance][col+distance] = newMarker;
}
if(row-distance > 0 &&
board[row-distance][col+distance] == oldMarker) {
board[row-distance][col+distance] = newMarker;
}
distance++;
markForward (row, col, distance, oldMarker,
newMarker);
}
}
Get Answers For Free
Most questions answered within 1 hours.