| | |
Custom pascal triangle ?
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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.
THanks to all once again.
)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]
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
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
•
•
Join Date: Jul 2005
Posts: 1,755
Reputation:
Solved Threads: 283
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.
C Syntax (Toggle Plain Text)
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.
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
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
•
•
Join Date: Mar 2009
Posts: 1
Reputation:
Solved Threads: 0
@~s.o.s~seriously is that much really necessary? moreover gcc is giving many many errors.
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.
C Syntax (Toggle Plain Text)
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’
C Syntax (Toggle Plain Text)
#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.
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: add two matrices
- Next Thread: numeric to figures
Views: 5371 | Replies: 13
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile csyntax directory drawing dynamic executable fflush file fork frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling scripting segmentationfault send shape socketprograming spoonfeeding stack standard string strings structures student suggestions systemcall testautomation unix user variable voidmain() wab win32 win32api windows.h






