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?
 
danielsonchrisConnect With a Mentor Commented:
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
 
bcladdConnect With a Mentor Commented:
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
 
pankajtiwaryConnect With a Mentor Commented:
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
All Courses

From novice to tech pro — start learning today.