Recursive Pascal's Triangle

/* 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 `++'
edelossantosAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

danielsonchrisCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bcladdCommented:
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
pankajtiwaryCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.