954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ codes for pascal triangle

Hello everyone,
im new here and dont know yet most of the policies.. but i have been reading lots of your C++ codes, mostly, codes made by Narue.... i wonder if you someone could help me make a code for pascal's triangle wherein the ouput would look like a triangle and not like this:
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


hope someone could help.. thanks a lot!!!

PolarClaw
Newbie Poster
7 posts since Jul 2006
Reputation Points: 10
Solved Threads: 0
 
Hello everyone, im new here and dont know yet most of the policies..

Hello and welcome. Start learning the policy that we only give homework help to those who show effort.

You will have to post what you have done upto now.

WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 

i see....

can someone just teach me how to align the output to center?

PolarClaw
Newbie Poster
7 posts since Jul 2006
Reputation Points: 10
Solved Threads: 0
 

here is my codes for pascal's triangle... but i do not know how to center align the output...

#include <iostream.h>
int main()
{
  int n;
  cin >> n;
  for (int y = 0; y < n; y++)
  {
    int c = 1;
    for (int x = 0; x <= y; x++)
    {
      cout << c << " ";
      c = c * (y - x) / (x + 1);
    }
    cout<<endl;
  }
  cout<<endl;
  return 0;
}
PolarClaw
Newbie Poster
7 posts since Jul 2006
Reputation Points: 10
Solved Threads: 0
 

Think of it not as alligning to the center. Think of it as outputing a certain number of spaces and then the numbers of a line in the triangle.
For example
7 Spaces+1 8 28 56 70 56 28 8 1
4 Spaces + 1 9 36 84 126 126 84 36 9 1
0 Spaces + 1 10 45 120 210 252 210 120 45 10 1
will give you the output something like

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


The best thing to do in my opinion will be to count the length of characters output in the last row, and then use the width method to format the above rows with respect to the last row.

WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 

The following code gives a crude Triangle like output. It gets uglier when the number of line increases.

#include <iostream>
int main()
{
  int n;
  std::cin >> n;
  for (int y = 0; y < n; y++)
  {
    int c = 1;
    std::cout.width(n - y );// Added only this.
    for (int x = 0; x <= y; x++)
    {
      std::cout << c << " ";
      c = c * (y - x) / (x + 1);
    }
    std::cout<<std::endl;
  }
  std::cout<<std::endl;
  return 0;
}
WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 

thanks a lot.. i'll try it.. by the way, what does std mean? i havent encountered that yet...

PolarClaw
Newbie Poster
7 posts since Jul 2006
Reputation Points: 10
Solved Threads: 0
 
thanks a lot.. i'll try it.. by the way, what does std mean? i havent encountered that yet...

cin , cout , endl and the such are members of the std namespace. So in new compilers, you have to tell it to look inside the std namespace, otherwise you will get a compile error. Since it maybe tedious to write std:: all the time, you can use

using namespace std;


after before the main function and you can just type cout , cin , endl . Read this for more information. You can search for " C++ Namespaces std " in google too.

Also in new compilers #include <iostream.h> will give you an error. The correct way is #include <iostream> . Without the .h .

WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 

here is my codes for pascal's triangle... but i do not know how to center align the output...

#include <iostream.h>
int main()
{
  int n;
  cin >> n;
  for (int y = 0; y < n; y++)
  {
    int c = 1;
    for (int x = 0; x <= y; x++)
    {
      cout << c << " ";
      c = c * (y - x) / (x + 1);
    }
    cout<<endl;
  }
  cout<<endl;
  return 0;
}

Can you please explain a little how you derived the formula c = c * (y - x) / (x + 1); ....just to get the idea of what steps to do in order to reach this formula

ramizs
Newbie Poster
1 post since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

>Can you please explain a little how you derived the formula c = c * (y - x) / (x + 1); ....just to get the idea of what steps to do in order to reach this formula

Dude, are we going the reinvent the whole history of maths?
No!

BTW, Do you think that reviving a three year old thread is such a good idea?
I don't think so.
(Just check out this and if you've any more questions, then you should start a new thread about it)

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

>It's a legitimate question. Are you really trying to accomplish something by flaming the guy, or just being an ass?
I didn't want to blame anyone, Narue, but didn't you once write the following?

Search for your answer first!

If you're having a problem, chances are good that someone else has had the same problem. Please search the forum for existing answers before starting a new thread. Nothing is more irritating to a long time member than to answer the same question for the umpteenth time because someone didn't use the forum's search feature.

Don't resurrect old threads!

Inevitably, if you use the search feature you'll stumble upon an interesting thread that you think you can add to. Resist the temptation to post until you've looked at the post date for the most recent post in that thread. It's possible that the thread is years old and you have no business bumping it to the top of the list. A thread is considered old after two months.

Let dead threads lie, don't post in them!


Source: http://www.daniweb.com/forums/thread78223.html
:P

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

I am not good at using TEX, because I have never used it before.

However I would like you all to make an assumption Let Capital 'C' mean or [TEX]C[/TEX] mean the Combinations. Or [TEX](n!)/(n-r)! * r![/TEX]

Where '!' represents the factorial values.

So next we just take the normal

[TEX] nCr = n!/n-r!*r![/TEX]

Then now lets consider nC(r+1),

[TEX]nC(r+1)= n!/(n-(r+1))!*(r+1)![/TEX]

Now We can then derieve:
[TEX]nCr+1=n!/(n-r-1)!*(r+1)![/TEX]

So derieving more out of it. we get

[TEX]=> n!*(n-r)/(n-r)*(r+1)![/TEX]

[TEX]=>n!*(n-r)/(n-r)!*(r)!* (r+1)[/TEX]

[TEX]=>n!/(n-r)!*(r!) * n-r/(r+1)[/TEX]

Which is nothing but

[TEX]=> nCr * n-r/(r+1)[/TEX]

So what we actuall are doing in the code is that considering.

int c=1 /*let c= nC0 ==1*/


But We are using the variables.

x==n in the formula and y==r in the formula.

By that we can just derieve the value.

Because the Pascal triangle is nothing but

The Combinations [TEX]nC0 , nC1 , nC2 ................. nC(r-1), nCr[/TEX]

Sky Diploma
Practically a Posting Shark
865 posts since Mar 2008
Reputation Points: 673
Solved Threads: 131
 

i need to create/have a program/code that display pascal triangle in a simple ouput display.....help me.
plsSs....

asSer
Newbie Poster
3 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 
i need to create/have a program/code that display pascal triangle in a simple ouput display.....help me. plsSs....


Read post no. 11.

Thread closed due to multiple useless resurrections in the last 3 years.

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You