Nested for loop and if conditional, to draw a pattern ??

Hi c++ lovers :)

This code that contains nested for loops and if conditional made sense to me to half way through. When you take a look at the outcome, I understand why it looks the way it does, up to line 50. But then, after that, I do not really know what causes the pattern to go backward.

I have a feeling that it has to do with line 27, but I am not sure why/HOW it would make the pattern go backward. I do not know if I made myself clear enough or not. If not, let me know, and I will try to explain better.

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
55
56
57
#include <iostream>
using namespace std;

int main()
{

int scale;

for(int x=0 ; x <= 10 ; x++)
{
	
	if (x <= 5)
	{
	  scale = x;
	  for(int blanks = 0 ; blanks < scale ; blanks++)
	  {
		cout << " ";
	  }
	  cout << "*" << endl;
	}
	
	else
	{
	
	if (x >= 6)
	{
	  scale = scale - 1;
	  for(int blanks = 0 ; blanks < scale ; blanks++)
	  {
		cout << " ";
	  }
	  cout << "*" << endl;
	}
	
	}
	
}

return 0;
}

/*
Outcome:

*
 *
  *
   *
    *
     *
    *
   *
  *
 *
*

*/
Last edited on
Hello Ephan,

Yes line 27 is the key. More often what I have seen for something like this is: for(int blanks = 5; blanks > 0; blanks--. You have achieved the same thing by using "scale" to change how many times the for loop iterates. The reason it goes backwards is because the value of "scale" decreases each time through the loop. Since "scale" starts with a high number the "*" is printed to the right. The second time through the loop "scale" is one less, so the "*" is printed one space closer to the left.

If it is still unclear let me know.

Hope that helps,

Andy
Last edited on
Maybe it's clearer with better naming:
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>
using namespace std;

int main()
{
int number_of_lines = 10;
int max_number_of_blanks = number_of_lines / 2;
int number_of_blanks = 0;

for(int x=0 ; x <= number_of_lines; x++)
{
	
	if (x <= max_number_of_blanks)
	{
	  ++number_of_blanks;
	  for(int blanks = 0 ; blanks < number_of_blanks; blanks++)
	  {
		cout << " ";
	  }
	  cout << "*" << endl;
	}
	
	else
	{
	  --number_of_blanks;
	  for(int blanks = 0 ; blanks < number_of_blanks; blanks++)
	  {
		cout << " ";
	  }
	  cout << "*" << endl;
	}
	
	}
	
}

return 0;
}
Optimized:
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
#include <iostream>
using namespace std;

int main()
{
int number_of_lines = 10;
int max_number_of_blanks = number_of_lines / 2;
int number_of_blanks = 0;

for(int x=0 ; x <= number_of_lines; x++)
{
	
	if (x <= max_number_of_blanks)
	{
	  ++number_of_blanks;
	}
	
	else
	{
	  --number_of_blanks;
	}
	
	  for(int blanks = 0 ; blanks < number_of_blanks; blanks++)
	  {
		cout << " ";
	  }
	  cout << "*" << endl;
	}
	


return 0;
}
In the first case the number of blanks is increased while in the second case decreased. That's it.
Thank you for replying Andy!

More often what I have seen for something like this is: for(int blanks = 5; blanks > 0; blanks--)

Yes, it's much easier to understand in that form. But I want to understand it in the form I gave :)

The reason it goes backward is because the value of "scale" decreases each time through the loop. Since "scale" starts with a high number

Yes, according to line 27, the scale does decrease by 1, with every iteration.

But I guess what I should have asked is, from what value does scale starts? You say that is starts with a
"high number", but, what number is that? See, inside the first if, in line 14, scale is said to be equal to x, which makes things easy. But inside the second if, no such declaration of scale is given! So how am I supposed to calculate scale = scale - 1?
I assume (probably wrongly) that scale=6, since up to line 50; we went through the values 0, 1, 3, 4, 5 and 6. Am I right?
Last edited on
Hi coder777.

Both the codes you provided make things really clear and easy to understand. However, my goal is to be able to understand codes that are not so "straight to the point"... for the sake of an upcoming exam :)

But thank you very much, it was helpful!
Topic archived. No new replies allowed.