
February 18th, 2004, 08:43 PM
#1
Pascal's Triangle with Recursion
Hey everyone, I am new here. I have a Computer Science 2 Assignment due in a few days dealing with printing out the Nth row of pascal's triangle using a recursive method. Here is what I have so far:
class PascalTriangle {
public static int[] Pascal(int num){
int[] ans = new int[num];
if(num==1){
ans[0]=1;
}
else {
int[] pre = Pascal(num1 + num);
}
return ans;
}
public static void main(String[] argv) throws IOException {
BufferedReader inBuf = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the row number: ");
StringTokenizer T = new StringTokenizer(inBuf.readLine());
while(T.countTokens()!=0){
int num = Integer.valueOf(T.nextToken()).intValue();
System.out.println("Row "+num+" of the Pascal Triangle is:");
int[] row = Pascal(num);
for(int i=0; i<num; i++)System.out.print(row[i]+"\t");
System.out.println();
System.out.print("Enter the row number: ");
T = new StringTokenizer(inBuf.readLine());
}
}
The problem is within my Pascal method, I can't quite figure out how to determine the rows using recursion. I have used recursion for a few assignments before this, so I understand the concepts. I was hoping someone would be willing to POINT ME into the right direction. Please don't give me an answer. By the way, my Professor said that it should take no more than 45 lines in the method.

February 18th, 2004, 09:52 PM
#2
Please reedit your post and reformat your code block using the code tags.
Tell us what you think Pascal Triangle is.

February 18th, 2004, 10:21 PM
#3
Originally posted by Joe Nellis
Tell us what you think Pascal Triangle is.
http://mathforum.org/workshops/usi/p.../fill.comb.gif
@UofSMark
Think about, how to get from the i1'th row to the i'th row.
In the recursive method start by putting the i1'th row in a temporary array, then construct the i'the row.
Last edited by khp; February 18th, 2004 at 10:26 PM.
The biggest problem encountered while trying to design a system that was completely foolproof,
was, that people tended to underestimate the ingenuity of complete fools.
Douglas Adams

February 19th, 2004, 08:28 AM
#4
Originally posted by khp
Think about, how to get from the i1'th row to the i'th row.
In the recursive method start by putting the i1'th row in a temporary array, then construct the i'the row.
This is what I was trying to do with my int[] pre array. The origanol line was: int[] Pre = Pascal(num1); But that got me no where. Atleast I know I was on the right track from your response, thanks.

February 19th, 2004, 09:46 AM
#5
In your 'else' branch, you need to get the n1'th row then calculate the current (n'th) row from it:
Code:
else {
// get the previous row
int[] previousRow = Pascal(num1);
// now calculate the current row from the previousRow
ans = getNextRow(previousRow);
}
return ans;
}
I'll leave the calculation of the next row to you...
God is in the details...
M. van der Rohe
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

February 19th, 2004, 10:18 AM
#6
Originally posted by dlorde
In your 'else' branch, you need to get the n1'th row then calculate the current (n'th) row from it:
Code:
else {
// get the previous row
int[] previousRow = Pascal(num1);
// now calculate the current row from the previousRow
ans = getNextRow(previousRow);
}
return ans;
}
I'll leave the calculation of the next row to you...
God is in the details...
M. van der Rohe
Thank you, if I have any more problems I will post.
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
This is a Codeguru.com survey!
