Left sided Diamond

I am trying to create the diamond (with 5 rows) using for loops. I think I have the idea correct but it's not outputting the diamond right. The diamond should look like below:

*
**
***
**
*

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
#include <iostream>
using namespace std;
int main()

{
	int i, j;
	cout << "Enter a number of rows:" << endl;
	cin >> i;//rows variable

	cout << "Enter a character to draw the shape with:" << endl;
	char VIEW = '*';
	cin >> VIEW;

	for (i = 1; i < 5; i++)//initial rows is 1,
		//if row is less than 5, add row
	{
		//increasing stars
		for (j = 1; j < 3; j++)//initial stars is 1
			//if stars are less than 3 add star
		{
			cout << VIEW;
		}
		//begin reducing stars
		for (j = 2; j < 1; j++)//initial stars is 2
			//if stars are less than 1, add star
		{
			cout << VIEW;
		}
	}
}
your looping is incorrect.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	for (i = 1; i < 5; i++)//initial rows is 1,
		//if row is less than 5, add row
	{
		//increasing stars
		for (j = 1; j < 3; j++)//initial stars is 1
			//if stars are less than 3 add star
		{
			cout << VIEW;
		}
		//begin reducing stars
		for (j = 2; j < 1; j++)//initial stars is 2
			//if stars are less than 1, add star
		{
			cout << VIEW;
		}
	}

think about exactly how this is gonna work. everything inside the 'i' loop is going to be executed 5 times. this means that the first 'j' loop is going to go through and it's going to print two *'s.

Now we come to a real problem, the second 'j' loop, is going to run for quite a while. J
starts at 2, and increments by 1 until j overflows and wraps around to a negative number, at which point you will have added several millions of *'s to your output.

then, once the two inner 'j' loops have finished, we back out to the 'i' loop again, increment 'i' to 2, and run through both 'j' loops a second time... then a third time, and a fourth time, then, finally, the i loop is done, and you've just spat out 3.7 bajillion *'s to the output stream (without ever sending a newline, btw), and the program ends.
Last edited on
Actually I was mistaken. the 2nd J loop will not spit out millions of *'s to the output, it won't do anything at all, as 2 is not less than 1. I'm slipping in my old age. I could have just edited my mistake out of the previous post, but it makes me giggle so what the hell.

Another problem:
You ask the user to enter the number of rows, and store the answer in i. Then you hard code the response as 5, and use i as your loop counter.
Try for( int x = 0; x < i; x++ ) for the outer loop.
Last edited on
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
// for loop diamond
#include <iostream>
using namespace std;

int main()
{
	int row = 9;  // odd number row
	int r = (row+1)/2;  
	int s = 1;
	for (int s=1; s<2*r; s++)
	{
		if(s<=r)
		{
		  for(int i= r-s; i>0; i-- )
		    cout << " ";
		  for(int i= 2*s-1; i>0; i-- )
		    cout << "*";			
		}
		else
		{
		  for(int i= s-r; i>0; i-- )
		    cout << " ";
		  for(int i= 4*r-2*s-1; i>0; i-- )
		    cout << "*";			
		}					

		cout << endl;		
	}
cin.ignore();
return 0;	
}
1
2
3
4
5
6
7
8
9
10
11
12
13
	int increment = 1;
	int chars = 1;
	int halfPoint = (rows+1)/2;

	for( int i = 0; i < rows; i++ )
	{
		for( int j = 0; j < chars; j++ )
			cout << "*";
		cout << endl;
		if( i == halfPoint )
			increment = -1;
		chars += increment;
	}
Last edited on
Thank you, yes I need to revisit the logic of my program, if you could even call it "logic". What exactly is the sign += indicating? Is there a reason you didn't put the executable statements in your if statement in brackets?

For your code:
1
2
3
if( i == halfPoint )
increment = -1;
chars += increment;


I translated it to my own:
1
2
3
4
5
6
7
8
//once stars reach halfway point (in this case 3)
//decrement stars output by 1 less than the current printed stars

if (i == half)
{
i--;
s += i;//don't know what this code means
}
X += Y; is the same as writing X = X + Y; since assigning the result of a calculation to one of the variable used in the calculation is so common, c/c++ provides this as a convenient shorthand. it works with any of the standard mathematical operators: += -= *= /= %=

Now about the {} or lack thereof:

if(), while(), and for() loops all work with either the next line of code, or the next block of code. if there is no block delimited by {}, then only the next single line is looped (or conditionally executed by "if" statements).

in my example above, adding the braces for the 'if' would look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	int increment = 1;
	int chars = 1;
	int halfPoint = (rows+1)/2;

	for( int i = 0; i < rows; i++ )
	{
		for( int j = 0; j < chars; j++ )
			cout << "*";
		cout << endl;
		if( i == halfPoint )
		{
			increment = -1;
		}
		chars += increment;
	}


notice also there are no braces in the for( int j = 0; j < chars; j++ ) loop. only the cout << "*"; line is repeated there.
Last edited on
Thanks, I got it to print out the rows with stars each incremented by 1, however, the compiler doesn't seem to be even acknowledging the halfway if statement...I wonder if it's a matter of code placement.

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

#include <iostream>
using namespace std;
int main()

{
	cout << "Enter a number of rows:" << endl;
	int rows = 0;//initialize rows
	cin >> rows;

	cout << "Enter a character to draw the shape with:" << endl;
	char VIEW = '*';
	cin >> VIEW;

	for (int i = 1; i <= rows + 1; i++)//initialize 1 row, if i is less than or equal to rows plus 1 increase rows by 1
	{
		//section for printing the stars
		//for most recent value of stars is less than or equal to recent value of i minus 1, add star
		int halfway = (rows + 1) / 2; //halfway mark
		int star = 0;//its making me initialize star even though its already initialized in the for loop
		for (int star = 1; star <= i - 1; star++)
			cout << VIEW;
		cout << endl;
		//once stars reach halfway point (in this case 5 rows plus 1 equals 6 divided by 2 is 3 rows)
		//decrement stars output by 1 less than the current printed stars
		if (rows == halfway)
			star = star--;
	}
}




the reason you're having to initialize star before the for loop and in the for() statement, is that you are actually making two separate star variables. the one in the for() loop is only visible within the loop, and the other that you declared outside the loop is the one that is decremented later at line 27. You can fix this by changing the for() loop like this for( star = 1; star <= i -1; star++ ) by removing the 'int', the for loop will now use the existing star declared on the previous line.

star = star--; is a redundancy. star--; alone is all you need.

now there's still a logical problem. You check if you're at the halfway point and then decrement star. but this would only decrement star once at the exact halfway point. after that it'll go back to incrementing it.

in my example, I made a separate variable 'increment' which holds the value to increment the 'chars' variable by each time through the loop. initially, increment is set to 1, so 1 is added to 'char' each time through the loop. When the halfway point is reached, I change 'increment' to -1, which will then cause chars += increment; to evaluate as chars = chars + increment; or chars = chars + -1;
Last edited on
Thank you for helping!!...I made some changes and got things closer to what I want...when I have some more free time to concentrate, I'll tweak it some more.
Finally got some time and hashed this out...got correct output now! Thanks
Did you just bring this thread back to the top to tell us that? I for one am very curious to see how you chose to solve this :)
yep
Topic archived. No new replies allowed.