-
2 Attachment(s)
intricate star pattern
hi, I need to make a pattern that looks like the one attached. I can get the stars on the left side fine, but i can't seem to get the stars on the right or the pound symbols. I tried splitting up each portion of stars ( the upper left, lower left, upper right, lower right) but i just can't seem to get the right side. If you want, you can look at my code that I have attached as well.
I would greatly appreciate anyone who attempts to help me. You must do it using a nested for loop, and if you can, no methods.
Thanks for your help,
Adam
-
I'm partially confused how you can do the left half, but not the right half, when they are mirror images. If you have code that does the left half, perform the mirror of it for the right half.
-
well, codesaway, would you mind looking at my code and showing me where i went wrong??
-
Could you describe the pattern to me, instead?
1) Describe the number of stars required for a row
2) Describe the number of blanks for a row
That's where you need to begin. By describing the pattern, you can then convert it to code.
P.S. Why is the class file saved in a .doc, instead of .java?
-
codesaway, if you just look at the star pattern.doc file i uploaded, i think you'll have a better idea of the pattern.
-
I do understand the pattern, and I have already written code that outputs it. What I'm trying to do is to get you to think like a programmer. You said that you were having problems, so I started with the most important question, how many stars are on a row?
If you can tell me, for a given row number, how many stars there are, then you are done. Since the pattern is a mirror, with the '#" in the middle, the number of stars before the "#" is the same as the number of stars after. Also, the number of blanks are directly related to the number of stars. So, in essence, the number of stars for a given row is the ONLY question that matters, because using that knowledge, you can output the pattern, as desired.
-
do I need two separate for loops to create the left pyramid and right pyramid, respectively? or is it all in the same for loop? I see it goes like this
line 1: 15 stars
2: 7 stars # 7 stars
3: 6 stars space # space 6 stars
4: 5 stars space space # space space 5 stars
5: 4 stars space space space # space space space 4 stars
etc, etc.
I don't know how to produce the mirror image.
-
How I tackled it is I broke it up into pieces. Other than the first and last row, a pattern is used, stars blanks # blanks stars.
So, I had a variable, starsCount, which I used to store the number of stars for the row. I then had a variable, blanksCount, which stores the number of blanks for the row. These values were for the left half, since the right half is a mirror.
Then, you loop. First you output <starsCount> number of stars, then <blanksCount> number of blanks. You add in the "#". Then, you do the same in reverse (since it's a mirror).
That's how I tackled the problem. This method ensures self-documenting code, since the loops, and variable names tell you what the output should be. My professors always told me that if you couldn't tell the output by reading just the code, then you did something wrong already.