Solved

Non-Recursive Pascal's Triangle

Posted on 2004-10-24
1,306 Views
Last Modified: 2007-12-19
// 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
Question by:edelossantos
    1 Comment
     
    LVL 4

    Accepted Solution

    by:
    Hi edelossantos,

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

    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?

    This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
    Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

    913 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

    16 Experts available now in Live!

    Get 1:1 Help Now