Solved

Recursive Pascal's Triangle

Posted on 2004-10-24
4,391 Views
Last Modified: 2012-08-14
/* non-recursive Pascal's Triangle      
   programmer: Enrique De Los Santos*/

#include <iostream>
#include <stdlib.h>
#include <stdio.h>

using namespace std;

#define MAX 12
typedef int PascalArray[MAX][MAX];
PascalArray p;

int main() {

  int c(int n, int k) {
    if(!k||(n==k)
      return 1;
    else
      return(c(n-1,k)+c(n-1,k-1);
       
   }

  int col, row;

    for(row = 0; row < MAX; row++)
      for(col = 0; col <= row; col++) {
      if(col == 0)
        p[row][col] = 1;
      else
        if(row == col)
          p[row][col] = 1;
        else
          p[row][col]=p[row-1][col]+p[row-1][col-1];

      }

  // triangle display
  for(row = 0; row < MAX; row++) {
    for(col = 0; col <= row; col++)
      cout << "%5d" << p[row][col] << endl;

  }

}

out:

[edeloss2@pegasus part3]$ g++ recPasc.cpp
recPasc.cpp: In function `int main()':
recPasc.cpp:16: parse error before `{'
recPasc.cpp: At top level:
recPasc.cpp:26: parse error before `for'
recPasc.cpp:26: parse error before `;'
recPasc.cpp:26: syntax error before `++'
recPasc.cpp:27: syntax error before `<='
recPasc.cpp:27: syntax error before `++'
recPasc.cpp:39: parse error before `;'
recPasc.cpp:39: syntax error before `++'
recPasc.cpp:40: syntax error before `<='
recPasc.cpp:40: syntax error before `++'
0
Question by:edelossantos
    3 Comments
     
    LVL 5

    Accepted Solution

    by:
    Did you mean to add the
    "int c(int n, int k) {" within the main()?
    If you move that out, then you'll get a different error.  Have a look at your if you condition within the strangely named "c" function

     if(!k||(n==k)
          return 1;
        else
          return(c(n-1,k)+c(n-1,k-1);


    should be

    if(!k||(n==k))
          return 1;
        else
          return(c(n-1,k)+c(n-1,k-1));


    hope this helps.
    0
     
    LVL 11

    Assisted Solution

    by:bcladd
    You cannot declare a function inside of another function (in C++). Thus the line declaring the function c (I would urge you to use a better name for the function) cannot appear inside of main:

    int main() {

      int c(int n, int k) {
        if(!k||(n==k)
          return 1;
        else
          return(c(n-1,k)+c(n-1,k-1);
           
       }

      int col, row;


    -bcl
    0
     
    LVL 4

    Assisted Solution

    by:pankajtiwary
    Hi edelossantos,

    Take the function definition outside main(). just like this:

    /* non-recursive Pascal's Triangle    
       programmer: Enrique De Los Santos*/

    #include <iostream>
    #include <stdlib.h>
    #include <stdio.h>

    using namespace std;

    #define MAX 12
    typedef int PascalArray[MAX][MAX];
    PascalArray p;

    int c(int n, int k) {
      if(!k||(n==k)
        return 1;
      else
        return(c(n-1,k)+c(n-1,k-1);  
    }

    int main() {
      int col, row;
      for(row = 0; row < MAX; row++)
        for(col = 0; col <= row; col++) {
          if(col == 0)
            p[row][col] = 1;
          else
            if(row == col)
              p[row][col] = 1;
            else
              p[row][col]=p[row-1][col]+p[row-1][col-1];
        }

        // triangle display
        for(row = 0; row < MAX; row++) {
          for(col = 0; col <= row; col++)
            cout << "%5d" << p[row][col] << endl;

        }

    }


    Cheers!
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: From Zero to Hero with Nodejs & MongoDB

    Interested in Node.js, but don't know where to start or how to learn it properly? Confused about how the MEAN stack pieces of MongoDB, Expressjs, Angularjs, and Nodejs fit together? Or how it's even possible to run JavaScript outside of the browser?

    Some Windows API functions expect you to provide a pointer to a CALLBACK function that the system will need to call as part of the operation.  Such API functions as SetTimer, timeSetEvent, CreateThread, EnumWindows, LineDDA, even window message hand…
      Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

    884 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now