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;
}
Member Avatar for iamthwee

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

Formula for each element:

R!
--------
C!(R-C)!

Formula for each element:

R!
--------
C!(R-C)!

Err...
Please elaborate if possible.
Thanks.

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();
}

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

>Err...
>Please elaborate if possible.

Take note of the ncr function:

Dont think this would solve the hybrid pascals problem?

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.

Please can anyone help me on this thing?

Member Avatar for 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?

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.

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.

THanks to all you ppls help i finally managed to put the code together ( though using two arrays which i couldnt make to one :( )

Here is the code i am pasting here for others to have a reference.

[c]
#include "stdafx.h"
 
#include <stdio.h>

constint leftCorner = 3;
constint rightCorner = 4;
constint topElement = 2;
int *oldTriangle = 0;
int *newTriangle = 0;
void displayTriangle (int iteration)
{
if ((iteration % 2) == 1) 
{
for (int i = 1; i <= iteration; ++i)
printf ("%d ", oldTriangle[i-1]);
}
else
{
for (int i = 1; i <= iteration; ++i)
printf ("%d ", newTriangle[i-1]);
}
printf ("\n");
}
 
int buildTriangle (int tmpPascalRow)
{
int tmpRow, elementIndex;
int* src = NULL;
int* dest = NULL;
 
 
oldTriangle[0] = topElement;
displayTriangle (1);
if (tmpPascalRow == 1)
return 1;
 
newTriangle[0] = leftCorner;
newTriangle[1] = rightCorner;
displayTriangle (2);
if (tmpPascalRow == 2)
return 1;
 
for (tmpRow = 3; tmpRow <= tmpPascalRow; ++tmpRow)
{
if ((tmpRow % 2) == 1)
{
dest = oldTriangle;
src = newTriangle;
}
else
{
dest = newTriangle;
src = oldTriangle;
}
 
dest[0] = src[0];
dest[tmpRow-1] = src[tmpRow-2];
for (elementIndex = 1; elementIndex < tmpRow - 1; ++elementIndex)
{
dest[elementIndex] = src[elementIndex] + src[elementIndex - 1];
}
displayTriangle (tmpRow);
}
}
 
int main (void)
{
char inputBuffer[10];
int pascalRow = 0;
int index;
 
fputs ("Enter the number of rows required: ", stdout);
fgets (inputBuffer, sizeof (inputBuffer), stdin);
fflush (stdin);
pascalRow = atoi (inputBuffer);
newTriangle = (int*) calloc (pascalRow, sizeof (int));
oldTriangle = (int*) calloc (pascalRow, sizeof (int));
buildTriangle (pascalRow);
getchar();
return 0;
}
[/c]

THanks to all once again.

commented: Thanks for posting the solution for others to refer. +2

Use of multidimensional arrays isn't all that difficult, though it can be a bit confusing if you haven't used them before, particularly if you start out using dynamic memory allocation instead of static memory allocation for your first experience. I'm going to use C++ syntax in the following pseudocode since I'm more comfortable with that, but you shouldn't have much trouble converting the C++ syntax to the appropraite C syntax.

declare variable, lines, to hold number of lines in the pascals triangle
prompt user to enter number of lines
store number of lines from user in lines

declare pascals triangle to be a pointer to pointer to type int like this:
int ** pascalsTri;

//allocate memory for an array of pointer to int
pascalsTri = new int *[lines];

//for each pointer to int allocate memory for an array of int
for(int i = 0; i < lines; ++i)
  pascalsTri[i] = new int[lines];

declare two ints, row and col, to act as indices to access ints in pascalsTri

pascalsTri is now equivalent to:

int pascalsTri[lines][lines];

and any given int in pascalsTri can be refered to individually using pascalsTri[row][col] with the appropriate values of row and col.

declare variables to hold the three given values of the triangle; topCorner, rightCorner, leftCorner.

prompt user for value of each variable and store it appropriately

now use rightCorner, leftCorner, row and col to get the other values of the triangle

for each row starting with row 2 up to and including the value lines - 1
  for each col starting with 0 up to and including the value of row 
     if(col == 0)
       pascalTri[row][col] = leftCorner;
     else if(col == row)
       pascalTri[row][col] = rightCorner;
     else
       assign the sum of the int at pascalTri[row - 1][col - 1] plus pascalTri[row -1][col] to the current int with indices row and col

Yes the given problem accordnig to me can be solved in three different ways :

1) Using two arrays dynamically allocated (the way which i have done)

2) Using two dimensional array dynamically allocated (the way proposed by Mr. Lerner)

3) Using an array having size (2 * Number of rows of pascal triangle) wherein we use the left part of the array to store the old pascal triangle and the right part of the triangle holding the new row of pascal triangle ( a slight variation of my method).

Hope this serves as a pointer to all the ppl who want to code pascal triangle. Thanks to all.

@~s.o.s~seriously is that much really necessary? moreover gcc is giving many many errors.

pas2.c:5: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘leftCorner’
pas2.c:6: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘rightCorner’
pas2.c:7: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘topElement’
pas2.c: In function ‘displayTriangle’:
pas2.c:14: error: ‘for’ loop initial declaration used outside C99 mode
pas2.c:19: error: ‘for’ loop initial declaration used outside C99 mode
pas2.c: In function ‘buildTriangle’:
pas2.c:32: error: ‘topElement’ undeclared (first use in this function)
pas2.c:32: error: (Each undeclared identifier is reported only once
pas2.c:32: error: for each function it appears in.)
pas2.c:37: error: ‘leftCorner’ undeclared (first use in this function)
pas2.c:38: error: ‘rightCorner’ undeclared (first use in this function)
pas2.c: In function ‘main’:
pas2.c:76: warning: incompatible implicit declaration of built-in function ‘calloc’
#include <stdio.h>
 
void pascaltriangle(unsigned int n)
{
  unsigned int c, i, j, k;
 
  for(i=0; i < n; i++) {
    c = 1;
    for(j=1; j <= 2*(n-1-i); j++) printf(" ");
    for(k=0; k <= i; k++) {
      printf("%d ", c);
      c = c * (i-k)/(k+1);
    }
    printf("\n");
  }
}
 
int main()
{
  int depth=0;
  printf("Enter depth");
  scanf("%d",&depth);
  pascaltriangle(depth);
  return 0;
}

not only would that be a huge waste of memory(using array) the time complexity of the code is pretty bad also...

just my 2cents.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.