Solved

# Shapes Generator

Posted on 2004-11-19
881 Views
// This code is giving an infinite loop via the arrow code.

// Secondly, is there a nested for loop algorithm that will print out an oval shape?

********************Actual Code***********************************

// diamond.cpp

#include <iostream.h>

using namespace std;

const int SIZE = 9;

void draw_line(char, int);
void draw_box(int, int, int, char);
void draw_tip(int, char);

int main(int argc, char *argv[]) {

int a, b, c, i, j, x, y;

char symbol;
int tail_width, tail_length;
int indent;
int tip_height, tip_width;
draw_tip(tip_height, symbol);
tip_width = 2 *tip_height - 1;
indent = (tip_width - tail_width) / 2;
draw_box(indent, tail_length, tail_width, symbol);

cout << '\n' << "Shapes Generator " << endl << endl;

// right triangle nested for loops

for(i = 1; i <= SIZE; i++) {
for(j = 0; j < i; j++) cout << "*";
cout << endl;

}

cout << '\n';

// box nested for loops

for(x = 1; x <= SIZE; x++) {
for(y = 0; y < i; y++)
cout << "*";
cout << endl;

}

cout << '\n';

// diamond nested for loops

for(a = 1; a < 6; a++) {
for(b = a; b < 5; b++) {
cout << ' ';
}

for(c = 1; c < (2 *a); c++) {
cout << "*";
}
cout << endl;

}

// reversal

for(a = 4; a > 0; a--) {
for(b = a; b < 5; b++) {
cout << ' ';
}

for(c = 1; c < (2 *a); c++) {
cout << "*";
}
cout << endl;

}

cout << '\n';

// oval

cout << "   ***    " << '\n'
<< "  *****   " << '\n'
<< " *******  " << '\n'
<< "********* " << '\n'
<< "********* " << '\n'
<< " *******  " << '\n'
<< "  *****   " << '\n'
<< "   ***    " << '\n' << endl;

cout << "Enter the tip height: " << flush;
cin >> tip_height;

cout << "Enter the tail width: " << flush;
cin >> tail_width;

cout << "Enter the tail length: " << flush;
cin >> tail_length;

cout << "Enter the character to use: " << flush;
cin >> symbol;

cout << endl << '\t' << "Enrique De Los Santos." << endl << endl;

return 0;

}

void draw_line(char character, int how_many) {

for(int d = 1; d <= how_many; d++)
cout << character << endl;

return;

}

void draw_box(int indent, int box_length, int box_width, char symbol) {

for(int e = 1; e <= box_length; e++) {
draw_line(' ', indent);
draw_line(symbol, box_width);
cout << endl;

}
return;

}

void draw_tip(int tip_height, char symbol) {

for(int f = 1; f <= tip_height; f++) {
draw_line(' ', tip_height - f);
draw_line(symbol, 2 *f - 1);
cout << '\n';
}
return;

}

0
Question by:edelossantos

LVL 55

Accepted Solution

what do you mean with arrow code?

About the oval, here is a first aproach, the rest is yours (you will need to include <math.h>):

int h = 10;
for(a = 0; a < h; a++) {
int w = (int)(sin(3.14/h*a)*h*2);

for(b = h-w/2; b; b--) {
cout << ' ';
}

for(c = 0; c<w; c++) {
cout << "*";
}
cout << endl;
}
0

Author Comment

I need to print out an arrow pointing upwards.
0

Author Comment

Thanks Jaime...got it.  Happy Thanksgiving.  Del
0

LVL 55

Expert Comment

>Thanks Jaime...got it
Thanks, I was a little bussy. I don't know if you have solved the arrow issue. The problem with this shape is that it has many kind of "white holes", so best approach could be to use a buffer and put proper characteres row by row, something like this:

char buffer[12];  // some arbitrary size

for(a = 0; a < 8; a++) {
strcpy(buffer, "           ");  // clear row each time

buffer[5] = '*';     // central column
if (a>0 && a<5) {   // draw wings
buffer[5-a] = buffer[5+a] = '*';
}
cout << buffer << endl;
}

>Happy Thanksgiving
Sorry, I am not from US, and don't like turkey, but thanks anyway ;-)
0

## Featured Post

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.