Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Programming a parallelogram in C++

  1. Jun 26, 2005 #1
    I'm having troube getting my program to display a parallelogram. The source code here just displays a triangle with "6" as the speicifed size.
    *
    **
    ***
    ****
    *****
    ******
    *****
    ****
    ***
    **
    *

    The output is suppose to be something like this:
    _____*
    _____**
    _____***
    _____****
    _____*****
    _____******
    ______*****
    _______****
    ________***
    _________**
    __________*

    Ignore the straightlines on the left. I couldn't get this post to display right when i just copied and pasted the parallelogram figure.


    I started out programming 2 separate triangles. The top triangle displayed perfectly, but I can't get the bottom one to look like the sample shape displayed above. i'm thinking that i need to add "setw" somewhere in the 2nd set of nested loops to shift the rows of symbols over. I've tried adding setw to different parts the nested loop, but nothing works.


    #include <iostream>
    using namespace std;
    int main()
    {

    int length;
    char symbol;

    cout << "This program will output a parallelogram." << endl;
    cout << "How long do you want each side to be? ";
    cin >> length;
    cout << "Please enter the character you want it to be made of: ";
    cin >> symbol;


    int count = 0;
    int count2 = 0;



    while ( count < 1)
    {
    for (int col = 0; col < length; col++)
    {
    for (int row = 0; row <= col; row++)
    {
    cout << symbol;
    }
    cout << endl;
    }
    count++;
    }


    while (count2 < 1)
    {

    for (int row2 = 1; length >= row2; length--)
    {

    for (int col2 = 1; length > col2; col2++)
    {

    cout << symbol;

    }

    cout << endl;

    }

    count2++;
    }

    return 0;
    }
     
    Last edited: Jun 26, 2005
  2. jcsd
  3. Jun 26, 2005 #2

    dduardo

    User Avatar
    Staff Emeritus

    1) Display the top triangle. First line gets 1 star, second line gets 2 stars, etc until you get nth line which is your side length.
    2) Do the same thing as part one, but in reverse starting with the 1 minus the number of stars of the maximum line. Remember to offset this triange incrementally by one.
     
  4. Jun 26, 2005 #3
    Does this involve loops and nested loops, because i'm suppose to use those to display the parallelogram.
     
  5. Jun 26, 2005 #4

    dduardo

    User Avatar
    Staff Emeritus

    Yes, of course.
     
  6. Jun 26, 2005 #5
    Finally got it working. I just retyped the whole the program again, the way i used my variables in the beginning was just too confusing. hint #2 was just what i needed to get it working. Thanks man
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Programming a parallelogram in C++
  1. C-Programming Help (Replies: 6)

  2. Loops in C-Programming (Replies: 15)

  3. C++ program help (Replies: 2)

Loading...