Question

T My question from C++ Plus Data Structures By Nell Dale, Chip Weems, Tim Richards book,page499...

T

My question from C++ Plus Data Structures By Nell Dale, Chip Weems, Tim Richards book,page499 question 16.

please if you have solution manual for this book,help me

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 followsshows three of many paths, if N = 10.(a) [1] 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;
else
return NumPaths(row + 1, col) * NumPaths(row, col + 1);
}
(b) [1] After you have corrected the function, trace the execution of NumPaths with n =
4 by hand.
(c) [1.5] You can improve the efficiency of this operation by keeping intermediate
values of NumPathsin 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 NumPathsthat uses this approach.
(d) [0.5] Show an invocation of the version of NumPaths you developed in part (c),
including any array initialization necessary.
(e) [1] How do the two versions of NumPaths compare in terms of time efficiency?
Space efficiency?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

copyable code

a)

program code debug the function

public class NumPathsval {

protected int calls1 = 0;

public int numPathsval(int rowval, int colval, int n1)

{

    calls1++;

    if (rowval == n1)

      return 1;

    else if (colval == n1)

      return 1;

    else

      return numPathsval(rowval+1, colval, n1) + numPathsval(rowval, colval+1, n1);

}

public static void main(String[] args) {

  

    int n1 = Integer.parseInt(args[0]);

    NumPathsval helperval = new NumPathsval();

    int count11 = helperval.numPathsval(1,1,n1);

    System.out.println(" " + count11 + " paths to (" + n1 + "," + n1 + ")");

    System.out.println(" That " + helperval.calls1 + " calls.");

}

}

a) program code debug the function public class NumPathsval f protected int calls10 public int numPathsval (int rowval, int cpublic static void main (String[] 큐rag) Integer.parseInt (args NumPathsval helperval new NumPathsval ) int count 11 helpervalath3Fromva1 for (int k 0; k < n1 ; ++k) new long [n11 [n1 for (int l= 0; 1 < n1; ++1) pathsEromvalk] [1] -1; if (pathsFromvalcopyable code

public class NumPathsval {

protected long calls1 = 0;

    protected long[][] pathsFromval;

public long numPathsval(int rowval, int colval, int n1)

{

    calls1++;

    // Base cases:

    if ((rowval == n1) || (colval == n1)) {

     return 1;

    }

    else {

     

      if (pathsFromval == null) {

        pathsFromval = new long[n1][n1];

        for (int k = 0; k < n1; ++k)

          for (int l = 0; l < n1; ++l)

            pathsFromval[k][l] = -1;

      }

      if (pathsFromval[rowval-1][colval-1] == -1) {

        pathsFromval[rowval-1][colval-1] =

          numPathsval(rowval+1,colval,n1) + numPathsval(rowval,colval+1,n1);

      }

      // Return

      return pathsFromval[rowval-1][colval-1];

    }

}

public static void main(String[] args) {

   

    int n1 = Integer.parseInt(args[0]);

    NumPathsval helperval = new NumPathsval();

    long count1 = helperval.numPathsval(1,1,n1);

    System.out.println(" " + count1 + " paths to (" + n1 + "," + n1 + ")");

    System.out.println(" That " + helperval.calls1 + " calls.");

}

}

d) Answe %2umPathSyal 4 20 paths to (4.4) That 1 lls. 3432 paths to (8.8) That 99 calls. 12870 paths to (9) That 129 calls. %Space Efficiency It has highly recursive version since it has stach but not O(n) deep Qin 2) space effficency clarity The arr

Add a comment
Know the answer?
Add Answer to:
T My question from C++ Plus Data Structures By Nell Dale, Chip Weems, Tim Richards book,page499...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java 1. Write a getCount method in the IntArrayWorker class that returns the count of the...

    Java 1. Write a getCount method in the IntArrayWorker class that returns the count of the number of times a passed integer value is found in the matrix. There is already a method to test this in IntArrayWorkerTester. Just uncomment the method testGetCount() and the call to it in the main method of IntArrayWorkerTester. 2. Write a getLargest method in the IntArrayWorker class that returns the largest value in the matrix. There is already a method to test this in...

  • ================Data Structures C++=============== – Implement the Eight Queens Problem This is a...

    ================Data Structures C++=============== – Implement the Eight Queens Problem This is an object oriented program. This is #1 on page 187 of your book with ONE difference, I’m requiring you add a client program to test your code (a main). If you have the old book the question says “Complete the Queen and Board class for the Eight Queens problem.” On page 179 of your book is a function placeQueens that solves the Eight Queens problem. On page 180 of...

  • Language is C++ Basic principles and theory of structured programming in C++ by implementing the class:...

    Language is C++ Basic principles and theory of structured programming in C++ by implementing the class: Matrix Use these principles and theory to help build and manipulate instances of the class (objects), call member functions Matrix class implementation from the main function file and, in addition, the Matrix class must have its interface(Matrix.h) in a separate file from its implementation (Matrix.cpp). The code in the following files: matrix.h matrix.cpp matrixDriver.h Please use these file names exactly. Summary Create three files...

  • #include <stdio.h> #include <stdlib.h> #include <string.h> struct game_piece { ...

    #include <stdio.h> #include <stdlib.h> #include <string.h> struct game_piece { }; struct game_board { }; void game_piece_init_default(struct game_piece* piece) { } void game_piece_init(struct game_piece* piece, char* new_label) { } char* game_piece_get_label(struct game_piece* piece) { return ""; } char* game_piece_to_string(struct game_piece* piece) { return ""; } void game_board_init(struct game_board* game_board, int rows, int cols) { } int game_board_is_space_valid(struct game_board* game_board, int row, int col) { return 0; } int game_board_add_piece(struct game_board* game_board, struct game_piece* piece, int row, int col) { return 0;...

  • PLEASE COMPLETE IN C++ LANGUAGE Location row : int = -1 col : int = -1...

    PLEASE COMPLETE IN C++ LANGUAGE Location row : int = -1 col : int = -1 setLocation(row : int, col : int) getRow(): int getColl): int isEmpty(): bool Details Write all the code necessary to implement the Location class as shown in the UML Class Diagram to the right. Do this in a project named snake (we'll continue adding files to this project as the term progresses). Your class definition and implementation should be in separate files. When complete, you...

  • Please solve only if you know how to do it. Write the code using C++ (not...

    Please solve only if you know how to do it. Write the code using C++ (not Python or Java). Show and explain everything neatly. COMMENTS (7.5% of programming assignment grade): Your program should have at least ten (10) different detailed comments explaining the different parts of your program. Each individual comment should be, at a minimum, a sentence explaining a particular part of your code. You should make each comment as detailed as necessary to fully explain your code. You...

  • This is my code for a TicTacToe game. However, I don't know how to write JUnit...

    This is my code for a TicTacToe game. However, I don't know how to write JUnit tests. Please help me with my JUnit Tests. I will post below what I have so far. package cs145TicTacToe; import java.util.Scanner; /** * A multiplayer Tic Tac Toe game */ public class TicTacToe {    private char currentPlayer;    private char[][] board;    private char winner;       /**    * Default constructor    * Initializes the board to be 3 by 3 and...

  • I need a basic program in C to modify my program with the following instructions: Create...

    I need a basic program in C to modify my program with the following instructions: Create a program in C that will: Add an option 4 to your menu for "Play Bingo" -read in a bingo call (e,g, B6, I17, G57, G65) -checks to see if the bingo call read in is valid (i.e., G65 is not valid) -marks all the boards that have the bingo call -checks to see if there is a winner, for our purposes winning means...

  • This is my code for my game called Reversi, I need to you to make the...

    This is my code for my game called Reversi, I need to you to make the Tester program that will run and complete the game. Below is my code, please add comments and Javadoc. Thank you. public class Cell { // Displays 'B' for the black disk player. public static final char BLACK = 'B'; // Displays 'W' for the white disk player. public static final char WHITE = 'W'; // Displays '*' for the possible moves available. public static...

  • Please help i need a C++ version of this code and keep getting java versions. Please...

    Please help i need a C++ version of this code and keep getting java versions. Please C++ only Purpose: This lab will give you experience harnessing an existing backtracking algorithm for the eight queens problem, and seeing its results displayed on your console window (that is, the location of standard output). Lab A mostly complete version of the eight queens problem has been provided for you to download. This version has the class Queens nearly completed. You are to provide...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT