Results 1 to 5 of 5
Thread: Pascal Triangle help
- 05-01-2008, 04:39 PM #1
Member
- Join Date
- May 2008
- Posts
- 3
- Rep Power
- 0
Pascal Triangle help
hey everyone i am new to Java that's why i need any help i can get.
i need to fill up the function Rows returns ragged array containing the first n rows of Pascal's triangle.
i tried playing around with it but considering i am totally new to this i have no idea if i am doing it right or how it should be implemented. I think i am doing way too much of what needs to be done.
can someone please take a look and based on what's given in the main function right now tell me what Rows should look like...
Java Code:public class Pascal { public static int[][] Rows(int n){ int[][] pt = new int[n][]; for (int i = 0; i < n; i++) { pt[i] = new int[i + 1]; // Construct row i. pt[i][0] = 1; // Leftmost value of row i. for (int j = 1; j < i; j++) { pt[i][j] = pt[i - 1][j - 1] + pt[i - 1][j]; // Sum 2 entries above. } pt[i][i] = 1; // Rightmost value of row i. } return pt; } public static void main(String[] args) { if (args.length != 1) { System.out.println("usage: java " + Pascal.class.getName() + " rows"); System.exit(1); } int n = Integer.parseInt(args[0]); if (n > 0) { int[][] pascal = Rows(n); for (int[] row : pascal) { for (int v : row) System.out.print(v + " "); System.out.println(""); } } } }
when i try running this of course nothing gets printed except the usage:java message.
thanks a lot :)
- 05-01-2008, 06:21 PM #2
I would use recursion to generate the Fibonacci numbers then just print them out.
My IP address is 127.0.0.1
- 05-01-2008, 06:46 PM #3
Member
- Join Date
- May 2008
- Posts
- 3
- Rep Power
- 0
- 05-01-2008, 07:41 PM #4
Heres a nice template for recursion
Try to do this with Fibonacci numbersJava Code:public <modifier> recusiveAlg(You need some sort of parameter) { if(some sort of stopping criteria) { return something; } else { return recursiveAlg(your parameter - 1) } }
hint: Fibonacci's numbers are the previous fib number + the one before the pervious fib number.My IP address is 127.0.0.1
- 05-01-2008, 07:51 PM #5
Member
- Join Date
- May 2008
- Posts
- 3
- Rep Power
- 0
hmmm correct me if i'm wrong but isn't it already implemented in the main function?
all Rows has to do is to generate the array somehow to decide the row number...Java Code:if (n > 0) { int[][] pascal = Rows(n); for (int[] row : pascal) { for (int v : row) System.out.print(v + " "); System.out.println("");Last edited by Magic101; 05-01-2008 at 07:58 PM.
Similar Threads
-
change the square to triangle java
By anotsu in forum New To JavaReplies: 3Last Post: 07-09-2009, 11:17 AM -
Triangle
By jkswebsite in forum New To JavaReplies: 8Last Post: 01-10-2009, 02:08 PM -
Computing the area of a triangle using Heron's Formula
By Java Tip in forum java.langReplies: 0Last Post: 04-12-2008, 08:39 PM -
Making triangle
By banie in forum New To JavaReplies: 4Last Post: 02-02-2008, 11:23 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks