User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Jun 2006
Location: India
Posts: 6,731
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 323
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

Custom pascal triangle ?

  #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 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."
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Posts: 4,588
Reputation: iamthwee is a jewel in the rough iamthwee is a jewel in the rough iamthwee is a jewel in the rough iamthwee is a jewel in the rough 
Rep Power: 15
Solved Threads: 293
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Custom pascal triangle ?

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


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 subroutine



Main
    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!
Reply With Quote  
Join Date: Jun 2004
Location: H4x0rville
Posts: 2,105
Reputation: server_crash is on a distinguished road 
Rep Power: 9
Solved Threads: 18
server_crash's Avatar
server_crash server_crash is offline Offline
Postaholic

Re: Custom pascal triangle ?

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

  
   R!
--------
C!(R-C)!
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,731
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 323
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

Re: Custom pascal triangle ?

  #4  
Jul 11th, 2006
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."
Reply With Quote  
Join Date: Sep 2004
Posts: 6,005
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 408
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Custom pascal triangle ?

  #5  
Jul 11th, 2006
Pascal's triangle is boring. How about something more fractalish:
#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();
}
>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 <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.
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,731
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 323
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

Re: Custom pascal triangle ?

  #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."

"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."
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,731
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 323
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

Re: Custom pascal triangle ?

  #7  
Jul 12th, 2006
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."
Reply With Quote  
Join Date: Aug 2005
Posts: 4,588
Reputation: iamthwee is a jewel in the rough iamthwee is a jewel in the rough iamthwee is a jewel in the rough iamthwee is a jewel in the rough 
Rep Power: 15
Solved Threads: 293
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Custom pascal triangle ?

  #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?
Member of: F-ugly code club

Join today don't delay!
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,731
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 323
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

Re: Custom pascal triangle ?

  #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."

"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."
Reply With Quote  
Join Date: Jul 2005
Posts: 1,066
Reputation: Lerner is a jewel in the rough Lerner is a jewel in the rough Lerner is a jewel in the rough Lerner is a jewel in the rough 
Rep Power: 8
Solved Threads: 138
Lerner Lerner is offline Offline
Veteran Poster

Re: Custom pascal triangle ?

  #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 3:27 pm.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 11:12 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC