Question
Not a problem from the book, but it uses multidimensional arrays. Pascals triangle is a chart of numbers where each row is created from the
Not a problem from the book, but it uses multidimensional arrays. Pascals triangle is a chart of numbers where each row is created from the values on the row above it. Each row begins and ends with 1, and additional numbers are the sum of the two numbers above it.
Example of first five rows: 1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Your job is to create the first few rows of Pascals triangle in a multidimensional array and output it to the screen. You can line the columns up to the left to make it easier to store and/or output. I want you to create each row from the row above it, and allow the user to tell you how many rows to create.
Note that each row will have a different length. The first row should only be able to hold 1 value, which is 1. The second row should only be able to hold 2 values, which should be 1 and 1. Etc. If the user types in 10, do not just create a 10 x 10 array and then leave spots blank.
This program displays Pascals triangle. How many rows would you like to see?
This program is working fine.
I have a question while creating the array.
please read the following line at multi -dimensional array creating
import java.util.*;
class Pascal {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[][] arr = new int[n+1][n+1]; (-- Overhere she want me to leave second [n+1] blank and do it some other way)
for(int i = 0; i <= n; ++i) {
for(int j = 0; j <= i; ++j) {
if(j == 0 || j == i) {
arr[i][j] = 1;
} else {
arr[i][j] = arr[i-1][j-1] + arr[i-1][j];
}
}
}
for(int i = 0; i <= n; ++i) {
for(int j = 0; j <= i; ++j) {
System.out.print(arr[i][j] + "\t");
}
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