

-----------------------------------

Kenster102.5

Wed Dec 03, 2008 8:54 pm


URGENT: Diamond Asterisk, I wish I kept that program!

-----------------------------------

Well last year, I failed my Grade 11 Comp Sci because I was too preoccupied with a Biology course which I failed as well, but at the time I was trying to save it, and so I let Comp Sci slide. This year I retook it and I found that I remembered quite a lot, and am doing so much better with an 81%. I also remember this one question, and it was the Diamond question using just counted for loops to create the top half. I did last year complete the code, but since, I wasn't thinking that I was going to retake Comp Sci I accidently threw it away not knowing that I would need it again for another task for this year.

By the way I am using Ready to Program.
First of all lets take baby steps and look at this arrow. 
Is there anyway I can use variables from the loops for the top half of this arrow, and initialize them in the loops for bottom half of the arrow? Or will they just be fine as separate loops?


//Top Half
for (int row = 1 ; row < 5 ; row++)
        {
            System.out.println ();
            for (int position = 1 ; position < row ; position++)
            {
                System.out.print ("");
                System.out.print ("*");
            }
        }
        
//Bottom Half
for (int row2 = -5 ; row2 < -1 ; row2++)
        {
            System.out.println ();
            for (int position2 = -1 ; position2 > row2 ; position2--)
            {
                System.out.print ("");
                System.out.print ("*");
            }
        }


After this is answered, I will then proceed to the actual diamond question.
Thanks
Ken


-----------------------------------

HellblazerX

Wed Dec 03, 2008 9:49 pm


Re: URGENT: Diamond Asterisk, I wish I kept that program!

-----------------------------------

Declare the variables you want outside both the for loops, that way you can use them in both of them.  You don't always have to declare a new variable for a for loop counter; you can use preexisting ones as well:
int row;
for (row = 1; row < 5; row++) {
     //code
}
for (row = -5; row 