We want to count the number of possible paths to move from row 1, column 1 to ro
ID: 3775570 • Letter: W
Question
We want to count the number of possible paths to move from row 1, column 1 to row N. column N in a two-dimensional grid. Steps are restricted to going up or to the right, but not diagonally. The illustration follows shows three of many paths, if N = 10. The following function. NumPaths, is supposed to count the number of paths, but it has some problems. Debug the function. int NumPaths(int row, int col, int n) (if(row == n) return 1; else if(col == n) return NumPaths + 1; eIse return NunPaths(row + 1, col) * NumPaths(row, col + 1);} After you have corrected the function, trace the execution of NumPaths with n = 4 by hand. You can improve the efficiency of this operation by keeping intermediate values of NumPaths in a two-dimensional array of integer values. This approach keeps the function from having to recalculate values that it has already figured out. Design and code a version of NumPaths that uses this approach. Show an invocation of the version of NumPaths you developed in pari (c). including any array initialization necessary. How do the two versions of NumPaths compare in terms of time efficiency? Space efficiency?Explanation / Answer
Part A
int NumPaths(int row,int col,int n)
{
if(row == n)
{
return 1;
}
else if(col == n)
{
return 1;
}
else
{
return NumPaths(row+1,col,n) + NumPaths(row,col+1,n);
}
}
Part B
Following function calls will be generated to calculate the value of NumPaths when n = 4
NumPaths(2,1,4) + NumPaths(1,2,4)
NumPaths(3,1,4) + NumPaths(2,2,4)
NumPaths(4,1,4) + NumPaths(3,2,4)
NumPaths(4,2,4) + NumPaths(3,3,4)
NumPaths(4,3,4) + NumPaths(3,4,4)
NumPaths(3,2,4) + NumPaths(2,3,4)
NumPaths(4,2,4) + NumPaths(3,3,4)
NumPaths(4,3,4) + NumPaths(3,4,4)
NumPaths(3,3,4) + NumPaths(2,4,4)
NumPaths(4,3,4) + NumPaths(3,4,4)
NumPaths(2,2,4) + NumPaths(1,3,4)
NumPaths(3,2,4) + NumPaths(2,3,4)
NumPaths(4,2,4) + NumPaths(3,3,4)
NumPaths(4,3,4) + NumPaths(3,4,4)
NumPaths(3,3,4) + NumPaths(2,4,4)
NumPaths(4,3,4) + NumPaths(3,4,4)
NumPaths(2,3,4) + NumPaths(1,4,4)
NumPaths(3,3,4) + NumPaths(2,4,4)
NumPaths(4,3,4) + NumPaths(3,4,4)
Final value would be 20.
Part C
Will post the others answers shortly.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.