Help with Diamond * pattern

Hi guys
So i am brand new to programming. I have just started self learning about 4 days ago,
so far I've only learn't variables, input/output, conditional statements and loops. at the moment I'm stuck on a for loop problem;

"Write a program to print the diamond star(*) pattern based on an input number."

In my code so far without really knowing what I'm doing i tried to reverse what i did in my first for loop but it's not quite working.

I would appreciate if somebody could explain where I'm going wrong.

thanks guys


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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  // Write a program to print the diamond star(*) pattern based on an input number. 

#include <iostream>
using namespace std;

int main()
{
	int number = 0;
	cout << "Please enter a number: ";
	cin >> number;

	for (int i = 0; i < number; i++)
	{
		// prints space
		for (int j = 0; j < (number - i - 1); j++)
		{
			cout << " ";
		}

		// prints stars
		for (int j = 0; j < (2 * i + 1); j++)
		{
			cout << "*";
		}

		cout << endl;
	}

	// trying to reverse for loop here !
	for (int i = number ; i >= 0; i--)
	{

		for (int j = 0; j < (number - i - 1); j++)
		{
			cout << " ";
		}

		for (int j = 0; j < (2 * i + 1); j++)
		{
			cout << "*";
		}

		cout << endl;
	}

	return 0;
}
Last edited on
Is this what you expect to see? I tweaked the terminating condition of both inside for loops (line 33 and 38).

1
2
3
4
5
6
7
8
9
10
11
Please enter a number: 5
    *
   ***
  *****
 *******
*********
*********
 *******
  *****
   ***
    *
Yeah that's the one :).
Yeah i was looking at those conditions but at the moment i seem to not be understanding nested for loops very well.
I'll keep having a little play around tomorrow, i can't think properly at the moment, my brain is fried but from what i hear i'll be feeling like this a lot.

thanks for the reply and the advice wildblue
Topic archived. No new replies allowed.