[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1336
  • Last Modified:

Non-Recursive Pascal's Triangle

// Does not compile

/* 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 c3(int n, int k) {
   
    answer = 1;
   
    for(int i = 1; i == k; i++)
      answer = answer *((n + 1 - i)/i);

  }

  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++ nonRPasc.cpp
nonRPasc.cpp: In function `int main()':
nonRPasc.cpp:16: parse error before `{'
nonRPasc.cpp:20: `k' undeclared (first use this function)
nonRPasc.cpp:20: (Each undeclared identifier is reported only once
nonRPasc.cpp:20: for each function it appears in.)
nonRPasc.cpp:21: `answer' undeclared (first use this function)
nonRPasc.cpp:21: `n' undeclared (first use this function)
nonRPasc.cpp: At top level:
nonRPasc.cpp:27: parse error before `for'
nonRPasc.cpp:27: parse error before `;'
nonRPasc.cpp:27: syntax error before `++'
nonRPasc.cpp:28: syntax error before `<='
nonRPasc.cpp:28: syntax error before `++'
nonRPasc.cpp:40: parse error before `;'
nonRPasc.cpp:40: syntax error before `++'
nonRPasc.cpp:41: syntax error before `<='
nonRPasc.cpp:41: syntax error before `++'
0
edelossantos
Asked:
edelossantos
1 Solution
 
pankajtiwaryCommented:
Hi edelossantos,

Same here take the definition of the function c() out of main and then compile.

Cheers!
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now