CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: Pascal's Triangle with Recursion

1. Junior Member
Join Date
Feb 2004
Posts
3

## 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(num-1 + num);
}
return ans;
}

public static void main(String[] argv) throws IOException {
System.out.print("Enter the row number: ");
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: ");
}
}

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 4-5 lines in the method.

2. Member +
Join Date
Apr 2003
Location
Los Angeles area
Posts
776

Tell us what you think Pascal Triangle is.

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 i-1'th row to the i'th row.
In the recursive method start by putting the i-1'th row in a temporary array, then construct the i'the row.
Last edited by khp; February 18th, 2004 at 09:26 PM.

4. Junior Member
Join Date
Feb 2004
Posts
3
Originally posted by khp

Think about, how to get from the i-1'th row to the i'th row.
In the recursive method start by putting the i-1'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(num-1); But that got me no where. Atleast I know I was on the right track from your response, thanks.

5. Elite Member Power Poster
Join Date
Aug 1999
Location
UK
Posts
10,163
In your 'else' branch, you need to get the n-1'th row then calculate the current (n'th) row from it:
Code:
```   else {
// get the previous row
int[] previousRow = Pascal(num-1);

// 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

6. Junior Member
Join Date
Feb 2004
Posts
3
Originally posted by dlorde
In your 'else' branch, you need to get the n-1'th row then calculate the current (n'th) row from it:
Code:
```   else {
// get the previous row
int[] previousRow = Pascal(num-1);

// 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
•