Drawing a Diamond

Hello everyone,

My objective is to draw a diamond shaped object with unique border and fill characters, and a size specified by the user/data file. Also there are spaces between each character. The size that is specified is for one side of the diamond, on this example diamond the size would be 4.
1
2
3
4
5
6
7
   $
  $ $
 $ * $
$ * * $
 $ * $
  $ $
   $


So, I know that I have to use some combination of for loops.
1
2
3
4
for(int linenum = 0; linenum < size*2 - 1; linenum++)
	{
	
	}


Man am I lost. This is all I have.. I have thought about making some sort of bool for when it gets to the middle line to start going in reverse, or something like that but I cannot come up with how to switch from the border to fill, or fill to border, or spaces to border. Can someone point me in the right direction here?

Any help is appreciated.
As you go down the diamond starting from the top, the length of each slice of diamond first increases then decreases. So I think you could have two for() loops one after the other. The first counting up and the second counting down.

Then with each iteration of one of those loops you know how many characters there are in that particular 'slice' of diamond...
Alright I got the two for loops but I can't figure out how to get the rest of the fill and other border characters in without messin it up.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
for(int lcv2 = 0; lcv2 < size; lcv2++)
	{
		countdown = countdown - 1;
		for(int count = 0; count < countdown; count ++)
			cout << " ";
		cout << border << endl;
	
		
	}

	for(int lcv = 0; lcv < size - 1; lcv++)
	{
		while(countup < size - 1)
		{
			countup = countup + 1;
			for(int lcv = 0; lcv < countup; lcv++)
				cout << " ";
			cout << border << endl;
		}
		
	}



I'm gettin
1
2
3
4
5
6
7
   $
  $
 $
$
 $
  $
   $


Excellent start.

Note that I'm low on caffeine, so this may not be the best solution to a problem I've come up with.

Now you will want a for loop that runs immediately after the $ sign is placed that runs (size of the diamond - number of spaces before the $ in question - 1) * 2 times to fill in the... texture is all I can call it. It would add space on the even number and an asterisk on the odd (assuming the iterator starts from 0). On the last iteration, it would also add a $.

-Albatross
So here is my final code, finally got it.. Although I am not sure why the

1
2
for(int lcv3 = 0; lcv3 < size - countup - 2; lcv3++)
				cout << " " << fill;


loop runs the correct amount of times. I didn't follow exactly what you were saying Albatross but it just gave me the idea to use the number of spaces before the border character in order to figure everything else out. But thanks for the help!


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
	for(int lcv2 = 0; lcv2 < size; lcv2++)
	{ // begin initial for loop
		countdown = countdown - 1;		
		numspaces = size - countdown;
		for(int count = 0; count < countdown; count ++)
			cout << " ";
		cout << border;
		
		if(numspaces == 1)
			cout << endl;
		else if(numspaces == 2)
			cout << " " << border << endl;
		else if(numspaces > 2)
		{	//begin elseif
			for(int lcv3 = 0; lcv3 < lcv2 - 1; lcv3++)
				cout << " " << fill;
			cout << " " << border << endl;
		}	//end elseif
		
	}	//exit initial for loop

	for(int lcv = 0; lcv < size - 1; lcv++)
	{	//begin FORLOOP2
		countup = countup + 1;

		for(int lcv2 = 0; lcv2 < countup; lcv2++)
			cout << " ";
		cout << border;

		if(countup == size - 1)
			cout << endl;
		else if(countup == size - 2)
			cout << " " << border << endl;
		else if(countup < size - 2)
		{	//begin ELSEIF2
			for(int lcv3 = 0; lcv3 < size - countup - 2; lcv3++)
				cout << " " << fill;
			cout << " " << border << endl;
		}	//end ELSEIF2
		
	  }	//end FORLOOP2
Topic archived. No new replies allowed.