Pascal's Triangle with Recursion
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Pascal's Triangle with Recursion

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

  2. #2
    Join Date
    Apr 2003
    Location
    Los Angeles area
    Posts
    776
    Please re-edit your post and reformat your code block using the code tags.

    Tell us what you think Pascal Triangle is.

  3. #3
    Join Date
    May 2003
    Location
    Denmark
    Posts
    1,315
    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 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

  4. #4
    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. #5
    dlorde is offline 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
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  6. #6
    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center