Nested Loop

I need help creating a nested loop (it can be a while loop or a for loop) that results in this paturn:

□□□□□*
□□□□**
□□□***
□□****
□*****
******
What code do you have at the moment.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    int s = 0;
    for (int i = 1; i <= 6; i++)
    {
        for (int j = 0; j < s + i; j++)
        {
            cout << "*";
        }
        s += i;
        cout << left;
        cout << "\n";
    }
    return 0;
}
Thats something different.

Ok. lets analyze what you want.

- Each line will have same number of characters, lets say N.
- for each line the loop would go from 1 - N
- print count would start from N-1 for boxes and 1 for a *
- for each line, the count of boxes will keep on decreasing and the same would be added to the print count of *. Eg. line 1 - 5 boxes, 1 star; line 2 - 4 boxes, 2 stars etc..

Do you think, you would be able to do it now ?
Is this it?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    int s = 0;
    for (int i = 1; i <= 6; i++)
    {
        for (int j = 0; j < s + i; j++)
        {
            cout << "*";
        }
        s += i;
        cout << left;
        cout << "\n";
    }
    return 0;
}

Because I'm still not getting what I need, also the boxes represents space.
Please I need help, it's due tomorrow.
Declare a few more variables so you can see exactly what you need, then focus on making it leaner and more clever later. Notice how the variable names make it easy to spot where you must figure out the logic for your problem.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;

int main()
{
    int numberOfSpaces, numberOfStars;

    for (int i = 1; i <= 6; i++)
    {
        //these are going to somehow be based on i (your loop counting variable)
        numberOfSpaces = ?;
        numberOfStars = ?;

        for (int j = 0; j < numberOfSpaces; j++) //print the spaces
        {
            cout << " ";
        }
        for (int j = 0; j < numberOfStars; j++) //print the stars
        {
            cout << "*";
        }

        cout << "\n";
    }
    return 0;
}
Last edited on
Never mind, thank you!
Last edited on
Make sure you didn't accidentally forget to close your executable since the last time you ran it (i.e. make sure none of your console windows from your previous runs are still open).
closed account (3CXz8vqX)
writeonsharma's solution is about the best you're going to get. We're not here to solve your assignments we're here to assist with concepts that you don't get.


But since I'm generous....

Public Domain Release.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//Written by Ravenshade to be distributed to public domain. Use how you wish. 
//Solves a common nested forloop problem. 

#include <iostream>

using std::cout;

int main()
{
    int total_space = 6;//Not needed, can be deleted. 

    for(int rows = 0; rows < 6; rows++)
    {
        for(int spaces = 5; spaces - rows > 0; spaces--)
        {
            cout << "_";
        }
        for(int stars = 5; stars + rows > 5; stars--)
        {
            cout << "*";
        }

        cout << "\n";
    }
    return 0;
}


No idea why you needed iomanip though? It's solvable with just loops. ^_^ (I am so pleased with myself for solving this one. )
Last edited on
Topic archived. No new replies allowed.