how can i make this star pattern?

I want the code to print this;
1
2
3
4
5
    *
   **
  ***
 ****
*****


I cannot go further than this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;
int main ()
{
	for(int line=1;line<=5;line++)
	{
		for(int star=1;star<=line;++star)
		{
			cout<<"*";
		}
		cout<<endl;
	}
	return 0;
}



How can i re-arrange my code?
Last edited on
You could build upon what you already have.
Let's say that you start with this code:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;
int main ()
{
    for (int line=1;line<=5;line++)
    {

        cout<<endl;
    }
    return 0;
}

and that you had to output this pattern:
----
---
--
-



If you can solve that, then you should be able to merge the two to give this:
----*
---**
--***
-****
*****


(I've used the '-' character rather than a space, for clarity, but the code would be the same apart from the character used).


Of course there are always many ways to solve questions like this, there isn't one single 'right' answer.
In C++ to show star it is much difficult
Difficult? Learning something new is always a challenge.
However the problem can be solved in just two lines of code. (Plus the usual headers etc.).
This is as far as i can go using your hints
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;
int main ()
{
	for(int line=5;line>=1;line--)
	{
		for (int starr=line;starr>=1;starr--)
		{
			cout<<" ";
		}
		for(int star=0;star<1;star++)
		{
		    for(int lines=0;lines<=star;lines++)
		    {		
			    cout<<"*";
		    }
		}
		cout<<endl;		
	}
	return 0;
}
Last edited on
That is progress. Having one loop count up, and the other count down (using starr-- and star++) was roughly what I had in mind. Now all you need to do is adjust the start value. Instead of counting down from line for (int starr=line try something like for (int starr=5-line. It may need a little adjustment, but that should help you head in the right direction.
for (int starr=5-line
can you explain the logic behind this please? We can also right
(int starr=0
If we go back to the original pattern,
    *
   **
  ***
 ****
*****

line 1, 4 spaces, 1 star.
line 3, 2 spaces, 3 stars.
line 5, 0 spaces, 5 stars.
The total number of characters per line always adds up to 5.
Thus the number of spaces required is (5 - number of stars)

Do it any way you like, so long as the total characters per line adds up to five.
The code above displayed this;
    *
   *
  *
 *

You probably know that.

I had been working on this problem for 8 hours, this problem drained the juices out of my brain. So, I wrote the entire code again

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;
int main ()
{
	for(int space=5;space>=1;space--)
	{
		for(int spaces=1;spaces<=space;spaces++)
		{
			cout<<" ";
		}
		for(int star=1;star<5;star++)
		{
			for(int stars=1;stars<star;stars++)
			{
				cout<<"*";
			}
		}
		cout<<endl;		
	}
	return 0;
}


The output im getting now is;
    ******
   ******
  ******
 ******
******

Im getting close, i am aware of that. But i just can't figure out why it is displaying six stars in each line.
I understand, solving any problem can at times have one chasing round in circles and getting exhausted at the end. Hopefully you took a break and had some sleep. That usually helps.

Also I need to apologise, I may not have been thinking clearly myself yesterday. Some of the hints or suggestions I gave may have been wrong.

As for the latest code. Firstly, it looks over-complicated. Using a pair of nested for loops to output the stars seems more complexity than is needed. Just a single for loop should be enough.

While we're here. why are there six stars? Well, the nested loop does this:
star=1
star=2 *
star=3 **
star=4 ***
that is, 1+2+3 = 6.
Enough of that. Just use one loop.

The second thought is, the number of stars printed on each line needs to be different each time, it depends on the line number (I see in the latest code, the line number is renamed as int space). The number of stars printed needs to be related to this, I suggest number of stars per line = 5 - space.
Done!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;
int main ()
{
	for(int rows=1;rows<=5;rows++)
	{
		for(int space=1;space<=5-rows;space++)
		{
			cout<<" ";
		}
		for(int star=1;star<=rows;star++)
		{
			cout<<"*";
		}
		cout<<endl;		
	}
	return 0;
}


Can you give your code? Earlier you said that this code can be written in 2 lines
Well done!

My version, here it is, making use of some C++ features which you may not have used yet, but they are fairly ordinary ones which you will use yourself at some stage.
Of course it is more than two lines with all the necessary stuff there.
1
2
3
4
5
6
7
8
#include <iostream>
#include <string>
#include <iomanip>
int main ()
{
    for (int line=1; line<=5; line++)
        std::cout << std::setw(5) << std::string(line, '*') << '\n';
}


Last edited on
After I made the pattern above, I decided to make a few changes in this program. i want to make this;
1 1 1 1 *
1 1 1 * 1
1 1 * 1 1
1 * 1 1 1
* 1 1 1 1

Its quite obvious that we have to make a few re-arrangements in the code above;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;
int main()
{
	for(int rows=1;rows<=5;rows++)
	{
		for(int num1=1;num1<=5-rows;num1++)
		{
			cout<<"1";
		}
		for(int star=rows;star<=rows;star++)
		{
			cout<<"*";
		}
		for(int num2=1;num2<=rows;num2++)
		{
			cout<<"1";
		}
		cout<<endl;
	}
	return 0;
}

I got this far. There's just one problem. It prints out an extra column of "1" at the end. It should not do that. I just can't figure out the proper condition we have to use in the 3rd Inner Loop.
1 1 1 1 * 1
1 1 1 * 1 1
1 1 * 1 1 1
1 * 1 1 1 1
* 1 1 1 1 1

This is the output I'm getting
Last edited on
That shouldn't be too hard to solve, you just need the loop at line 15 to repeat by one less. Perhaps change the "<=" to just "<".


But here's another challenge for you. Try to do the same pattern using just two for loops and one if statement. (that is, rather than the current four loops, just use two).
Last edited on
Topic archived. No new replies allowed.