954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help With Pascal's Triangle in C++

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

lostinthespiral
Newbie Poster
5 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

Here are some of the things the teacher gave us to work on... I've looked at some example code, but nothing I've tried seems remotely close. Here are some links he gave the class:
http://met.dnsalias.net:1111/teaching/f04/program5.jsp
http://en.wikipedia.org/wiki/Pascal%27s_triangle

lostinthespiral
Newbie Poster
5 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 
daniusr
Newbie Poster
3 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

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

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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;
    }
  }
}
parth89
Newbie Poster
2 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

here is the code

parth89
Newbie Poster
2 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

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';
  }
}
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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");
    }
}
invisal
Posting Pro
562 posts since Mar 2005
Reputation Points: 350
Solved Threads: 64
 

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


for (i=1;i<=6;i++)
{
for(j=6;j>=i;j--)
{
printf("*");
}
printf(\n);
}

amrita76
Newbie Poster
1 post since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You