943,673 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 11721
  • C++ RSS
Aug 26th, 2004
0

pascals triangle

Expand Post »
hi
i want to display pascals triangle on computer screen.the pascals triangle
goes like this. and what is logic.
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1 and so on.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
let us c is offline Offline
17 posts
since Aug 2004
Aug 28th, 2004
0

Re: pascals triangle

Hi there are many logics behind this problem. This is the simple logic. Though it is inefficient, this will give you a better approach to this program

a[0] = 1
for(i=0;i<n;i++){
for(j=0;j<=i;j++){
Value_to_print = a[j-1]+a[j];
/* Print the values in the order you want */
}
a[j] = 0
a[j-1] = Value_to_print
}
Reputation Points: 12
Solved Threads: 0
Newbie Poster
ganesh is offline Offline
2 posts
since Aug 2004
Sep 2nd, 2004
0

Re: pascals triangle

Well, you can try this code, but I don't know if it will work beyond n=9.
C++ Syntax (Toggle Plain Text)
  1. #include <iostream.h>
  2. #include <conio.h>
  3.  
  4. /* This program creates a half pyramid of nos. Created on 12.6.2k3. */
  5.  
  6. void main()
  7.  
  8. {
  9. int n, i, r;
  10. cout<<"Enter the number of numbers: ";
  11. cin>>n;
  12. if (n>9)
  13. cout<<"\n This won't work out. ";
  14. else
  15. {
  16. for (i=1; i<=n; i++)
  17. {
  18. for (r=1; r<=i; r++)
  19. {
  20. cout<<r;
  21. }
  22. cout<<endl;
  23. }
  24. }
  25. getch();
  26. }
  27.  
  28. /* Output:
  29. Enter the number of numbers: 7
  30. 1
  31. 12
  32. 123
  33. 1234
  34. 12345
  35. 123456
  36. 1234567
  37.  
  38. */
btw, I didn't know it was called a pascal's triangle. I used to refer to it just as a no. pyramid or whatever.
Reputation Points: 12
Solved Threads: 0
Newbie Poster
iamboredguy is offline Offline
23 posts
since Aug 2004
Aug 6th, 2010
0
Re: pascals triangle
program by iamboredguy is not a Pascal Triangle !!
Pascal Traingle is expected to give following sequence.
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Last edited by manali04; Aug 6th, 2010 at 4:15 am. Reason: NA
Reputation Points: 10
Solved Threads: 0
Newbie Poster
manali04 is offline Offline
3 posts
since Jul 2010
Aug 6th, 2010
0
Re: pascals triangle
First logic feature to see is that it is always symmetric about the central value in one sequence, so you will probably just need to store or compute one half of each row (from start to middle) and repeat in reverse for the remainder of the row (taking into account even and odd number of elements).

The second logic feature to see is more evident in the normal display of the pascal triangle (forgive the ASCII art):
C++ Syntax (Toggle Plain Text)
  1. 1
  2. / \
  3. 1 1
  4. / \/ \
  5. 1 2 1
  6. / \/ \/ \
  7. 1 3 3 1
  8. / \/ \/ \/ \
  9. 1 4 6 4 1
  10. ....
From this it's clear that the operation that joins a \/ segment is the addition. So like in ganesh's pseudo-code, the basic idea is to add two adjacent elements of the previous row to get the corresponding element of the next row (except for the first element which is always 1 and the centre element of an odd-row which might be twice the element closest to centre of the even-row above it).

Show us some code of your own, and we can help you further.
Featured Poster
Reputation Points: 1437
Solved Threads: 398
Posting Virtuoso
mike_2000_17 is online now Online
1,839 posts
since Jul 2010
Aug 6th, 2010
0
Re: pascals triangle
I believe you can use the coefficient formula.
Reputation Points: 10
Solved Threads: 3
Newbie Poster
sunyifei23 is offline Offline
11 posts
since Jul 2010

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: how to remove a particular pattern and retain other patterns in a string
Next Thread in C++ Forum Timeline: How do i work on a 12 digit integer in c++





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


Follow us on Twitter


© 2011 DaniWeb® LLC