Diamond shape explanation

Hi there , the below program prints a diamond shape. this program was written by Fazle R Dayeen. I am very interested in printing different patterns like this. How the iterations of the loops working here , I have no idea.

Would you please explain this ?

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>

#include<cstdlib>

using namespace std;

int main()

{

int i=0, j=0, NUM=3;

for(i=-NUM; i<=NUM; i++)

{

for(j=-NUM; j<=NUM; j++)

{

if( abs(i)+abs(j)==NUM) // Change this condition

 { cout<<"*"; }

else { cout<<" ";}

}

cout<<endl;

}
return 0;
}
Last edited on
'i' is the row counter
'j' is the row counter.

Both will loop from -NUM to NUM (inclusive)

So the loops basically act as though they are stepping over a 2 dimentional grid. And the 'if' statement on line 22 determines which character to print on each particular point on that grid.

The condition is abs(i)+abs(j)==NUM. If that condition is true, it prints a star, otherwise it prints a space.

abs() is the absolute value (ensures a number if always positive by negating it only if it's negative).

So if abs(row) + abs(column) == NUM, you print a star. (Remember that NUM=3)

So take a look at the below grid:


  ---
  3210123
-3   *
-2  * *
-1 *   *
 0*     *
 1 *   *
 2  * *
 3   *


You'll see that the stars only exist on places where that condition is true. For example, that first row... the row number is -3

So stars will be printed in columns where abs(-3) + abs(column) = 3 is true.

All you have to do is simplify that expression:
1
2
3
4
abs(-3) + abs(column) = 3
3 + abs(column) = 3
abs(column) = 0
column = 0


Which is why... in the first row.. a star is only placed at column zero.

In the next row... it's placed at columns -1 and 1. Because again:

1
2
3
4
abs(-2) + abs(column) = 3
2 + abs(column) = 3
abs(column) = 1
column = +/- 1


And so on, and so on.
Last edited on
thank you , disch. :) You are the champion. Would you please suggest some tips to understand such problems ? Is there any tutorial exists on net which explains nested For Loop iteration better ? Or I can follow your personal advice. :)
Would you please suggest some tips to understand such problems ?


Look at each line of code and walk though it thinking "what is the computer going to do here?" It all comes down to a step-by-step sequence, and all you have to do to understand the overall process is understanding each step.

If you have difficulty doing this... use a debugger. Debuggers have 3 key features:

1) You can set a breakpoint on any line of code. When the program gets to that line of code it will "snap", pausing program execution and bringing the debugger/IDE to the foreground.

2) It has a "watch" window, where you can type in the name of any variable, and it will show you that variables contents. Combine this with #1 and you can see not only where the program is, but also what all the variable are filled with at the time.

3) It has the ability to "step into"/"step over" lines of code. When the debugger is snapped... these will allow the program to move forward exactly one line of code.


Using Step Into / Step Over with a Watch window will allow you to walk though the program and see exactly what is happening, line by line. It'll show you which lines are executing, and the contents of all interesting variables at the time. You can see exactly how it's working.



How to use basic functionality of debuggers really should be taught from day 1 in programming courses. I really don't understand how so many of them don't even mention them.



Is there any tutorial exists on net which explains nested For Loop iteration better ?


There's nothing special to nested for loops. If you understand how a for loop works... then a nested for loop is the same thing. It's just a loop that inside another loop.

In this case... the inner for loop on line 18 is looping once for each column
The outer for loop on line 14 is looping once for each row.

Put them together and you're looping once for each column, in each row. It isn't any more complex than that.
Last edited on
Topic archived. No new replies allowed.