I need help ... hours of frustration

I need help with a program that will produce an output like this ...


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

PLZ HELP ME
PLZ HELP ME

What have you done so far?
Last edited on
I did this pattern with just 2 for loops and some if-statements in between. The entire function is about 15 lines of code only. I'd be happy to help out if you show your attempt.
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() 
{ 
   int i = 1, j = 9;
   while ( i * j ) cout << string( min(i++,j--), '*' ) << endl;
}

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int main(){
	std::cout << 
R"(*
**
***
****
*****
****
***
**
*)";
}
I have this but i keep getting a normal triangle ...

#include <iostream>
using namespace std;

int main()
{
int rows;

cout << "Enter number of rows: ";
cin >> rows;

for(int i = 1; i <= rows; ++i)
{
for(int j = 1; j <= i; ++j)
{
cout << "* ";
}
cout << "\n";
}
return 0;
}
@LilXeno, that's a good start.

Your inner j loop puts out enough stars to give a row of length i (which is correct).

Your outer i loop runs from 1 to rows, increasing by 1 each time (also correct).

This gives the upper part of the figure (correct).


Now, all you need to do is repeat with a similar nesting to get the lower part. The inner loop (on j) won't change. What matters is the length of the row, which is set by the i loop.
This time you will START at i=rows-1 (not 1).
This time you will END at i=1 (not rows). Remember also that you will be looping whilst >=, not <=.
This time you will decrement i each time: simply use --i, rather than ++i.
Can you see how to change the for loop involving i so as to do this?


Could you possibly use code tags on your snippets of code, please. It's the first item in the format menu to the right; i.e. select your code and press the <> button.
Last edited on
Hi, I remember having to do these applications in HighSchool when I started taking C++ courses, they can be a little frustrating but you'll grow to like them soon hopefully. Anyway, here's how I created the pattern:

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
#include <iostream>

int main()
{
	int h = 0;

	for (int i = -5; i <= 5; ++i)  
	{
		h = i;

		if (h < 0)
			h *= -1;  

		for (int j = 0; j <= 5; ++j)
		{
			if (h <= j)
				std::cout << "* "; 
		}
		std::cout << "\n";
	}

	int iTemp = 0;
	std::cin >> iTemp;
	return 0;
}


I am using only two for loops to create your star pattern. The first for-loop handles the number of rows. The inner for-loop handles the columns. I am using a small logic with variable h to accomplish the pattern.
Last edited on
@Uk Marine

Wait ... the program has to allow the user to input the number of stars on the outer row that they want. So what would I do then?
Like I said, the first for-loop handles the number of rows while the inner for-loop handles the number of columns. Simply change the value 5 to whatever you want.

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
int main()
{
	int h = 0;
        int number = 0;
        std::cin >> number;

	for (int i = -number; i <= number; ++i)  
	{
		h = i;

		if (h < 0)
			h *= -1;  

		for (int j = 0; j <= number; ++j)
		{
			if (h <= j)
				std::cout << "* "; 
		}
		std::cout << "\n";
	}

	int iTemp = 0;
	std::cin >> iTemp;
	return 0;
}


EDIT: I modified it for you above ^
Last edited on
Topic archived. No new replies allowed.