Starting in the top left corner of a 2 times 2 grid, and only being able to move
ID: 3792928 • Letter: S
Question
Starting in the top left corner of a 2 times 2 grid, and only being able to move to the right and down, there are exactly 6 routes to the bottom right corner. Compute the number of these types of routes through an n times n grid for 1 lessthanorequalto n lessthanorequalto 20. Allow the user to choose n Inputs the size of the grid (ie: n = 10 where the grid is n times n) Output: the number (of routes from the top left corner to the bottom right corner) Extra Credit: Repeat the question but where n may be any positive integer.Explanation / Answer
#include <iostream>
using namespace std;
int numberOfPaths(int m, int n)
{
if (m == 1 || n == 1)
return numberOfPaths(m-1, n) + numberOfPaths(m, n-1);
}
int main()
{
cout>> "Size of grid: ";
cin << n;
cout << numberOfPaths(n, n);
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.