| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
|
|
#1
|
|||
|
|||
|
Creating a rectangle / triangle with asterisks
Hi, i'm new to C++ and I just started my first course on it. I'm having some trouble with a program i'm supposed to make. The program would ask for width / length for rectangle, and then print the shape of the rectangle with asterisks (*), and display the area and perimeter. Heres what i have so far, but i dont know where to even start with creating the rectangle. I'm supposed to use a nested loop to create the rectangle.
int main() { //declare variables int length = 0; int width = 0; int perimeter = 0; double area = 0.0; //inputs cout << "Enter length: "; cin >> length; cout << "Enter width: "; cin >> width; //calculate area and perimeter perimeter = (length * 2) + (width * 2); area = length * width; //output rectangle //output area and perimeter cout << "Area: " << area << endl; cout << "Perimeter: " << perimeter << endl; return 0; } //end of main function Any help would be appreciated. |
|
#2
|
|||
|
|||
|
okay after some work ive finally come up with this for printing the rectangle:
for (int row=0; row<length; row++) { for (int col=0; col<width; col++) { cout << "*"; } cout << endl; } However, this prints just a full rectangle of the stars, i would like to print the rectangle, but have the center be spaces, not just asterisks. I'm assuming i need to use if statements to do so, but can anybody help me? |
|
#3
|
|||
|
|||
|
Basically, except the first line and the last line, you need to begin and end with '*' else use ' '.
Pseudocode: Code:
if(rows>0) for all columns output '*'
for (all other rows before rows-1) {
if (columns>0) output '*'
for (all othercolumns before columns-1) draw ' '
if (columns>1) output '*'
}
if (rows>1) for all columns output '*'
If you can assume at least 2x2, then you can remove all if(...) statements. Triangles will be a good bit harder, depending on the requirements. |
|
#4
|
|||
|
|||
|
just come to have a look
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Creating a rectangle / triangle with asterisks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|