Star Program

Hi guys,
I'm new to this so please be patient... I need to create a program in C++ that shows a pattern of 1 star followed by 2 stars followed by 3 stars, and then back to two and then one:

*
**
***
**
*

I need to do this using loops. So far I've learned a little bit about while, do-while, and for loops. I'm having a hard time understanding how loops work to make this pattern. Any help would be greatly appreciated. I don't know how long it takes for you guys to respond to this, but I'm hoping to get this done by Wednesday.
For loops work like this:
for(counter/s, test, action)
{
blook of code to do
}

You can have 1 or more counters declared, se parated by a "," .Also you can have 1 or more tests, you just put all the test in a parenthesis. and the action is usualy an incrementat or decrementat of the counters.
For exemple

1
2
3
4
for(int i = 0; i < 10; i++)//i is the counter, i < 10 is the test, and i++ is the action
{
	cout << i << "\n"; // and this is the block of code that is executed each iteration
}


You can go the other way around to like so
1
2
3
4
for(int i = 10; i > 0; i++)
{
	cout << i << "\n";
}


Now, for your need i sugest using an integer to hold the max number of stars to be printed and to use 2 nested loops.The first loop counts the number of rows(you need to print a maxim of 5 stars in a row, you need 5 rows) and another loop inside it that prints the stars on the row. You can use the i for the outer for loop as a counter for the number of stars to be printed.
Here is an exemple that only goes in one way. For printing in the decreasing order you need to the counting the other way around, you start with the high values - 1 and you decrease them till you reach 0;

1
2
3
4
5
6
7
8
for(int i = 1; i <= 5; i++)
{
	for(int j = 0; j < i;j++)
	{
		cout << "*";
	}
	cout << endl;
}


This will print
1
2
3
4
5
*
**
***
****
*****

Topic archived. No new replies allowed.