Question
Java Question!!!: Modify the following code, Hello, The following code will ask a user to input a line number of Pascal's triangle and the program
Java Question!!!: Modify the following code,
Hello,
The following code will ask a user to input a line number of Pascal's triangle and the program will print this line number only. The program is done recursively. However, an error produces an does not output anything! I've tried everything but I cannot seem to get this program to run correctly. Can someone please help me out and modify this code so it will work properly?
The purpose of the program: ask a user to see a line of Pascal's triangle, and then prints this line only. the program uses recursion.
If possible, can you also add a comment where you fixed it so I can follow it?
thank you so much!!!!!
import java.util.Scanner; public class PascalRecursive { private int lineNumber, count; private int[] num; public PascalRecursive() { lineNumber = 1; } public void set (int n) { if (n < 1) lineNumber = 1; else lineNumber = n; } public int get() { return lineNumber; } private void pascal (int[] row) { if (count >= lineNumber) return; num = new int[row.length+1]; num[0] = 1; int i; for(i = 1; i < row.length; i++) num[i] = row[i-1] + row[i]; num[row.length] = 1; count++; pascal(num); return; } public int[] output() { count = 1; num = new int[count]; num[0] = 1; pascal(num); return num; } public static void main (String args[]) { int i, num; Scanner scan = new Scanner(System.in); System.out.println("Enter line number:"); System.out.println(""); num = scan.nextInt(); PascalRecursive t = new PascalRecursive(num); int[] result = t.output(); System.out.println("The " +t.get() + "line is"); for(i = 0; i < result.length; i++) System.out.print(result[i] + " "); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started