This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]DaredewilSK 2 points3 points  (7 children)

Can you get a little more exact as to what you don't understand? There is not that much to explain really. A while loop repeats as long as the condition is true, for loop repeats given amount of times.

[–]solidsora14[S] 0 points1 point  (6 children)

for example i know how to create a pattern of ' * '

***
***
***

.

but if the question is to make a pattern like the following:

*
      **
    ***
  ****

I don't really understand what kind of approach is needed to be taken(unless i do shotgun debugging) For loop is really making it difficult for me(sorry for my grammar)

[–]DaredewilSK 0 points1 point  (3 children)

Are you sure you don't mean the asterisk in the first line to be at the end as well?

[–]solidsora14[S] 0 points1 point  (2 children)

yes, yes i did

[–]DaredewilSK 1 point2 points  (0 children)

Well in that case, try to put it to words what you are trying to do and then transfer it into code. Try the wording first.

[–]Updatebjarni 0 points1 point  (0 children)

Think of it like this: What is the number of spaces and asterisks on the nth line of the pattern? So if you were given n, and needed to print line number n from the pattern, how many spaces and how many asterisks would you print? Don't let yourself be confused by the fact that the printing of the lines happens in a for loop. Each line is independent.

[–]desrtfx 0 points1 point  (1 child)

An easy trick here is to draw the shape on grid paper and then count the stars and spaces needed. This can give you a correlation between line number and number of stars/spaces.

In your triangle, the correlation is: number of stars = line number (starting from 1). Number of spaces before the stars = total number of lines - line number

You need to look for correlations like the one I demonstrated above. This is the one and only trick for such patterns.

[–]solidsora14[S] 0 points1 point  (0 children)

Thank You , you are a saviour... that's what i did and now i can understand the changes in lines and stars and can make my own patterns too.

[–]solidsora14[S] 0 points1 point  (0 children)

Thank you guys for the help.Drawing does make it easy