hey guys can anyone plz xplain to me how can v make a hexagon using only for loops?i just cant figure out how to do it :(

Comments
kiddy speak, and no effort shown.

This should give ye' at least an idea. I'll draw half of the hexagon, ye' can do the rest. This is untested, but feel free to tweak it as necessary to draw that perfect hexagon:

#include<iostream>
#include<iomanip>

void draw_hex()
{
     //Draw Top
     cout << setw(10);
     for(int i=0; i<5; i++)
     {
          cout << '*';
     }
     cout << endl;

     //Draw Top Sides
     int left_offset = 10, right_offset = 5;
     for(int i=0; i<5; i++)
     {
          cout << setw(--left_offset) << '*' << setw(right_offset+=2) << '*' << endl;
     }

     //Draw Bottom Sides
     ...
     ...
     ...
     //Draw Bottom
     ...
     ...
     ...
}

Edited 7 Years Ago by Clinton Portis: n/a

oh sorry i forgot to mention...v r only to use for loops and ony iostream.h

sorry i didnt mention it before

I would then suggest maybe trying a char hex[20][20] array. (This will be a char[row][column] 20x20 square array).

Initialize all the array elements to a white space:

for(int i=0; i<20; i++)
     for(int j=0; j<20; j++)
     {
          hex[i][j] = ' ';
      }

Now I would suggest using a piece of graph paper and figure out what elements ye' would like to populate to draw a hexagon.

hex[3][10] = '*';
hex[4][11] = '*';
hex[5][12] = '*';
hex[6][11] = '*';
...
...
etc etc.

Now you can draw your entire hexagon with one nested for loop:

for(int i=0; i<20; i++)
{
     for(int j=0; j<20; j++)
     {
          cout << hex[i][j];
     }
    
     cout << endl;
}

Edited 7 Years Ago by Clinton Portis: n/a

This article has been dead for over six months. Start a new discussion instead.