•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 397,802 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,445 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: 3843 | Replies: 12
![]() |
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 2:06 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."
•
•
Join Date: Jul 2005
Posts: 1,098
Reputation:
Rep Power: 9
Solved Threads: 142
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.
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."
"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."
![]() |
•
•
•
•
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