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 it.(Hint: Use a 2D array to store the values on each line.)
1
11
121
1331
14641
15101051
1615201561
172135352171
18285670562881
To do this, you will create two classes:
PacalTriangleGenerator and the Driver
PascalTriangleGenerator class:
Instance variable: two integer arrays, row1 and row2
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 1.
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 2
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 2.
o The general case calls the computeNextRow method and passes into it the result of
calling the computerRow method, decreasing the row number by 1 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? 5
1
11
121
1331
14641
Another (y/n)? y
How many lines of Pascal's Triangle? 7
1
11
121
1331
14641
15101051
1615201561
Another (y/n)? n

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!