Spacing the output of Pascal's Triangle

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • harishram007
    New Member
    • Jul 2007
    • 4

    Spacing the output of Pascal's Triangle

    Sorry for the inconvienience but i am expecting this pattern the pattern is in the attachment....i am just pasted my code and want alterations.... please help
    but i am ending up wid something like

    1
    1 1
    1 2 1
    1 3 3 1
    with the following code
    [code=cpp]
    #include <iostream.h>
    void main()
    {
    int n,y,x,c;
    cin >> n;
    for (y = 0; y < n; y++)
    {
    c = 1;
    for (x = 0; x <= y; x++)
    {
    cout<<" "<<c<< " ";
    c = c * (y - x) / (x + 1);
    }
    cout<<endl;
    }
    cout<<endl;
    }
    [/code]
    so please can someone help me out in spacing!
    Attached Files
    Last edited by sicarie; Jul 23 '07, 03:06 PM. Reason: Code tags, title, etc...
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by harishram007
    Sorry for the inconvienience but i am expecting this pattern
    1
    1 1
    1 2 1
    1 3 3 1
    but i am ending up wid something like

    1
    1 1
    1 2 1
    1 3 3 1
    with the following code
    which looks good to me (they're identical, so what's the problem?)

    kind regards,

    Jos

    Comment

    • sicarie
      Recognized Expert Specialist
      • Nov 2006
      • 4677

      #3
      Please read these thoroughly.

      Thanks

      Update:: You have already been asked to read the Posting Guidelines throughly, so if you violate them again, I'm afraid I will have to issue you a temporary ban. If you want to post on the site, you have to follow the rules.
      Last edited by sicarie; Jul 23 '07, 03:01 PM. Reason: You've already been warned

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        Check out the setw() command.
        Last edited by sicarie; Jul 23 '07, 03:09 PM. Reason: Made it a link

        Comment

        • harishram007
          New Member
          • Jul 2007
          • 4

          #5
          but where do i place the setw()

          Comment

          • sicarie
            Recognized Expert Specialist
            • Nov 2006
            • 4677

            #6
            Originally posted by harishram007
            but where do i place the setw()
            I'd start by putting it in front of your first 1. You're going to have to play around with it and figure it out for yourself. This might require a variable to keep track of what level of the triangle you are at, and probably figuring out a spacing in between each of the numbers on a line...

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Originally posted by sicarie
              I'd start by putting it in front of your first 1. You're going to have to play around with it and figure it out for yourself. This might require a variable to keep track of what level of the triangle you are at, and probably figuring out a spacing in between each of the numbers on a line...
              What sicarie says will work. I suggest you write your display down on paper so you can count the spaces you need for each line of Pascal's Triangle.

              Comment

              • bitgeek
                New Member
                • Jul 2008
                • 1

                #8
                try this code:

                Code:
                #include <iostream>
                #include <iomanip>
                using namespace std;
                int main()
                {
                  double n;
                  cout << "Number: ";
                  cin >> n;
                  cin.ignore();
                  for (int y = 0; y < n; y++)
                  {
                    int c = 1;
                    cout << setw(n-y);
                
                    for (int x = 0; x <= y; x++)
                    {
                      cout  << c << " ";
                      c = c * (y - x) / (x + 1);
                
                    }
                    cout << "\n";
                  }
                  cin.get();
                  }

                Comment

                • Banfa
                  Recognized Expert Expert
                  • Feb 2006
                  • 9067

                  #9
                  Actually the problem is not quite as simple as it seems, the amount of spcing required is not only dependent on the current output line but the total number of lines that will be output.

                  For example it is fairly easy to see that the large the number of lines output the further over the 1 on the first line will have to be to leave room for the data on the following lines.

                  Comment

                  Working...