Programming a parallelogram in C++

In summary, the program displays a triangle, but the bottom triangle does not look like the sample shape displayed. The program seems to require that the side lengths be specified in increments of one.
  • #1
teknodude
157
0
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:
Technology news on Phys.org
  • #2
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.
 
  • #3
dduardo said:
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.

Does this involve loops and nested loops, because I'm suppose to use those to display the parallelogram.
 
  • #4
Yes, of course.
 
  • #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
 

1. How do I declare a parallelogram in C++?

In C++, you can declare a parallelogram by first defining a struct with four variables for the coordinates of the four vertices. Then, you can use the struct to create an object for your parallelogram.

2. How do I calculate the area of a parallelogram in C++?

To calculate the area of a parallelogram in C++, you can use the formula A = b * h, where b is the base and h is the height. You can also create a function that takes in the coordinates of the vertices as parameters and returns the area of the parallelogram.

3. How can I check if a parallelogram is a rectangle or a rhombus in C++?

In C++, you can check if a parallelogram is a rectangle by comparing the lengths of its opposite sides. If they are equal, then it is a rectangle. For a rhombus, you can check if all four sides are equal in length.

4. How can I draw a parallelogram on the screen using C++?

In C++, you can use a graphics library such as OpenGL or SDL to draw a parallelogram on the screen. You would need to define the coordinates of the vertices and use the library's functions to draw the lines and fill in the shape.

5. Can I use C++ to animate a parallelogram?

Yes, you can use C++ to animate a parallelogram by constantly updating the coordinates of the vertices and redrawing the shape on the screen. You can also use libraries like OpenGL or SDL to create more complex animations.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
Replies
10
Views
955
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
5
Views
879
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
4
Views
5K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
2
Replies
35
Views
2K
Back
Top