Programming "The Triangle"
"The Triangle" refers to Pascal''s Triangle. I was just doing some gambling odds for my poker hands when i realized that pascal''s triangle and the binomial expansion theory are very closly related to combinations in probability. So, being that motivated little programmer i am, i decided to write a program that would print out pascals triangle to a given number of rows; formatted output to look like a triangle. I then realized that this one was going to be different than other little things i have done. I was just wondereing how ppl would go about the triangle (dont worry about the formattin part, i can do that with C++ output stuff).
I know ill probably think of it in my sleep or in a dream or something, but i was just wondereing how you guys would go about doing it.
mmmmh ... linked lists comes to mind
youpla :-P
youpla :-P
-----------------------------Sancte Isidore ora pro nobis !
Although some might suspect you''re trying to make us do your homework, I don''t think this is the case. And besides, if a guy wants to fool himself, let him !
No linked lists, I did it like this, using arrays (in 5 minutes):
Perhaps it could be sped up, but that''s for someone else to do .
Dormeur
No linked lists, I did it like this, using arrays (in 5 minutes):
#include <iostream>using namespace std;void triangle(int n) { // Calculate Pascal''s triangle for n int *row, *prevRow, *tmp; row = new int [n]; prevRow = new int [n]; // Initialization prevRow[0] = 1; // Do the rest of the triangle for (int i = 1; i < n; i++) { row[0] = 1; row[i-1] = 1; int j; for (j = 1; j < i-1; j++) { row[j] = prevRow[j-1] + prevRow[j]; } // Print out for (j = 0; j < i; j++) cout << row[j] << " "; cout << endl; // Swap row and prevRow tmp = prevRow; prevRow = row; row = tmp; } delete [] row; delete [] prevRow;}int main(int argc, char *argv[]) { int n = 10; if (argc > 1) n = atoi(argv[1]); cout << "Calculating Pascal''s Triangle for n = " << n << "..." << endl; triangle(n); cout << "Done" << endl; return 0;}
Perhaps it could be sped up, but that''s for someone else to do .
Dormeur
Wout "Dormeur" NeirynckThe Delta Quadrant Development Page
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement