# Nested for loops

• 02-12-2002
Unregistered
Nested for loops

I've researched every textbook, visited at least 25 sites, looked at more tutorials than I can remember and I still can't solve this problem.

It's a easy problem as I'm only a newbie but please help me.... I've have been racking my brains for over a week now...

Here it is.
I'm trying to display this pattern with using just nested for loops but I don't know how...

*****
****
***
**
*

This is what I've got so far but that's it. I'm stuck!!!

#include <iostream.h>

main()
{
for (int a = 1; a <= 5; a++) //First loop. The condition is
//true so it will continue to next loop
{
for (int b = 5; b >= a; b--) //Second loop. Also true until b is
//zero
{
cout << "*";
for (int c = b; c <= a; c++) //This is where I'm stuck!!!
//HELP. HELP.
{
cout << " ";
}
}
cout << endl;
}
}

What should I do? Is the third loop required? Do I need another loop?

Please if anyone could help me with this I will be eternally grateful :)
• 02-12-2002
Unregistered
The correct pattern I'm trying to get is

*****
_****
__***
___**
____*

The "_" are empty spaces...
• 02-12-2002
samGwilliam
Code:

```#include <iostream.h> main () {         int i, j;         for (i = 0; i < 5; i++)         {                 // Spaces.                 for (j = 0; j < i; j++)                         cout << " ";                 // Stars.                 for (j = 0; j < (5 - i); j++)                         cout << "*";                 cout << endl;         } }```
• 02-12-2002
Govtcheez
> for (j = 0; j < (5 - i); j++)
> cout << "*";

Ewww....

for (j = 5; j > i; j--)
cout << "*";
• 02-12-2002
samGwilliam
Whatever.