Click to See Complete Forum and Search --> : figures from nested loops question, not a homework


lanef
September 30th, 2009, 12:06 AM
Hi,

I'm embarassed to ask this but I've gotta. I came across an example in an algorithms book and for the life of me I can't figure out how they got the answer.

Problem: Draw an isoceles triangle using asterisks (afaik this is the only requirement) using nested loops.

Final output: (I don't know if this was given before hand or not, it's just all thrown together in the book, I guess maybe they had to give the expected final image first (I'm shortening it but the song remains the same).

---*--
--***- Where - represents a blank and * makes the triangle visible
-*****

Solution: (written in c-esque)


main()
{
constant N = 3;

for (int i = 0; i < N; i++)
{
for (int j = 0; j < N * 2; j++)
{
if (j < N-i || j > N+i) print (' '); // print a blank
else print ('*'); // print a star
}
print('\n');
}
}// end main()


Questions:
How did they know to set N at 3? Is that just the number of rows?

Why isn't there a row with two asterisks in it? And another with four asterisks in it?

How did they know to set the conditional for the inner loop at N * 2? Is that just the number of columns?

How did they know to always leave the first column blank?

How did they know how many rows and columns make an isoceles triangle (unless they were given the image as part of the requirements)?

How did they know to compare j to be less than N-i OR greater than N+i?

Thank you for your time and patience and if you will help explain this to me I'd really appreciate it.

Be Well,
lanef

Zachm
September 30th, 2009, 07:36 AM
How did they know to set N at 3? Is that just the number of rows?


Yes, this is just the number of rows - you could also input it from program user or change it in the code to any valid(non-negative) int value.


Why isn't there a row with two asterisks in it? And another with four asterisks in it?

The outer loop is responsible of iterating over the rows while the inner loop iterates over the columns.
According to the if statement inside the inner loop -
For i == 0, the inner loop will print '*' only for j == N
For i == 1, the inner loop will print '*' for j == N-1, j == N, j == N+1
For i == 2, the inner loop will print '*' for j == N-2, j == N-1, j == N, j == N+1, j = N+2

And so on...
This is because of the if statement condition that requires the j value to be in between N-i and N+i. Since i increases by 1 on each row, the range of asterisks {N-i to N+i} increases by 2 on each row. Since in the 1st row the number of asterisks is 1, in the 2nd it will be 3, in the 3rd it will be 5 and so on.


How did they know to set the conditional for the inner loop at N * 2? Is that just the number of columns?

If you want to print a pyramid of height N where the number of asterisks on level k (level 0 would be the top of the pyramid), is equal to the number of asterisks on level k-1 plus 2 asterisks, the number of columns needed is twice the number of rows. This is simple math.


How did they know to always leave the first column blank?

Actually you could do without leaving it blank by running the inner loop from start index 1 and not 0 (see change marked in red):

main()
{
constant N = 3;

for (int i = 0; i < N; i++)
{
for (int j = 1; j < N * 2; j++)
{
if (j < N-i || j > N+i) print (' '); // print a blank
else print ('*'); // print a star
}
print('\n');
}
}



How did they know how many rows and columns make an isoceles triangle (unless they were given the image as part of the requirements)?

Refer to my answer to your 3rd question.


How did they know to compare j to be less than N-i OR greater than N+i?

Refer to my answer to your 2nd question.

Regards,
Zachm

lanef
September 30th, 2009, 01:44 PM
Thank you Zachm!