| | |
Help With Pascal's Triangle in C++
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2004
Posts: 5
Reputation:
Solved Threads: 0
I am new to programming and am having REAL trouble trying to output Pascal's Triangle (formatted into a triagle) using multidimensional arrays. I would really appreciate some help.
In the program, the user is supposed to input the # of rows desired (up to 15... or else it won't fit on the screen). I would REALLY appreciate it if someone could help me. Thank you!
The output needs to look like this (but formatted to look like a triangle):
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
1 10 45 120 210 252 210 120 45 10 1
1 11 55 165 330 462 462 330 165 55 11 1
1 12 66 220 495 792 924 792 495 220 66 12 1
1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1
In the program, the user is supposed to input the # of rows desired (up to 15... or else it won't fit on the screen). I would REALLY appreciate it if someone could help me. Thank you!
The output needs to look like this (but formatted to look like a triangle):
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
1 10 45 120 210 252 210 120 45 10 1
1 11 55 165 330 462 462 330 165 55 11 1
1 12 66 220 495 792 924 792 495 220 66 12 1
1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1
•
•
Join Date: Oct 2004
Posts: 5
Reputation:
Solved Threads: 0
Here are some of the things the teacher gave us to work on... I've looked at some example code, but nothing I've tried seems remotely close. Here are some links he gave the class:
http://met.dnsalias.net:1111/teaching/f04/program5.jsp
http://en.wikipedia.org/wiki/Pascal%27s_triangle
http://met.dnsalias.net:1111/teaching/f04/program5.jsp
http://en.wikipedia.org/wiki/Pascal%27s_triangle
•
•
Join Date: Mar 2007
Posts: 3
Reputation:
Solved Threads: 0
The following link may be helpful: http://www.codechamber.com/tutorials...cpp/pascal.cpp
•
•
Join Date: Oct 2007
Posts: 2
Reputation:
Solved Threads: 0
what ur looking for is this.
C++ Syntax (Toggle Plain Text)
#include<iostream.h> #include<conio.h> void display(int[],int); int pas(int n) { int f=0; while(n>0) { f=f+n; n--; } return(f-1); } void main() { clrscr(); int n,a[100];int z=0; cout<<"enter n";cin>>n; for(int i=0;i<4;i++) { a[i]=1; } for(int x=3;x<n;x++) { z=pas(x);a[z]=1;z++;a[z]=1; } int c=0; c=pas(n); a[c]=1; for(int b=3;b<=n;b++) { int d=0; int e=0,f=0; e=pas(b-1)+2; f=pas(b-1); d=pas(b); while(e!=d) { a[e]=a[f]+a[f-1]; e++;f--; } } display(a,n); getch(); } void display(int a[],int n) { int j=40,k=10;int s=0;int p=1; gotoxy(j,k); cout<<a[s]; for(int t=1;t<n;t++) { p++; int h=0;j=j-2; k=k+2; for(int w=1;w<=p;w++) { s++; gotoxy(j+h,k); cout<<a[s]; h=h+4; } } }
Last edited by Narue; Oct 11th, 2007 at 12:26 pm. Reason: Added code tags and indentation
It's still a three year old thread. Though it is mildly humorous that people will bump ancient threads with bad advice and/or bad code. However, since this thread is so old, and because I'm bored, I'll reject your code and substitute my own:
C++ Syntax (Toggle Plain Text)
#include <iostream> int factorial ( int n, int k ) { int result = 1; while ( k-- > 0 ) result *= n--; return result; } int pascal_triangle ( int n, int k ) { return factorial ( n, k ) / factorial ( k, k ); } int main() { for ( int i = 0; i < 9; i++ ) { for ( int j = 0; j <= i; j++ ) std::cout<< pascal_triangle ( i, j ) <<' '; std::cout<<'\n'; } }
I'm here to prove you wrong.
How about my solution. Faster and can handle more rows. However,
this code is written in C. I will translate it to C++, if you want.
this code is written in C. I will translate it to C++, if you want.
C++ Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> int main() { int n, i, j; printf("How many rows you want to show?: "); scanf("%d", &n); for (i=0; i<n; i++) { int c = 1; for(j=i; j>=0; j--) { printf(" %d", c); c = c * j/(i-j+1); } printf("\n"); } }
•
•
Join Date: Oct 2009
Posts: 1
Reputation:
Solved Threads: 0
0
#9 Oct 9th, 2009
•
•
•
•
I am new to programming and am having REAL trouble trying to output Pascal's Triangle (formatted into a triagle) using multidimensional arrays. I would really appreciate some help.
In the program, the user is supposed to input the # of rows desired (up to 15... or else it won't fit on the screen). I would REALLY appreciate it if someone could help me. Thank you!
The output needs to look like this (but formatted to look like a triangle):
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
1 10 45 120 210 252 210 120 45 10 1
1 11 55 165 330 462 462 330 165 55 11 1
1 12 66 220 495 792 924 792 495 220 66 12 1
1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1
{
for(j=6;j>=i;j--)
{
printf("*");
}
printf(\n);
}
![]() |
Similar Threads
- C++ codes for pascal triangle (C++)
- Custom pascal triangle ? (C)
- Need Help With Printing Pascal's Triangle In C (C)
Other Threads in the C++ Forum
- Previous Thread: Please help! Need to find the closest pair of points recursively in c++
- Next Thread: readout
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings struct temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






