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

Modify the following C++ program to count the number of correct and incorrect responses typed by...
Modify the following C++ program to count the number of correct and incorrect responses typed by the student. After the student types 5 answers, your program should calculate the percentage of correct responses. If the percentage is lower than 75 percent, your program should print “Please ask for extra help” and then terminate. If the percentage is 75 percent or higher, your program should print “Good work!” and then terminate. The program is as follows: #include<iostream> #include<iomanip> #include<cstdlib> #include<time.h> using...
Write a recursive a c++ code that checks if a number is Palindrome. A palindrome number...
Write a recursive a c++ code that checks if a number is Palindrome. A palindrome number is a number that reads the same from beginning to end and from end to beginning, in other words, a palindrome number remains the same when its digits are reversed. For example, 13431 is a palindrome number. 2332 is another one. (Note: Your algorithm should define and work with an integer number) The functionality of your code should be commented to explain what you...
Write a recursive a Java code that checks if a number is Palindrome. A palindrome number...
Write a recursive a Java code that checks if a number is Palindrome. A palindrome number is a number that reads the same from beginning to end and from end to beginning, in other words, a palindrome number remains the same when its digits are reversed. For example, 13431 is a palindrome number. 2332 is another one. (Note: Your algorithm should define and work with an integer number) The functionality of your code should be commented to explain what you...
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...
I need to,  Modify my mapper to count the number of occurrences of each character (including punctuation...
I need to,  Modify my mapper to count the number of occurrences of each character (including punctuation marks) in the file. Code below: #!/usr/bin/env python #the above just indicates to use python to intepret this file #This mapper code will input a line of text and output <word, 1> # import sys sys.path.append('.') for line in sys.stdin: line = line.strip() #trim spaces from beginning and end keys = line.split() #split line by space for key in keys: value = 1 print...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT