Custom pascal triangle ?

Reply

Join Date: Jun 2006
Posts: 7,581
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 461
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Custom pascal triangle ?

 
0
  #1
Jul 11th, 2006
Hello to alll programmers out there.

Here i am trying to design a custom pascal triangle where the user can specify the TOP ELEMENT as well as both the CORNER elements.

The problem i am facing is that the prog works well till the 3rd row but starts giving rubbish values after 3rd. Looks like there is some problem with my logic. I want to do this problem using only one array and not two arrays.

Any suggestions?

#include "stdafx.h"
#include <stdio.h>
const int leftCorner = 1;
const int rightCorner = 1;
const int topElement = 1;
int *pascalTriangle = 0;
int buildTriangle (int tmpPascalRow)
{
int prev, curr, next;
int tmpRow, elementIndex = 0;
int buffer = 0;
if (tmpPascalRow == 1)
{
pascalTriangle[0] = topElement;
return 1;
}
pascalTriangle[0] = leftCorner;
pascalTriangle[1] = rightCorner;
for (tmpRow = 3; tmpRow <= tmpPascalRow; ++tmpRow)
{
pascalTriangle[0] = leftCorner;
pascalTriangle[tmpRow - 1] = rightCorner;
buffer = leftCorner;
for (elementIndex = 1; elementIndex < tmpRow - 1; ++elementIndex)
{
buffer = pascalTriangle[elementIndex]; 
pascalTriangle[elementIndex] += buffer;
}
}
}
 
int main (void)
{
char inputBuffer[10];
int pascalRow = 0;
int index;
 
fputs ("Enter the number of rows required: ", stdout);
fgets (inputBuffer, sizeof (inputBuffer), stdin);
pascalRow = atoi (inputBuffer);
pascalTriangle = (int*) calloc (pascalRow, sizeof (int));
buildTriangle (pascalRow);
for (index = 0; index < pascalRow; ++index)
printf ("%d\t", pascalTriangle[index]);
getchar();
return 0;
}
Last edited by ~s.o.s~; Jul 11th, 2006 at 4:36 am.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 376
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Custom pascal triangle ?

 
0
  #2
Jul 11th, 2006
How about some pseudo code for evaluating the row:


Subroutine
  1. int compute_pascal(int row, int position)
  2.  
  3. if(position = 1)
  4. then
  5. return 1;
  6. endif
  7. else if(position = row)
  8. then
  9. return 1;
  10. end elseif
  11. else
  12. return compute_pascal(row-1, position) +
  13. compute_pascal(row-1, position-1);
  14. end else
  15. end subroutine



Main
  1. int row, position
  2. print message "Please input a row : "
  3. input:row
  4.  
  5.  
  6.  
  7. for ( int i = 1; i <= row; i++ )
  8. then
  9.  
  10. Print: compute_pascal(row, i);
  11. Print:" "
  12. endfor
  13.  
  14. end main
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Custom pascal triangle ?

 
0
  #3
Jul 11th, 2006
Formula for each element:

  1.  
  2. R!
  3. --------
  4. C!(R-C)!
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,581
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 461
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Custom pascal triangle ?

 
0
  #4
Jul 11th, 2006
Originally Posted by server_crash
Formula for each element:

  1.  
  2. R!
  3. --------
  4. C!(R-C)!
Err...
Please elaborate if possible.
Thanks.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,541
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: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Custom pascal triangle ?

 
1
  #5
Jul 11th, 2006
Pascal's triangle is boring. How about something more fractalish:
  1. #include <iomanip>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int rows = 16;
  9.  
  10. for ( int i = 0; i < rows; i++ ) {
  11. cout<< setw ( rows - i - 1 ) <<"";
  12.  
  13. for ( int j = 0; j <= i; j++ )
  14. cout<< ( ( ~i & j ) ? ' ' : '*' ) <<' ';
  15.  
  16. cout<<'\n';
  17. }
  18.  
  19. cin.get();
  20. }
>Here i am trying to design a custom pascal triangle where the user can
>specify the TOP ELEMENT as well as both the CORNER elements.
That's not very logical. Can you give a few examples?

>Err...
>Please elaborate if possible.
Take note of the ncr function:
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. double factorial ( double n )
  6. {
  7. return ( n > 1 ) ? n * factorial ( n - 1 ) : 1;
  8. }
  9.  
  10. double ncr ( int n, int r )
  11. {
  12. return factorial ( n ) / ( factorial ( r ) * factorial ( n - r ) );
  13. }
  14.  
  15. int main()
  16. {
  17. cout.setf ( ios::fixed, ios::floatfield );
  18. cout.precision ( 0 );
  19.  
  20. for ( int i = 0; i < 15; i++ ) {
  21. for ( int j = 0; j <= i; j++ )
  22. cout<< right << ncr ( i, j ) <<' ';
  23.  
  24. cout<<'\n';
  25. }
  26.  
  27. cin.get();
  28. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,581
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 461
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Custom pascal triangle ?

 
0
  #6
Jul 11th, 2006
Originally Posted by Narue
>Here i am trying to design a custom pascal triangle where the user >can specify the TOP ELEMENT as well as both the CORNER >elements.
That's not very logical. Can you give a few examples?
Here is the eg.
3
4 5
4 9 5
4 13 14 5 ...

Though this does not exactly satisfy the rule of pascals triangle it is actually a variation of it which i saw on some web site. It stated the use of Pascal Triangle in composing music tones or something.

Originally Posted by Narue
>Err...
>Please elaborate if possible.

Take note of the ncr function:
Dont think this would solve the hybrid pascals problem?

Originally Posted by Narue
Pascal's triangle is boring. How about something more fractalish:
Whoa this is a beauty ! Miss Narue can you give me other such shapes to derive as execrices and where you got the problem stmt from.

Thanks a lot.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,581
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 461
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Custom pascal triangle ?

 
0
  #7
Jul 12th, 2006
Please can anyone help me on this thing?
Last edited by ~s.o.s~; Jul 12th, 2006 at 2:58 pm.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 376
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Custom pascal triangle ?

 
0
  #8
Jul 12th, 2006
what thing?

This?

       
       3
     4  5
   4  9  5
 4 13  14  5

Where the user gives the three corners, from top to bottom...

Can't you figure that out yourself?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,581
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 461
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Custom pascal triangle ?

 
0
  #9
Jul 13th, 2006
Originally Posted by iamthwee
what thing?

This?

       
       3
     4  5
   4  9  5
4 13  14  5

Where the user gives the three corners, from top to bottom...

Can't you figure that out yourself?
With two arrays yes, but with one array no.
Thats y i wanted the help of you ppl.
I have even posted my effort so far in my first post but i am stuck there so plese help me out.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,671
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Custom pascal triangle ?

 
1
  #10
Jul 13th, 2006
The first two lines of the triangle must be given. However, after that you can calculate each value on your own as needed. One way is to recognize that if you number the lines of the triangle 1 to x then the maximum number of values per line is also x, that is there are 4 values on the 4th line of the triangle, etc. So you could store the values of the triangle in a two dimensional array of size number of lines by number of lines. Then for each row in the array of index greater than 1:
1) if column index is 0 the value is a given
2) if the column index is the same as the row index then the value is a given
3) otherwise the value of the column at any other given column index less than the row index is the sum of the values at indexes row - 1 and column plus the value at indexes row - 1 and column - 1.

The sample triangle values as indicated in the most recent posts within the 2D array would then look like this:

3
4 5
4 9 5
4 13 14 5

To display the triangle in traditional equilateral triangle display rather than the right triangle display format is a little trickier, but doable, if you must.
Last edited by Lerner; Jul 13th, 2006 at 4:27 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC