-4

Guys how can i display a pascal triangle in for loop using a 2 dimensional array?

Here's my code.

void print(int a[3][3]) {
    for (int r = 0; r < 3; r++) {
        cout << endl;
        for (int c = 0; c < 3; c++) { // how to display a pascal triangle?
            cout << a[r][c] << " ";
        }
    }
}

Sample run:

 123
  56  
   9
lurker
  • 56,987
  • 9
  • 69
  • 103
HelpMe
  • 165
  • 3
  • 14
  • 2
    What are you trying to do? – 0xfee1dead Aug 17 '14 at 13:28
  • I want to put something that will display a pascal triangle. I need that "something". – HelpMe Aug 17 '14 at 13:30
  • Your sample run doesn't look even remotely similar to a Pascal triangle. And the code you gave just outputs the contents of a 2D array. I suspect this is a homework question and you want us to do your homework. – celtschk Aug 17 '14 at 13:33
  • It's not a homework I just want to make a program that will display a pascal triangle. – HelpMe Aug 17 '14 at 13:35
  • I have to improve my question. – HelpMe Aug 17 '14 at 13:40
  • you don't have to use a 2-D matrix, you can do it using a 1-D array. – dguan Aug 17 '14 at 13:43
  • You confuse data storage and data output here. An array cannot display anything, but you can use other means of the language to display an array. And of course, you should not use raw arrays. Make that a `std::vector >`. – Christian Hackl Aug 17 '14 at 14:56

1 Answers1

0

How about that, I've found the following code on [http://www.programiz.com/article/c%2B%2B-programming-pattern]:

#include <iostream>
using namespace std;
int main()
{
    int rows,coef=1,space,i,j;
    cout<<"Enter number of rows: ";
    cin>>rows;
    for(i=0;i<rows;i++)
    {
        for(space=1;space<=rows-i;space++)
        cout<<"  ";
        for(j=0;j<=i;j++)
        {
            if (j==0||i==0)
                coef=1;
            else
               coef=coef*(i-j+1)/j;
            cout<<"    "<<coef;
        }
        cout<<endl;
    }
}

Tried that one out, seemed "mathematically" okay ... Regards, M.

Micha
  • 181
  • 5