Question: Recursion Program Write a recursive program to determine and print the Nth line of Pascal's Triangle, as shown below. Each interior value is the sum
Recursion Program
Write a recursive program to determine and print the Nth line of Pascal's Triangle, as shown below. Each
interior value is the sum of the values above itHint: Use a D array to store the values on each line.
To do this, you will create two classes:
PacalTriangleGenerator and the Driver
PascalTriangleGenerator class:
Instance variable: two integer arrays, row and row
Methods:
Constructor that instantiates the two arrays The first array will contain one element and the
other will contain two elements. Set the value of each element to
computeRow This is your recursive method that computes the specified row of Pascals
triangle. Accepts an integer that represents a row number and returns an integer array
o There will be two base cases: value passed in is a one, which would only occur if the
original number is a one, and if value passed in is a
o Initially the specified row that is passed in is the bottom of the triangle. With each call of
this method recursively, you will work your way up the triangle until you are on row
o The general case calls the computeNextRow method and passes into it the result of
calling the computerRow method, decreasing the row number by each time
computeNextRow computes the next row of Pascals Triangle given the previous one. It
accepts an integer array that represents the previous row and returns an integer array
Driver class:
This class will only contain one method, in addition to the main method. It will prompt the user
for a total lines, call the method to create a pascal triangle and then prompt the user to indicate
whether a new triangle should be drawn.
Methods:
newTriangle This method does not receive or return anything. It draws the Pascal
Triangle.
o Prompt and accept the number of rows
o Inside a loop loop should traverse each row of the triangle
Instantiate a PascalsTriangleGenerator object
Call the computeRow method of PascalsTriangleGenerator
Output the triangle
Main
o Inside a loop loop should terminate when the user indicates they want to stop
Call the newTriangle
Prompt and accept user input for whether they want another triangle?
Sample output:
How many lines of Pascal's Triangle?
Another yn y
How many lines of Pascal's Triangle?
Another yn n
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
