Question

In: Computer Science

Modify the following code to count the number of recursive calls made for the Manhattan-path problem...


Modify the following code to count the number of recursive calls made for the Manhattan-path problem we studied earlier. Next, modify to include stored values and see how that reduces the number of calls made.

public class ManhattanWithCallCount {

    public static void main (String[] argv)
    {
        // Test case 1: go from (2,2) to (0,0)
        int r = 2;
        int c = 2;
        int n = countPaths (r, c);
        System.out.println ("r=" + r + " c=" + c + " => n=" + n);

        // Test case 2: go from (5,7) to (0,0)
        r = 5;
        c = 7;
        n = countPaths (r, c);
        System.out.println ("r=" + r + " c=" + c + " => n=" + n);
    }


    static int countPaths (int numRows, int numCols)
    {
        // Bottom-out case:
        if ( (numRows == 0) || (numCols == 0) ) {
            return 1;
        }

        // Otherwise, reduce to two sub-problems and add.
        int downCount = countPaths (numRows-1, numCols);
        int rightCount = countPaths (numRows, numCols-1);
        return (downCount + rightCount);
    }
}

Solutions

Expert Solution

Added static count variable to the program

Program Screenshot :

Program Code :

public class ManhattanWithCallCount {
   public static int count = 0;

public static void main (String[] argv)
{
// Test case 1: go from (2,2) to (0,0)
  
int r = 2;
int c = 2;
int n = countPaths (r, c);
System.out.println ("r=" + r + " c=" + c + " => n=" + n);

// Test case 2: go from (5,7) to (0,0)
r = 5;
c = 7;
n = countPaths (r, c);
System.out.println ("r=" + r + " c=" + c + " => n=" + n);
System.out.println("the number of recursive calls made for the Manhattan-path problem : " + count);
}


static int countPaths (int numRows, int numCols)
{
   count++;
// Bottom-out case:
if ( (numRows == 0) || (numCols == 0) ) {
return 1;
}

// Otherwise, reduce to two sub-problems and add.
int downCount = countPaths (numRows-1, numCols);
int rightCount = countPaths (numRows, numCols-1);
return (downCount + rightCount);
}
}

Program output:

Modified the stored values like below

Sample Output:


Related Solutions

9. Modify the quicksort and mergesort programs we learnt during the class to count the number...
9. Modify the quicksort and mergesort programs we learnt during the class to count the number of element comparisons for the two sorting methods. Use the following test drive to test them. public class Sorts {    int numComparisions = 0;    public void quicksort(int [] x, int l, int h)    { // your modifies codes go here    }    public void mergesort(int [] x, int l, int h)    { // your modifies codes go here   ...
Consider the following code segment:    count = 1    while count <= 10:       print(count,...
Consider the following code segment:    count = 1    while count <= 10:       print(count, end=" ") Which of the following describes the error in this code? The loop control variable is not properly initialized. The comparison points the wrong way. The loop is infinite. The loop is off by 1. Does this code contain an error? If so, what line is the error on? 0: ans = input("Yes/No? ") 1: if ans == "Yes": 2: print("Confirmed!") 3: else...
Modify the following source code so that five subsequent threads print the message “Hello World number,”...
Modify the following source code so that five subsequent threads print the message “Hello World number,” where number indicates the unique thread created; e.g. “Hello World 1” // Your_name_goes_here #include <pthread.h> #include <semaphore.h> sem_t semaphore; // also a global variable just like mutexes void *thread_function( void *arg ) { sem_wait( &semaphore ); // perform some task pthread_exit( NULL ); } int main() { int tmp; tmp = sem_init( &semaphore, 0, 0 ); // initialize pthread_create( &thread[i], NULL, thread_function, NULL );...
Python. Write a code that asks the user to enter a string. Count the number of...
Python. Write a code that asks the user to enter a string. Count the number of different vowels ( a, e, i, o, u) that are in the string and print out the total. You may need to write 5 different if statements, one for each vowel. Enter a string: mouse mouse has 3 different vowels
PYTHON PROBLEM: TASKED TO FIND THE FLAW WITH THE FOLLOWING CODE: from itertools import count def...
PYTHON PROBLEM: TASKED TO FIND THE FLAW WITH THE FOLLOWING CODE: from itertools import count def take_e(n, gen):     return [elem for (i, elem) in enumerate(gen) if i < n] def take_zc(n, gen):     return [elem for (i, elem) in zip(count(), gen) if i < n] FIBONACCI UNBOUNDED: def fibonacci_unbounded():     """     Unbounded Fibonacci numbers generator     """     (a, b) = (0, 1)     while True:         # return a         yield a         (a, b) = (b, a + b) SUPPOSED TO RUN THIS TO ASSIST WITH...
in java code Modify your program as follows: Ask the user for the number of hours...
in java code Modify your program as follows: Ask the user for the number of hours worked in a week and the number of dependents as input, and then print out the same information as in the initial payroll assignment. Perform input validation to make sure the numbers entered by the user are reasonable (non-negative, not unusually large, etc). Let the calculations repeat for several employees until the user wishes to quit the program. Remember: Use variables or named constants...
write code to count the number of odd integers in an array of 100 random integers...
write code to count the number of odd integers in an array of 100 random integers in the range [0,99].
As code is given below, follow the instructions: 1. Count the number of comparisons and find...
As code is given below, follow the instructions: 1. Count the number of comparisons and find where to put that counter in the code 2. Pick a random pivot, right pivot, left pivot, middle pivot for each smaller array /sub-array import java.util.*; import java.util.List; public class QuickSort { public static void main(String[] args) { int[] values = { 6, 5, 4, 3, 1, 7, 8 }; System.out.println("Original order: "); for (int element : values) System.out.print(element + " "); IntQuickSorter.quickSort(values); System.out.println("\nFirst...
The number of incorrectly transferred calls made per month is listed below for a sample of...
The number of incorrectly transferred calls made per month is listed below for a sample of 8 different months: 13 28 39 19 43 28 38 49 2.1 Calculate the mean number of incorrectly transferred calls made per month. Contextually interpret your answer. (3) 2.2 Calculate the median number of incorrectly transferred calls made per month. Contextually Interpret your answer. (3) 2.3 State the modal number of incorrectly transferred calls made per month. Contextually interpret your answer. (2) 2.4 Calculate...
Given the following code for AES Electronic Code Block implementation for the encryption functionality. Modify the...
Given the following code for AES Electronic Code Block implementation for the encryption functionality. Modify the code to create a function named ‘encryptECB(key, secret_message)’ that accepts key and secret_message and returns a ciphertext.          #Electronic Code Block AES algorithm, Encryption Implementation from base64 import b64encode from Crypto.Cipher import AES from Crypto.Util.Padding import pad from Crypto.Random import get_random_bytes secret_message = b" Please send me the fake passport..." password = input ("Enter password to encrypt your message: ") key= pad(password.encode(), 16) cipher...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT