Objective: To output a parallelogram of the specified size and makeup. >>>>>USE
ID: 3684183 • Letter: O
Question
Objective: To output a parallelogram of the specified size and makeup. >>>>>USE C++<<<<<<
Write a program (using C++) that draws parallelograms with all sides of equal length, as shown below. The user input is shown in bold.
$ a.out
This program will output a parallelogram.
How long do you want each side to be? 6
Please enter the character you want it to be made of: @
@
@@
@@@
@@@@
@@@@@
@@@@@@
@@@@@
@@@@
@@@
@@
@
$ a.out
This program will output a parallelogram.
How long do you want each side to be? 9
Please enter the character you want it to be made of: *
*
**
***
****
*****
******
*******
********
*********
********
*******
******
*****
****
***
**
*
Requirements and Hints
Program must use at least one for-loop.
Program must output the parallelogram just one character at a time, inside a loop. (You will need nested loops)
Program must work with any length greater than 1. (You don't need to do any error checking for bad input)
Program must use the character the user inputs to draw the parallelogram.
Hint #1: I recommend you think of this as a program to output two triangles, one above the other (the longest horizontal line is the border between the triangles).
Hint #2: To make this easier to solve, start by writing a program that outputs this text for the last example above:
1*
2*
3*
4*
(etc.)
After that is done, then figure out how to output the right number of characters.
Follow the standard conventions for indentation, meaningful variable names, etc.
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
int i,j,side_length;
char character;
cout<<"This program will output a parallelogram. ";
cout<<"How long do you want each side to be? ";
cin>>side_length;
cout<<"Please enter the character you want it to be made of: ";
cin>>character;
for(i=1;i<=side_length;i++)
{
for(j=1;j<=i;j++)
cout<<character;
cout<<endl;
}
for(i=side_length-1;i>=1;i--)
{
for(j=1;j<=i;j++)
cout<<character;
cout<<endl;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.