Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-01-2008, 05:39 PM
Member
 
Join Date: May 2008
Posts: 3
Magic101 is on a distinguished road
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...

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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-01-2008, 07:21 PM
Zosden's Avatar
Senior Member
 
Join Date: Apr 2008
Posts: 353
Zosden is on a distinguished road
I would use recursion to generate the Fibonacci numbers then just print them out.
__________________
My IP address is 127.0.0.1
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-01-2008, 07:46 PM
Member
 
Join Date: May 2008
Posts: 3
Magic101 is on a distinguished road
Quote:
Originally Posted by Zosden View Post
I would use recursion to generate the Fibonacci numbers then just print them out.
thanks for response... but would you mind actually showing me how it should be done.. i am clueless oh that part
Main function stays as it is other functions is the one that needs modification...

thanks.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 05-01-2008, 08:41 PM
Zosden's Avatar
Senior Member
 
Join Date: Apr 2008
Posts: 353
Zosden is on a distinguished road
Heres a nice template for recursion

Code:
public <modifier> recusiveAlg(You need some sort of parameter) { if(some sort of stopping criteria) { return something; } else { return recursiveAlg(your parameter - 1) } }
Try to do this with Fibonacci numbers

hint: Fibonacci's numbers are the previous fib number + the one before the pervious fib number.
__________________
My IP address is 127.0.0.1
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 05-01-2008, 08:51 PM
Member
 
Join Date: May 2008
Posts: 3
Magic101 is on a distinguished road
Quote:
Originally Posted by Zosden View Post
Heres a nice template for recursion

Code:
public <modifier> recusiveAlg(You need some sort of parameter) { if(some sort of stopping criteria) { return something; } else { return recursiveAlg(your parameter - 1) } }
Try to do this with Fibonacci numbers

hint: Fibonacci's numbers are the previous fib number + the one before the pervious fib number.
hmmm correct me if i'm wrong but isn't it already implemented in the main function?

Code:
if (n > 0) { int[][] pascal = Rows(n); for (int[] row : pascal) { for (int v : row) System.out.print(v + " "); System.out.println("");
all Rows has to do is to generate the array somehow to decide the row number...

Last edited by Magic101 : 05-01-2008 at 08:58 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Computing the area of a triangle using Heron's Formula Java Tip java.lang 0 04-12-2008 09:39 PM
change the square to triangle java anotsu New To Java 2 02-06-2008 08:21 PM
Making triangle banie New To Java 4 02-02-2008 12:23 PM
Triangle jkswebsite New To Java 6 01-14-2008 04:33 AM


All times are GMT +3. The time now is 11:47 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org