•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 422,389 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 4,756 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: Programming Forums
Views: 8567 | Replies: 7
![]() |
•
•
Join Date: Oct 2004
Posts: 5
Reputation:
Rep Power: 0
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:
Rep Power: 0
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: 1
Reputation:
Rep Power: 0
Solved Threads: 0
The following link may be helpful: http://www.codechamber.com/tutorials...cpp/pascal.cpp
•
•
Join Date: Oct 2007
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 0
what ur looking for is this.
#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 11:26 am. 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:
#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 a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
•
•
Join Date: Mar 2005
Location: Phnom Penh, Cambodia
Posts: 410
Reputation:
Rep Power: 5
Solved Threads: 36
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.
#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");
}
}![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Need Help With Printing Pascal's Triangle In C (C)
- Custom pascal triangle ? (C)
- C++ codes for pascal triangle (C++)
Other Threads in the C++ Forum
- Previous Thread: Average help
- Next Thread: help me program this kind of output..



Linear Mode