Help With Pascal's Triangle in C++

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2004
Posts: 5
Reputation: lostinthespiral is an unknown quantity at this point 
Solved Threads: 0
lostinthespiral lostinthespiral is offline Offline
Newbie Poster

Help With Pascal's Triangle in C++

 
0
  #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
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 5
Reputation: lostinthespiral is an unknown quantity at this point 
Solved Threads: 0
lostinthespiral lostinthespiral is offline Offline
Newbie Poster

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

 
0
  #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 Quick reply to this message  
Join Date: Mar 2007
Posts: 3
Reputation: daniusr is an unknown quantity at this point 
Solved Threads: 0
daniusr daniusr is offline Offline
Newbie Poster

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

 
0
  #3
May 11th, 2007
The following link may be helpful: http://www.codechamber.com/tutorials...cpp/pascal.cpp
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 715
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
0
  #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 here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 2
Reputation: parth89 is an unknown quantity at this point 
Solved Threads: 0
parth89 parth89 is offline Offline
Newbie Poster

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

 
0
  #5
Oct 11th, 2007
what ur looking for is this.
  1. #include<iostream.h>
  2. #include<conio.h>
  3. void display(int[],int);
  4. int pas(int n)
  5. {
  6. int f=0;
  7. while(n>0)
  8. {
  9. f=f+n;
  10. n--;
  11. }
  12. return(f-1);
  13. }
  14. void main()
  15. {
  16. clrscr();
  17. int n,a[100];int z=0;
  18. cout<<"enter n";cin>>n;
  19. for(int i=0;i<4;i++)
  20. {
  21. a[i]=1;
  22. }
  23. for(int x=3;x<n;x++)
  24. {
  25. z=pas(x);a[z]=1;z++;a[z]=1;
  26. }
  27. int c=0; c=pas(n); a[c]=1;
  28. for(int b=3;b<=n;b++)
  29. {
  30. int d=0; int e=0,f=0;
  31. e=pas(b-1)+2;
  32. f=pas(b-1);
  33. d=pas(b);
  34. while(e!=d)
  35. {
  36. a[e]=a[f]+a[f-1];
  37. e++;f--;
  38. }
  39. }
  40. display(a,n);
  41. getch();
  42. }
  43.  
  44. void display(int a[],int n)
  45. {
  46. int j=40,k=10;int s=0;int p=1;
  47. gotoxy(j,k);
  48. cout<<a[s];
  49. for(int t=1;t<n;t++)
  50. {
  51. p++; int h=0;j=j-2; k=k+2;
  52. for(int w=1;w<=p;w++)
  53. {
  54. s++;
  55. gotoxy(j+h,k);
  56. cout<<a[s];
  57. h=h+4;
  58. }
  59. }
  60. }
Last edited by Narue; Oct 11th, 2007 at 12:26 pm. Reason: Added code tags and indentation
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 2
Reputation: parth89 is an unknown quantity at this point 
Solved Threads: 0
parth89 parth89 is offline Offline
Newbie Poster

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

 
0
  #6
Oct 11th, 2007
here is the code
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 715
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
0
  #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:
  1. #include <iostream>
  2.  
  3. int factorial ( int n, int k )
  4. {
  5. int result = 1;
  6.  
  7. while ( k-- > 0 )
  8. result *= n--;
  9.  
  10. return result;
  11. }
  12.  
  13. int pascal_triangle ( int n, int k )
  14. {
  15. return factorial ( n, k ) / factorial ( k, k );
  16. }
  17.  
  18. int main()
  19. {
  20. for ( int i = 0; i < 9; i++ ) {
  21. for ( int j = 0; j <= i; j++ )
  22. std::cout<< pascal_triangle ( i, j ) <<' ';
  23. std::cout<<'\n';
  24. }
  25. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

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

 
-1
  #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.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main()
  5. {
  6. int n, i, j;
  7. printf("How many rows you want to show?: ");
  8. scanf("%d", &n);
  9. for (i=0; i<n; i++) {
  10. int c = 1;
  11. for(j=i; j>=0; j--) {
  12. printf(" %d", c);
  13. c = c * j/(i-j+1);
  14. }
  15. printf("\n");
  16. }
  17. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 1
Reputation: amrita76 is an unknown quantity at this point 
Solved Threads: 0
amrita76 amrita76 is offline Offline
Newbie Poster
 
0
  #9
Oct 9th, 2009
Originally Posted by lostinthespiral View Post
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);
}
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC