• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 884
  • Last Modified:

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;

}
   

 

 
       

0
edelossantos
Asked:
edelossantos
  • 2
  • 2
1 Solution
 
Jaime OlivaresCommented:
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
 
edelossantosAuthor Commented:
I need to print out an arrow pointing upwards.  
0
 
edelossantosAuthor Commented:
Thanks Jaime...got it.  Happy Thanksgiving.  Del
0
 
Jaime OlivaresCommented:
>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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now