Diamond Project

Okay the goal is to make output a diamond like this

-----*
----***
---*****
--*******
-*********
***********
-*********
--*******
---*****
----***
-----*

(note: had to put dashes for the diamond to actually look like a diamond. Not actually part of the program)

I have 2 questions that I can not figure out. How to properly space the star from the left so like the single star on top is 5 spaces, the next is 4, then 3, 2,1 ect.

The next question is How to get the stars to descend after that middle row.

heres what I have so far. The loops are split up from Top of diamond, middle, and bottom

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
  // PART 2: print top of diamond lines

		for (int i = 0; i < row; i++)
		{
			

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

      // PART 3: print center line


		for (int i = 0; i < row; i++)
		{
			cout << "*";
		}

		cout << endl;

      // PART 4: print bottom of loop lines

		for (int i = 0; i > row; i++)
		{
			

			for (int j = row; j < i; j--)
			{
				cout << "*";
			}
			cout << endl;
		}
Last edited on
start with a string containing
"     *"

each time through the loop, remove a space from the front, add 2 asterisks to end.
at halfway point, revers process.

read this: http://www.cplusplus.com/reference/string/string/substr/
Last edited on
Question 1: You can use the manipulator setw() form the <iomanip> library. Setw is used to output the vaule of an expression in a specific number of columns. <iomanip> library offers you a lot of functions to format the output, it not hard, you should read it to make your desired output.

Question 2: You just reverse the process, not necessary to print center line. Set for ( i=(N-i); i>=1; i--);

Furthermore, your code is wrong, in ur Part 2, the result will be increasing by 1 * in each line, not meet the require of task( Line 1: 1 *, line 2: 3 *, line 3: 5 *, each line the number of starts will increase 2 not 1 ).

So this 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
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
	int i, j, N;
	cout<<"Enter N: ";
	cin>>N;
		for (i=1; i<=N; i++)
	{
		cout<<setw(N+1-i);
		for (j=3; j<=(2*i+1); j++)
		  cout<<"*";
		cout<<endl;
	}

		for (i=(N-1); i>=1; i--)
	{
		cout<<setw(N+1-i);
		for (j=3; j<=(2*i+1); j++)
		  cout<<"*";
		cout<<endl;
	}
	
	
return 0;
}

Last edited on
Topic archived. No new replies allowed.