Question

How do I convert this while loop to tail recursion? private void markForward (int row, int...

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++;

Homework Answers

Answer #1

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);
   }
   }

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
c++ Convert the following for loop to a while loop: for (int x = 50; x...
c++ Convert the following for loop to a while loop: for (int x = 50; x > 0; x-=2) {      cout << x << " seconds to go.\n"; }
In C can you change this code to use Tail Call Recursion int min (int n,...
In C can you change this code to use Tail Call Recursion int min (int n, int[] input) { int i; int result; for (result = input[0], i = 0; i < n; i += 1) { if (result > input[i]) result = input[i]; } return result; }
What is wrong with the following function: public static void funMethod() { int i=100; while (i...
What is wrong with the following function: public static void funMethod() { int i=100; while (i < 10) { System.out.println("i="+i); i++; } } Select one: a. loop control variable not declared b. loop control variable is never updated c. loop control variable not initialized d. loop control variable never resolves to true e. nothing is wrong with this code. What is wrong with the following function: public static void funMethod() { int i=1; while (i < 10) { System.out.println("i="+i); i++;...
convert the while loop into for loop x = int(input('Enter initial value: ')) count = 0...
convert the while loop into for loop x = int(input('Enter initial value: ')) count = 0 if(x<1): print('Error') exit() print('Initial value is: ',x,' ',end='') while(x!=1): if(x%2==0): x=x/2 else: x=3*x+1 count+=1 if(x!=1): rint('Next value is: ',int(x),' ',end='') else: print('Final value ',int(x),', ',end='') print('number of operations performed ',int(count),' ',end='') break
C++ #include<iostream> #include<string> #include<fstream> #include<cstdlib> using namespace std; const int ROWS = 8; //for rows in...
C++ #include<iostream> #include<string> #include<fstream> #include<cstdlib> using namespace std; const int ROWS = 8; //for rows in airplane const int COLS = 4; void menu(); //displays options void displaySeats(char[][COLS]); void reserveSeat(char [ROWS][COLS]); int main() { int number=0; //holder variable char seatChar[ROWS][COLS]; for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { seatChar[i][j] = '-'; } } int choice; //input from menu bool repeat = true; //needed for switch loop while (repeat...
What is wrong with the following function: public static void funMethod() { int i=1; while (i...
What is wrong with the following function: public static void funMethod() { int i=1; while (i < 10) { System.out.println("i="+i); i++; } } Select one: a. nothing is wrong with this code. b. loop control variable not initialized c. loop control variable is never updated d. loop control variable not declared e. loop control variable never resolves to true
Convert the factorial program to do-while loop and for loop programs
Convert the factorial program to do-while loop and for loop programs
Do While loop on C++ I need to program a do while loop in which the...
Do While loop on C++ I need to program a do while loop in which the user have to choose between option A,a, B,b or C,c. I need a do while loop that if other letter (different from options is choose) the loop print a cout saying "Invalid selection. Plese, enter the selection again.   I have one, but isn't working well int main() { char option; do { cout<<"Please choose one of the following options: "<<endl<<endl; cout<<"Option A - Julian...
True or False? a. int i = 0; while(i <= 20); { i = i+5; cout...
True or False? a. int i = 0; while(i <= 20); { i = i+5; cout << i << " "; } The above loop is an infinite loop. for(int i =5; i>=1 ; i++) { cout << i << endl; } The above loop is an infinite loop.
How do I implement this method BalancedByNodeCount() ? public class BinarySearchTree { private Node root; private...
How do I implement this method BalancedByNodeCount() ? public class BinarySearchTree { private Node root; private boolean isBalancedByNodeCount() { /**************************************************************************** Implement this method and replace the return statement below with your code. * Definition of Balance tree (On page 372 of book): * An unbalanced tree is created when most of the nodes are on one side of the root or the other. ****************************************************************************/    return false; }       public static void main(String args[]) { int[] values1 = {50,...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT