Custom pascal triangle ?

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2006
Posts: 7,649
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: 474
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 ?

 
1
  #11
Jul 14th, 2006
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.
Last edited by ~s.o.s~; Jul 14th, 2006 at 3:06 am.
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,755
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: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Custom pascal triangle ?

 
0
  #12
Jul 14th, 2006
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.

  1. declare variable, lines, to hold number of lines in the pascals triangle
  2. prompt user to enter number of lines
  3. store number of lines from user in lines
  4.  
  5. declare pascals triangle to be a pointer to pointer to type int like this:
  6. int ** pascalsTri;
  7.  
  8. //allocate memory for an array of pointer to int
  9. pascalsTri = new int *[lines];
  10.  
  11. //for each pointer to int allocate memory for an array of int
  12. for(int i = 0; i < lines; ++i)
  13. pascalsTri[i] = new int[lines];
  14.  
  15. declare two ints, row and col, to act as indices to access ints in pascalsTri
  16.  
  17. pascalsTri is now equivalent to:
  18.  
  19. int pascalsTri[lines][lines];
  20.  
  21. and any given int in pascalsTri can be refered to individually using pascalsTri[row][col] with the appropriate values of row and col.
  22.  
  23. declare variables to hold the three given values of the triangle; topCorner, rightCorner, leftCorner.
  24.  
  25. prompt user for value of each variable and store it appropriately
  26.  
  27. now use rightCorner, leftCorner, row and col to get the other values of the triangle
  28.  
  29. for each row starting with row 2 up to and including the value lines - 1
  30. for each col starting with 0 up to and including the value of row
  31. if(col == 0)
  32. pascalTri[row][col] = leftCorner;
  33. else if(col == row)
  34. pascalTri[row][col] = rightCorner;
  35. else
  36. 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
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,649
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: 474
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
  #13
Jul 14th, 2006
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.
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 1
Reputation: creeping death is an unknown quantity at this point 
Solved Threads: 0
creeping death creeping death is offline Offline
Newbie Poster

Re: Custom pascal triangle ?

 
0
  #14
Mar 13th, 2009
@~s.o.s~seriously is that much really necessary? moreover gcc is giving many many errors.
  1. pas2.c:5: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘leftCorner’
  2. pas2.c:6: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘rightCorner’
  3. pas2.c:7: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘topElement’
  4. pas2.c: In function ‘displayTriangle’:
  5. pas2.c:14: error: ‘for’ loop initial declaration used outside C99 mode
  6. pas2.c:19: error: ‘for’ loop initial declaration used outside C99 mode
  7. pas2.c: In function ‘buildTriangle’:
  8. pas2.c:32: error: ‘topElement’ undeclared (first use in this function)
  9. pas2.c:32: error: (Each undeclared identifier is reported only once
  10. pas2.c:32: error: for each function it appears in.)
  11. pas2.c:37: error: ‘leftCorner’ undeclared (first use in this function)
  12. pas2.c:38: error: ‘rightCorner’ undeclared (first use in this function)
  13. pas2.c: In function ‘main’:
  14. pas2.c:76: warning: incompatible implicit declaration of built-in function ‘calloc’


  1. #include <stdio.h>
  2.  
  3. void pascaltriangle(unsigned int n)
  4. {
  5. unsigned int c, i, j, k;
  6.  
  7. for(i=0; i < n; i++) {
  8. c = 1;
  9. for(j=1; j <= 2*(n-1-i); j++) printf(" ");
  10. for(k=0; k <= i; k++) {
  11. printf("%d ", c);
  12. c = c * (i-k)/(k+1);
  13. }
  14. printf("\n");
  15. }
  16. }
  17.  
  18. int main()
  19. {
  20. int depth=0;
  21. printf("Enter depth");
  22. scanf("%d",&depth);
  23. pascaltriangle(depth);
  24. return 0;
  25. }

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


Views: 5371 | Replies: 13
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC