Programming a parallelogram in C++

Click For Summary

Discussion Overview

The discussion revolves around programming a parallelogram in C++. Participants are addressing issues with code that currently outputs a triangle instead of the desired parallelogram shape. The conversation includes troubleshooting code, understanding nested loops, and the use of formatting functions.

Discussion Character

  • Technical explanation
  • Homework-related
  • Exploratory

Main Points Raised

  • One participant describes their current output, which is a triangle, and expresses confusion about how to modify their code to achieve a parallelogram.
  • Another participant suggests displaying the top triangle first and then reversing it, while also mentioning the need to offset the second triangle incrementally.
  • A similar suggestion is reiterated by a third participant, emphasizing the use of loops and nested loops to accomplish the task.
  • A later reply indicates that one participant resolved their issue by rewriting the program, suggesting that their initial variable usage was confusing and that they found the hints provided helpful.

Areas of Agreement / Disagreement

Participants generally agree on the need to use loops and nested loops to display the parallelogram, but there is no consensus on the specific implementation details or the effectiveness of the initial code provided.

Contextual Notes

There are unresolved issues regarding the correct implementation of the nested loops and the use of formatting functions like "setw" to achieve the desired output. The initial code contains potential errors that have not been fully addressed.

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;

count << "This program will output a parallelogram." << endl;
count << "How long do you want each side to be? ";
cin >> length;
count << "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++)
{
count << symbol;
}
count << endl;
}
count++;
}


while (count2 < 1)
{

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

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

count << symbol;

}

count << 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
 

Similar threads

Replies
10
Views
2K
  • · Replies 66 ·
3
Replies
66
Views
6K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
Replies
12
Views
2K
Replies
12
Views
3K
  • · Replies 35 ·
2
Replies
35
Views
4K
  • · Replies 75 ·
3
Replies
75
Views
7K