# Thread: Pascal Triangle noob help

1. Senior Member
Join Date
Nov 2008
Posts
105
Rep Power
0

## Pascal Triangle noob help

Alright, I have been working on this for so long and am now lost in this assignment. I've been working on it for a while so can you guys please help me with the loop to complete this?

Here is the main
Java Code:
```import java.util.Scanner;

public class PascalDriver{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("Enter number of rows");
int rowCount = scan.nextInt();
PascalTri t = new PascalTri(rowCount);
t.displayRows();
}
}```

Here is the class, I get totally confused on displayrows

Java Code:
```public class PascalTri {
int rowCount;
int cnt = 0;
public PascalTri(int n) {
rowCount = n;

}

public void displayRows() {
int whatever=0;
int bbb;
int previous[] = new int[1000];
for(int j = 0; j < 1000; j++)
previous[j] = -1;

previous[0]=1;
int current[] = new int[previous.length];
for(int j = 0; j < 1000; j++)
current[j] = -1;
System.out.println("----------------------");
if(isdone()==false) {

System.out.println(previous[0]);
cnt++;
}
if(isdone()== false) {
System.out.println(previous[0]+"1");
cnt++;
}
current[0] = 1;
current[1] = 1;
while(isdone()==false) {
System.out.print("1");
current[2] = current[1]+current[0];
System.out.print(current[2] + "1");
for(int ldm = 0; ldm<current.length; ldm++) {
if(current[ldm]>-1) {
int j = current[ldm];
int forprevious = j-1;
whatever=j;
for(bbb=1; bbb<j; bbb++) {
current[0]=current[0] + 1;
current[bbb]=current[bbb]+current[forprevious];
forprevious++;

}

}

}
//   System.out.print(current[whatever]+1);
//       System.out.print("1");
System.out.println("");
cnt++;
}
}

public boolean isdone() {

boolean d = false;
if(cnt==rowCount) {
d =true;
} return d;
}

}```
Here is what it happens when I run it

Enter number of rows
6
----------------------
1
11
121
141
1141
1311

2. i'm really surprised no one has taken a crack at this yet. I had alot of fun messing w/ the pascal triangles.

don't really understand your math in "displayRows" method. this is how i've written mine:

I used a ragged array.
set array[0][0] = 1;
set array[*][first] = 1;
set array[*][last] = 1;
then just calculate for middle values based on the previous line's values.

such as:
n = current line (starts from 1)
array[n][index] = array[n-1][index] + array[n-1][index-1]

and so on....
how for me to explain, you getting it?

3. Senior Member
Join Date
Nov 2008
Posts
105
Rep Power
0
Thanks for the response but I am still heavily confused I changed it to this
Java Code:
```public void displayRows() {
int whatever=0;
int bbb;
int previous[] = new int[1000];
for(int j = 0; j < 1000; j++)
previous[j] = -1;
int counter=0;
int drone;

previous[0]=1;
int current[] = new int[previous.length+1];
for(int j = 0; j < 1000; j++)
current[j] = -1;

current[0]= 1;
current[1]=1;
System.out.println("----------------------");
if(isdone()==false) {
System.out.println(current[0]);
cnt++;
}
if(isdone()==false) {
System.out.print(current[1]);
System.out.print(current[1]);
cnt++;
}

while(isdone()==false) {
previous=current;
for(int ldm = 0; ldm<current.length; ldm++) {
if(current[ldm]>-1) {
int j =ldm;
counter=j;

}

current = new int[current.length+1];
current[0]=1;
for(int i=1; i<current.length; i++) {
current[i]=previous[i]+previous[i-1];
System.out.print(current[i]);
}

cnt++;
}```
But when I run it I get this

Java Code:
```run:
Enter number of rows
3
----------------------
1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1001
at PascalTri.displayRows(PascalTri.java:50)
at PascalDriver.main(PascalDriver.java:9)
1120-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2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-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-2-1Java Result: 1
BUILD SUCCESSFUL (total time: 9 seconds)```
It points to here current[i]=previous[i]+previous[i-1];

4. Senior Member
Join Date
Dec 2008
Location
Hong Kong
Posts
473
Rep Power
9
previous has 1000 elements
current has 1002 elements

why you redeclare current in while loop and not do the same thing for previous ?

5. I don't think you are using the correct math to do this. maybe you should scrap the whole function and start over with a different appoach.

what you should know:
first line is a 1,
second line is 1 1,
third line is 1 X 1,
where x = previousLine[n-1] + previousLine[n];
and so on...

mtyoung, welcome back. I've out posted you. hehe

6. Senior Member
Join Date
Nov 2008
Posts
105
Rep Power
0
But I thought my math function does the same thing? I am brain stumped:(

7. i can't tell if your math is right or wrong, but its a mess.
the first: if(isdone()==false) prints a 1.
the second: if(isdone()==false) prints two 1's.
i'll let you pass on that.

Java Code:
```current = new int[current.length+1];
current[0]=1;
for(int i=1; i<current.length; i++) {
current[i]=previous[i]+previous[i-1];
System.out.print(current[i]);
}```
now thats just crazy. i<current.length.
BUT current.length is previous.length + 1
which is 1001. then you used print on it. which prints out
1001 int values!!!. that explains why you are getting -2-2-2-2-2-2-2-2-2-2-... and so on.

also, before going into the for looop, you are changing the current array. why?? try to find a different idea to calculate for the middle values.

8. Senior Member
Join Date
Nov 2008
Posts
105
Rep Power
0
So I changed it up a bit, man I get confused in my own code so much, I guess I am learning that lesson the hard way:(

Java Code:
```  while(isdone()==false) {
previous=current;
for(int ldm = 0; ldm<current.length; ldm++) {
if(current[ldm]>-1) {
int j =ldm;
counter=j;

}

// current = new int[current.length+1];
current[0]=1;
for(int i=0; i<ldm; i++) {
current[i]=previous[i]+previous[i-1];
System.out.print(current[i]);
previous=current;
}```
When I run it it gets out of bounds
at the line current[i]=previous[i]+previous[i-1];

but if I just change that to previous[i] the program does nothing? Just eats up cpu:confused:

9. 1) what is ldm??
2) you have previous=current; two times, one at top, second at bottom.
3) trying moving current = new int[current.length+1]; to bottom.

10. Senior Member
Join Date
Nov 2008
Posts
105
Rep Power
0
LDM is the last known position in the array that has info in it. Project is due in 50 minutes, hopefully I can figure it out, oh well you live you learn :(

11. Senior Member
Join Date
Nov 2008
Posts
105
Rep Power
0
Haha right after class ended it came to me I got it! I'll post the code later thank you so much

12. Senior Member
Join Date
Nov 2008
Posts
105
Rep Power
0
For anyone wanting the code for w/e reason
Java Code:
```public class PascalTri {
int rowCount;
int cnt = 0;
public PascalTri(int n) {
rowCount = n;

}

public void displayRows() {
int whatever=0;
int bbb;
int previous[] = new int[1000];
for(int j = 0; j < 1000; j++)
previous[j] = -1;
int counter=0;
int drone;

previous[0]=1;
int current[] = new int[previous.length+1];
for(int j = 0; j <= 1000; j++)
current[j] = -1;

current[0]= 1;
current[1]=1;
System.out.println("----------------------");
if(isdone()==false) {
System.out.println(current[0]);
cnt++;
//current[0]=1;
}
if(isdone()==false) {
System.out.print(current[1]);
System.out.println(current[1]);
cnt++;
//current[0]=1;
//current[1]=1;
}

while(isdone()==false) {
previous=current;
for(int ldm = 0; ldm<current.length; ldm++) {
if(current[ldm]>-1) {
counter=ldm;
}
}
current = new int[current.length];
for(int j = 0; j <= 1000; j++)
current[j] = -1;
current[0] = 1;
System.out.print(current[0]);

int g = 1;

for(int i=1; i<=counter; i++) { //It does not go through this for loop! I don't know why EDIT: added <= and one gets printed
current[i]=previous[i]+previous[i-1];
System.out.print(current[i]);
//System.out.println("hello"); // just to test if it's working, which it is not'
}
current[counter+1]=1;
System.out.println(1);

/*  while(g<counter-1) { //same loop same problem, probably with counter
current[g]=previous[g]+previous[g-1];
System.out.print(current[g]);
System.out.println("hello");
g++;
}
*/
cnt++;
}

/*
if(isdone()==false) {

System.out.println(previous[0]);
cnt++;
}
if(isdone()== false) {
System.out.println(previous[0]+"1");
cnt++;
}
current[0] = 1;
current[1] = 1;
while(isdone()==false) {
System.out.print("1");
current[2] = current[1]+current[0];
System.out.print(current[2] + "1");
for(int ldm = 0; ldm<current.length; ldm++) {
if(current[ldm]>-1) {
int j = current[ldm];
int forprevious = j-1;
whatever=j;
for(bbb=1; bbb<j; bbb++) {
current[0]=current[0] + 1;
current[bbb]=current[bbb]+current[forprevious];
forprevious++;

}

}

}
//   System.out.print(current[whatever]+1);
//       System.out.print("1");
System.out.println("");
cnt++;
}
*/
}

public boolean isdone() {

boolean d = false;
if(cnt>=rowCount) {
d =true;
} return d;
}

}

Driver ---
import java.util.Scanner;
public class PascalDriver{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("Enter number of rows");
int rowCount = scan.nextInt();
PascalTri t = new PascalTri(rowCount);
t.displayRows();
}
}```

13. dude, i can't believe you did it! I still can't understand your math though...

14. ## my take

I've decided not to post the complete code, but here's the math portion i've used.

Java Code:
```for(int line=1; line < max; line++){
// create individual array
matrix[line]= new BigInteger[line+1];

// set first
matrix[line][0] = BigInteger.valueOf(1);

// set mid
for(int i=1; i<line; i++)

// set last
matrix[line][matrix[line].length -1 ] = BigInteger.valueOf(1);
}```
where matrix is a ragged array of type BigInteger.
if type long is used, it starts to overflow after 67th row.

XML Code:
```S:\SVN\PascalTriangle>java PascalTriangle
Enter number of rows (zero-based): 16
----------------------
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
1 10 45 120 210 252 210 120 45 10 1
1 11 55 165 330 462 462 330 165 55 11 1
1 12 66 220 495 792 924 792 495 220 66 12 1
1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1
1 14 91 364 1001 2002 3003 3432 3003 2002 1001 364 91 14 1
1 15 105 455 1365 3003 5005 6435 6435 5005 3003 1365 455 105 15 1
1 16 120 560 1820 4368 8008 11440 12870 11440 8008 4368 1820 560 120 16 1```
Last edited by angryboy; 01-28-2009 at 03:34 PM.

15. Thought I update this because I've just found a really neat math equation for this:
Java Code:
```    nRow[0] = 1;
for(int col=1; col<nRow.length; col++){
nRow[col] = nRow[col-1] * (ROW-col)/col;
}```

16. What is ROW?
EDIT: Oh figured it out its the row's length. Thanks for this formula I made my own pascal triangle generator with it :).
Last edited by MK12; 02-17-2009 at 01:10 AM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•