User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 422,389 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,756 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 8567 | Replies: 7
Reply
Join Date: Oct 2004
Posts: 5
Reputation: lostinthespiral is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
lostinthespiral lostinthespiral is offline Offline
Newbie Poster

Help With Pascal's Triangle in C++

  #1  
Oct 7th, 2004
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2004
Posts: 5
Reputation: lostinthespiral is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
lostinthespiral lostinthespiral is offline Offline
Newbie Poster

Re: Help With Pascal's Triangle in C++

  #2  
Oct 7th, 2004
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
Reply With Quote  
Join Date: Mar 2007
Posts: 1
Reputation: daniusr is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
daniusr daniusr is offline Offline
Newbie Poster

Help Re: Help With Pascal's Triangle in C++

  #3  
May 11th, 2007
The following link may be helpful: http://www.codechamber.com/tutorials...cpp/pascal.cpp
Reply With Quote  
Join Date: Sep 2004
Posts: 6,297
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 28
Solved Threads: 454
Super Moderator
Narue's Avatar
Narue Narue is online now Online
Expert Meanie

Re: Help With Pascal's Triangle in C++

  #4  
May 11th, 2007
>The following link may be helpful
I doubt it, unless the OP is still having trouble with this assignment three years later.
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Oct 2007
Posts: 2
Reputation: parth89 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
parth89 parth89 is offline Offline
Newbie Poster

Re: Help With Pascal's Triangle in C++

  #5  
Oct 11th, 2007
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;
    }
  }
}
Last edited by Narue : Oct 11th, 2007 at 11:26 am. Reason: Added code tags and indentation
Reply With Quote  
Join Date: Oct 2007
Posts: 2
Reputation: parth89 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
parth89 parth89 is offline Offline
Newbie Poster

Re: Help With Pascal's Triangle in C++

  #6  
Oct 11th, 2007
here is the code
Reply With Quote  
Join Date: Sep 2004
Posts: 6,297
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 28
Solved Threads: 454
Super Moderator
Narue's Avatar
Narue Narue is online now Online
Expert Meanie

Re: Help With Pascal's Triangle in C++

  #7  
Oct 11th, 2007
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';
  }
}
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Mar 2005
Location: Phnom Penh, Cambodia
Posts: 410
Reputation: invisal will become famous soon enough invisal will become famous soon enough 
Rep Power: 5
Solved Threads: 36
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: Help With Pascal's Triangle in C++

  #8  
Dec 4th, 2007
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");
    }
}
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 8:54 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC