Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Recursive Pascal's Triangle

Posted on 2004-10-24
3
Medium Priority
?
4,446 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
Comment
Question by:edelossantos
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 5

Accepted Solution

by:
danielsonchris earned 500 total points
ID: 12396342
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
bcladd earned 500 total points
ID: 12396352
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
pankajtiwary earned 1000 total points
ID: 12396498
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

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

636 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