Results 1 to 8 of 8
Thread: a question about pascal triangle
 10292010, 04:12 PM #1Member
 Join Date
 Oct 2010
 Posts
 10
 Rep Power
 0
 10292010, 04:22 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,651
 Blog Entries
 7
 Rep Power
 21
You can generate the next row given the current row; let the number of numbers in the current row be n and let 'r' be an array representing the current row. From right to left do this:
Java Code:r[n]= 1; for (int i= n; i > 0; r[i]+= r[i1]);
kind regards,
Jos
 10292010, 04:35 PM #3Member
 Join Date
 Oct 2010
 Posts
 10
 Rep Power
 0
thanks jos but how can i do without using array?
 10292010, 04:55 PM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,651
 Blog Entries
 7
 Rep Power
 21
Why don't you want to use an array? A single array of the size equal to the height of the triangle is enough and the computations are extremely cheap. The alternative if that use calculate C(n, k) for each element in row n, element k but that would be much more expensive and involves quite a bit of cleverness to circumvent the obvious limit of 13 (13! is the max number you can store in an int).
kind regards,
Jos
 10302010, 12:42 AM #5Member
 Join Date
 Oct 2010
 Posts
 10
 Rep Power
 0
i want to do it by using loops because i do not have enough information about arrays and also the question asks me solve the problem by using loops.. You said: The alternative if that use calculate C(n, k) for each element in row n, element k. Could you clarify this?
 10302010, 12:59 AM #6Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,565
 Rep Power
 12
See for example the Wikipedia article on Combination.
 10302010, 06:31 PM #7Member
 Join Date
 Oct 2010
 Posts
 10
 Rep Power
 0
Java Code:import java.util.Scanner; public class Lab05c{ public static void main(String args[]){ int m, n, f, mf, nf, nmf, k, result, i; int maxRow=6; for( n =0;n<=maxRow;n++){ k=n; result=1; for(i=2;i<=k;i++) result=result*i nf=result; k=m=0; result=1; for(i=2;i<=k;i++) result=result*i; mf=result; k=nm; result=1; for(i=2;i<=k;i++) result=result*i; nmf=result; f= nf/(mf*nmf); for( m=0;m<=n;m++) System.out.print(f); System.out.println(); } } }
i did like this and the output of this:
1
11
111
1111
11111
111111
what do i need to have right result? can you help me
Moderator Edit: code tags addedLast edited by Fubarable; 10302010 at 06:56 PM. Reason: Moderator Edit: code tags added

Sorry, but it's kind of hard to follow your code. Myself, if this were my project, I'd subdivide things by creating a small method that calculated the factorial of a number, n! = n * (n 1) * (n  2)...., say
Java Code:public static int factorial(int n) { if (n == 0) { return 1; } //... more code goes here. :) }
And then again, using pbrockway's reference, create a very short method that calculates the combination:
Java Code:public static int combination(int n, int k) { // only one line of code here that uses the factorial method above a few times }
And then call the combination method inside of a nested for loop, and you're done.
Also, when posting code here, please use code tags so that your code will retain its formatting and thus will be readable  after all, your goal is to get as many people to read your post and understand your code as possible, right?
To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.
Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:
Java Code:[code] // your code goes here // notice how the top and bottom tags are different [/code]
Similar Threads

pascal to java converter
By j2me64 in forum Forum LobbyReplies: 1Last Post: 04172010, 09:56 PM 
Pascal Triangle noob help
By jigglywiggly in forum New To JavaReplies: 15Last Post: 02172009, 12:05 AM 
Translating Pascal into java
By willemjav in forum Advanced JavaReplies: 120Last Post: 08152008, 11:26 PM 
PASCAL & JAVA? for senior members
By willemjav in forum Forum LobbyReplies: 2Last Post: 07132008, 11:11 AM 
Pascal Triangle help
By Magic101 in forum New To JavaReplies: 4Last Post: 05012008, 07:51 PM
Bookmarks