C/C++ Programming a parallelogram in C++

AI Thread Summary
The discussion revolves around difficulties in programming a parallelogram display using C++. The original code successfully displays a triangle but fails to produce the desired parallelogram shape. The user initially attempted to create two separate triangles, with the top triangle displaying correctly. The challenge lies in formatting the bottom triangle to align properly with the top one, which requires adjusting the output using the "setw" function to create the necessary indentation.The user experimented with various placements of "setw" within nested loops but did not achieve the desired output. Ultimately, the solution was found by rewriting the program, simplifying variable usage, and correctly implementing loops to achieve the parallelogram shape. The successful output aligns with the specified format, confirming that proper use of loops and indentation is crucial for achieving the desired visual result in console applications.
teknodude
Messages
150
Reaction score
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
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.
 
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.
 
Yes, of course.
 
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
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top