•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 374,019 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,757 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Views: 3737 | Replies: 12
![]() |
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 3:36 am.
"I don't accept change. I don't deserve to live."
"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
How about some pseudo code for evaluating the row:
Subroutine
Main
Subroutine
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 subroutineMain
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 Member of: F-ugly code club
Join today don't delay!
Join today don't delay!
•
•
Join Date: Jun 2004
Location: H4x0rville
Posts: 2,105
Reputation:
Rep Power: 9
Solved Threads: 18
•
•
•
•
Originally Posted by server_crash
Formula for each element:
R! -------- C!(R-C)!
Err...
Please elaborate if possible.
Thanks.
"I don't accept change. I don't deserve to live."
"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
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:
#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:
#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();
} Member of: Beautiful Code Club.
•
•
•
•
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."
"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Please can anyone help me on this thing?
Last edited by ~s.o.s~ : Jul 12th, 2006 at 1:58 pm.
"I don't accept change. I don't deserve to live."
"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
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 5 Where the user gives the three corners, from top to bottom...
Can't you figure that out yourself?
Member of: F-ugly code club
Join today don't delay!
Join today don't delay!
•
•
•
•
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."
"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
•
•
Join Date: Jul 2005
Posts: 1,066
Reputation:
Rep Power: 8
Solved Threads: 138
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 3:27 pm.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
Similar Threads
Other Threads in the C Forum
- Previous Thread: Binary resource data
- Next Thread: homeworkhelp



Linear Mode