| | |
Custom pascal triangle ?
![]() |
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?
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.
How about some pseudo code for evaluating the row:
Subroutine
Main
Subroutine
C Syntax (Toggle Plain Text)
int compute_pascal(int row, int position) if(position = 1) then return 1; endif else if(position = row) then return 1; end elseif else return compute_pascal(row-1, position) + compute_pascal(row-1, position-1); end else end subroutine
Main
C Syntax (Toggle Plain Text)
int row, position print message "Please input a row : " input:row for ( int i = 1; i <= row; i++ ) then Print: compute_pascal(row, i); Print:" " endfor end main
*Voted best profile in the world*
•
•
•
•
Originally Posted by server_crash
Formula for each element:
C Syntax (Toggle Plain Text)
R! -------- C!(R-C)!
Please elaborate if possible.
Thanks.
I don't accept change; I don't deserve to live.
Pascal's triangle is boring. How about something more fractalish:
>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:
C Syntax (Toggle Plain Text)
#include <iomanip> #include <iostream> using namespace std; int main() { int rows = 16; for ( int i = 0; i < rows; i++ ) { cout<< setw ( rows - i - 1 ) <<""; for ( int j = 0; j <= i; j++ ) cout<< ( ( ~i & j ) ? ' ' : '*' ) <<' '; cout<<'\n'; } cin.get(); }
>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:
C Syntax (Toggle Plain Text)
#include <iostream> using namespace std; double factorial ( double n ) { return ( n > 1 ) ? n * factorial ( n - 1 ) : 1; } double ncr ( int n, int r ) { return factorial ( n ) / ( factorial ( r ) * factorial ( n - r ) ); } int main() { cout.setf ( ios::fixed, ios::floatfield ); cout.precision ( 0 ); for ( int i = 0; i < 15; i++ ) { for ( int j = 0; j <= i; j++ ) cout<< right << ncr ( i, j ) <<' '; cout<<'\n'; } cin.get(); }
I'm here to prove you wrong.
•
•
•
•
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?
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:
•
•
•
•
Originally Posted by Narue
Pascal's triangle is boring. How about something more fractalish:
Thanks a lot.
I don't accept change; I don't deserve to live.
what thing?
This?
Where the user gives the three corners, from top to bottom...
Can't you figure that out yourself?
This?
3
4 5
4 9 5
4 13 14 5Where the user gives the three corners, from top to bottom...
Can't you figure that out yourself?
*Voted best profile in the world*
•
•
•
•
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?
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.
•
•
Join Date: Jul 2005
Posts: 1,671
Reputation:
Solved Threads: 261
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.
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.
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: add two matrices
- Next Thread: numeric to figures
| Thread Tools | Search this Thread |
adobe ansi api array asterisks binarysearch calculate centimeter char character convert copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile createprocess() csyntax directory feet fflush file floatingpointvalidation fork forloop frequency givemetehcodez global grade graphics gtkgcurlcompiling hacking highest homework i/o inches infiniteloop interest kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate looping loopinsideloop. match meter microsoft mysql number odf open opendocumentformat openwebfoundation owf pattern pdf performance posix power probleminc process program programming pyramidusingturboccodes radix read recv recvblocked repetition research scanf scheduling segmentationfault send sequential single socket socketprograming socketprogramming stack standard string suggestions systemcall threads turboc unix urboc user variable voidmain() wab win32api windows.h






