Not Yet Answered # nested for loop

Rahul47 13 Gonbe 32 OK, so HostGator for some reason no longer allows gcc/g++ access unless you have a Designated Server account, which is a lot of money to spend just to compile my "Hello World" program. Thus I figured I'd compile at home, then upload. Program is your regular old bare-bones Hello World ...

0

1) Make an outer for loop for line no.

2) Make inner for loop for no of spaces and then stars.

Go ahead code this and post agin if you have problem.

0

Right, so you have this figure of a certain height. When looking at the provided example you can clearly see there's some logic behind it; when asked to draw this figure of height 5 you'd be able to do it.

So let's start with gathering information. We know how the figure grows, and because of this we can also determine how wide it will become for a given height. If we state a height of `0`

it's easy, the width would also be `0`

. If we define a height `> 0`

the width would be equal to `(2 * height) - 1`

. Why? Every line makes the figure 2 symbols wider except for the very first line which has a width of 1.

So we now know how high and how wide the figure will be. That seems like something we could code using a nestes for loop:

```
const unsigned int WIDTH = (2 * height) - 1;
for (unsigned int row = 0; row < height; row++)
{
for (unsigned int column = 0; column < WIDTH; column++)
{
// Something should go here!
}
cout << endl;
}
```

So this would go through every row/column combination until we've gone over the entire figure. We haven't defined how we should determine which symbol (`*`

or ) goes where though. As with the previous there's multiple ways to do this, but seeing the figure you can see the figure is mirrored through the middle: If you'd split it in half vertically the `*`

symbols on each line have an equal distance to the middle. This seems like something we could do as well.

We already defined the width of the figure, so determining the middle of it is easy. It's simply the width divided in half. The width could be an uneven number, in which case we might have to think about rounding it up or down. It's down in the case because we start counting at zero while the width and height is counted starting at 1.

So how far should the `*`

symbol be from the middle for each row? We start counting rows at 0. For the first row the `*`

should be exactly on the middle. So that would simply be the `middle`

column. For the other lines it should be plus or minus the row index away from the middle.

This results in something similar to this:

```
#include <iostream>
using namespace std;
void print_figure(const unsigned int height)
{
const unsigned int WIDTH = (2 * height) - 1;
const unsigned int MID = WIDTH / 2;
for (unsigned int row = 0; row < height; row++)
{
for (unsigned int column = 0; column < WIDTH; column++)
{
if (column == MID + row || column == MID - row)
cout << "*";
else
cout << " ";
}
cout << endl;
}
}
int main()
{
print_figure(10);
return 0;
}
```

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

Recommended Articles

I don’t want at this stage work on a big separate project as I've already got plenty ...

Hi. I have a form with list box : lst_product, datagridview : grd_order and button: btn_addline. lst_product has a list of product ids selected from database (MS Acess 2013) , grd_order is by default empty except for 2 headers and btn_addline adds rows to grd_order.

btn_addline :

`Private Sub btn_addline_Click(ByVal ...`