My asterisk diamond...

I have been tasked with making a diamond out of asterisks based on a given odd integer input. For some reason the bottom half of my diamond will not print. I'm not sure as to why.


Here is my code:


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
48
49
50
51
#include "stdafx.h"
#include <iomanip>
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	int n;
	int spaces = 15;
	int divider;

	cout << "Please enter an odd integer" << endl;
	cin >> n;

	divider = n / 2;

	//Top half of diamond
	for (int i = 0; i < n; i++)
	{
		if (i <= divider + 1)
		{

			cout << setw(spaces);
			spaces--;

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

	for (int i = 0; i > n; i--)
	{
		if (i <= divider)
		{
			cout << setw(spaces);
			spaces++;
			for (int j = 0; j < 2 * i - 1; j++)
			{
				cout << "*";
			}
			cout << endl;
		}
	}


	return 0;
}

You are starting i at 0 and looping while i is greater than n; unless n is negative, this will never be true.
Ok, I fixed the previous problem, but now my bottom half is coming out in the wrong direction, and a bit distorted. Here is what I have now.

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
48
49
50
51
52
53
54
#include "stdafx.h"
#include <iomanip>
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	int n;
	int spaces = 15;
	int divider;

	cout << "Please enter an odd integer" << endl;
	cin >> n;

	divider = n / 2;

	//Top half of diamond
	for (int i = 0; i < n; i++)
	{
		if (i <= divider + 1)
		{

			cout << setw(spaces);
			spaces--;

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

	for (int i = 0; i < n; i++)
	{
		if (i < divider + 1)
		{
			cout << setw(spaces);
			spaces++;
			for (int j = 0; j < 2 * i + -1; j++)
			{
				cout << "*";
			}
			cout << endl;
		}
		
	}


	return 0;
}

Last edited on
Topic archived. No new replies allowed.