Align Diamond in C++

Hello,
I wrote a program to draw a diamond in C++. I have a problem with the first and second rows. the first and second asterisk isn't aligned.
I upload my output and desired output.
Target:
https://imgur.com/M4fiPg5

My output:
https://imgur.com/Z69LBhq

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

using namespace std;
int main()
{

	int num, i, j, k;
	cout << "Enter a Number :";
	cin >> num;
	num = num - 1;
	for (i = 0; i < num; i++)
	{
		for (j = 0; j < 2*(num - i) ; j++)
		{
			cout <<" ";
		}
		for (k = 0; k < max(2*i,1); k++)
		{
			cout <<" *";
		}
		cout << endl;
	}
	for (i = 0; i <= num; i++)
	{
		for (j = 0; j < 2*i; j++)
		{
			cout << " ";
		}
		for (k = 0; k < max((2 * num - 2 * i),1); k++)
		{
			cout << " *";
		}

		cout << endl;
	}
}
Last edited on
I hate to have to tell you, but if you look very closely your TARGET isn't strictly a diamond when there are even numbers of stars in most rows ... because 1 isn't even.

If you want your designed line-up, then, for an even number, you will have to output something slightly different in the upper half when i=0 and in the lower half when i=num.
Last edited on
@lastchance
Dear friend,
Why do you hate for telling?
Just an English expression, @Shervan360.
@lastchance
Thank you for your answer, but it doesn't work. because of second row has the same problem.
The second row doesn't have any problem.
If you change the first k loop to
1
2
3
4
5
		for (k = 0; k < max(2*i,1); k++)
		{
		    if ( i != 0 ) cout << " ";
			cout <<"*";
		}

and the second k loop to
1
2
3
4
5
		for (k = 0; k < max((2 * num - 2 * i),1); k++)
		{
		    if ( i != num ) cout << " ";
			cout <<"*";
		}

then you will get:
Enter a Number :5
        *
       * *
     * * * *
   * * * * * *
 * * * * * * * *
   * * * * * *
     * * * *
       * *
        *

I reiterate: this is not (quite) a diamond.
Topic archived. No new replies allowed.