I am new to programming and am having REAL trouble trying to output Pascal's Triangle (formatted into a triagle) using multidimensional arrays. I would really appreciate some help.

In the program, the user is supposed to input the # of rows desired (up to 15... or else it won't fit on the screen). I would REALLY appreciate it if someone could help me. Thank you!

The output needs to look like this (but formatted to look like a triangle):


1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
1 10 45 120 210 252 210 120 45 10 1
1 11 55 165 330 462 462 330 165 55 11 1
1 12 66 220 495 792 924 792 495 220 66 12 1
1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1

Recommended Answers

All 10 Replies

>The following link may be helpful
I doubt it, unless the OP is still having trouble with this assignment three years later. :icon_rolleyes:

what ur looking for is this.

#include<iostream.h>
#include<conio.h>
void display(int[],int);
int pas(int n)
{
  int f=0;
  while(n>0)
  {
    f=f+n;
    n--;
  }
  return(f-1);
}
void main()
{
  clrscr();
  int n,a[100];int z=0;
  cout<<"enter n";cin>>n;
  for(int i=0;i<4;i++)
  {
    a[i]=1;
  }
  for(int x=3;x<n;x++)
  {
    z=pas(x);a[z]=1;z++;a[z]=1;
  }
  int c=0; c=pas(n); a[c]=1;
  for(int b=3;b<=n;b++)
  {
    int d=0; int e=0,f=0;
    e=pas(b-1)+2;
    f=pas(b-1);
    d=pas(b);
    while(e!=d)
    {
      a[e]=a[f]+a[f-1];
      e++;f--;
    }
  }
  display(a,n);
  getch();
}

void display(int a[],int n)
{
  int j=40,k=10;int s=0;int p=1;
  gotoxy(j,k);
  cout<<a[s];
  for(int t=1;t<n;t++)
  {
    p++; int h=0;j=j-2; k=k+2;
    for(int w=1;w<=p;w++)
    {
      s++;
      gotoxy(j+h,k);
      cout<<a[s];
      h=h+4;
    }
  }
}

here is the code

It's still a three year old thread. Though it is mildly humorous that people will bump ancient threads with bad advice and/or bad code. However, since this thread is so old, and because I'm bored, I'll reject your code and substitute my own:

#include <iostream>

int factorial ( int n, int k )
{
  int result = 1;

  while ( k-- > 0 )
    result *= n--;

  return result;
}

int pascal_triangle ( int n, int k )
{
  return factorial ( n, k ) / factorial ( k, k );
}

int main()
{
  for ( int i = 0; i < 9; i++ ) {
    for ( int j = 0; j <= i; j++ )
      std::cout<< pascal_triangle ( i, j ) <<' ';
    std::cout<<'\n';
  }
}

How about my solution. Faster and can handle more rows. However,
this code is written in C. I will translate it to C++, if you want.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n, i, j;
    printf("How many rows you want to show?: ");
    scanf("%d", &n);
    for (i=0; i<n; i++) {
        int c = 1;
        for(j=i; j>=0; j--) {
            printf(" %d", c);
            c = c * j/(i-j+1);
        }
        printf("\n");
    }
}

I am new to programming and am having REAL trouble trying to output Pascal's Triangle (formatted into a triagle) using multidimensional arrays. I would really appreciate some help.

In the program, the user is supposed to input the # of rows desired (up to 15... or else it won't fit on the screen). I would REALLY appreciate it if someone could help me. Thank you!

The output needs to look like this (but formatted to look like a triangle):

                                         1
                                     1     1
                                  1     2     1
                               1     3     3     1
                            1     4     6     4     1
                         1     5     10    10    5     1
                      1     6     15    20    15    6     1
                   1     7     21    35    35    21    7     1
                1     8     28    56    70    56    28    8     1
             1     9     36    84    126   126   84    36    9     1
          1     10    45    120   210   252   210   120   45    10    1
       1     11    55    165   330   462   462   330   165   55    11    1
    1     12    66    220   495   792   924   792   495   220   66    12    1
 1     13    78    286   715   1287  1716  1716  1287  715   286   78    13    1

end quote.

for (i=1;i<=6;i++)
{
for(j=6;j>=i;j--)
{
printf("*");
}
printf(\n);
}
#include <iostream>
using namespace std;

int main()
{
    const int ROWS = 15, COLS = 15;
    int pascal[ROWS][COLS] = {};

    pascal[0][0] = 1;
    cout << pascal[0][0] << endl;
    for ( int r = 1; r < ROWS; r++ )
    {
        pascal[r][0] = 1; // when c = 0, set the values to 1
        pascal[r][r] = 1; // when c = r (end of triangle), set the values to 1
        cout << pascal[r][0] << "    ";
        for ( int c = 1; c < r; c++ )
        {
            pascal[r][c] = pascal[r-1][c-1] + pascal[r-1][c];
            cout << pascal[r][c] << "    ";
        }
        cout << pascal[r][r] << endl;
    }
}

Thanks for the input nikita, but was it really worth bumping a 7 year old thread that already had working example code?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.