943,965 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 17844
  • C++ RSS
Oct 7th, 2004
0

Help With Pascal's Triangle in C++

Expand 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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lostinthespiral is offline Offline
5 posts
since Oct 2004
Oct 7th, 2004
0

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

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lostinthespiral is offline Offline
5 posts
since Oct 2004
May 11th, 2007
0

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

The following link may be helpful: http://www.codechamber.com/tutorials...cpp/pascal.cpp
Reputation Points: 10
Solved Threads: 0
Newbie Poster
daniusr is offline Offline
3 posts
since Mar 2007
May 11th, 2007
0

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

>The following link may be helpful
I doubt it, unless the OP is still having trouble with this assignment three years later.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 11th, 2007
0

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

what ur looking for is this.
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
parth89 is offline Offline
2 posts
since Oct 2007
Oct 11th, 2007
0

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

here is the code
Reputation Points: 10
Solved Threads: 0
Newbie Poster
parth89 is offline Offline
2 posts
since Oct 2007
Oct 11th, 2007
0

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

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:
C++ Syntax (Toggle Plain Text)
  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. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Dec 4th, 2007
-1

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

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.

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 350
Solved Threads: 63
Posting Pro
invisal is offline Offline
562 posts
since Mar 2005
Oct 9th, 2009
0
Re: 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
for (i=1;i<=6;i++)
{
for(j=6;j>=i;j--)
{
printf("*");
}
printf(\n);
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
amrita76 is offline Offline
1 posts
since Oct 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Please help! Need to find the closest pair of points recursively in c++
Next Thread in C++ Forum Timeline: readout





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC