Link to home
Start Free TrialLog in
Avatar of edelossantos
edelossantos

asked on

Shapes Generator

// 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;

}
   

 

 
       

ASKER CERTIFIED SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of edelossantos
edelossantos

ASKER

I need to print out an arrow pointing upwards.  
Thanks Jaime...got it.  Happy Thanksgiving.  Del
>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 ;-)