This is my first time posting in here, hopefully I am doing it right.
Basically I need help trying to figure out some code that I wrote for class using C. The purpose o
This type of small programs are the one which kept fascinating me in my earlier days. I think they play a vital role in building logics and understand how, when, where and in which situation which loop will be perfect one.
The best way one can understand what happening is to manually debug each and every statement.
But to understand better lets understand how to build the logic for that, I am making some minor changes so that we can understand it better,
n
is no of lines to print in pyramid n =5
' '
with '-'
(dash symbol) Now pyramid will look something like,
----#
---##
--###
-####
#####
Now steps to design the loop,
n
lines i.e. 5 lines so first loop will be running 5
times.for (int rowNo = 0; rowNo < n; rowNo++ )
the Row number rowNo
is similar to tall
in your loop5
characters but such that we get our desired figure, if we closely look at what logic is there,rowNo=0
(Which is our first row) we have 4
dashes and 1
hashrowNo=1
we have 3
dashes and 2
hashrowNo=2
we have 2
dashes and 3
hashrowNo=3
we have 1
dashes and 4
hashrowNo=4
we have 0
dashes and 5
hashrowNo
we have to print n - rowNo - 1
dashes -
and rowNo + 1
hashes #
,for
loop we have to have two loops, one to print dashes and one to print hashes.for (int dashes= 0; dashes < n - rowNo - 1; dashes ++ )
, here dashes
is similar to space
in your original programfor (int hash = 0; hash < rowNo + 1; dashes ++ )
,Hope, the above explanation provides a clear understanding how the for loops that you have written will be formulated.
In your program there are only minor changes then my explanation, in my loops I have used less then <
operator and you have used <=
operator so it makes difference of one iteration.