Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 URGENT: Diamond Asterisk, I wish I kept that program!
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Kenster102.5




PostPosted: Wed Dec 03, 2008 8:54 pm   Post subject: 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?

code:

//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
Sponsor
Sponsor
Sponsor
sponsor
HellblazerX




PostPosted: Wed Dec 03, 2008 9:49 pm   Post subject: 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:
Java:
int row;
for (row = 1; row < 5; row++) {
     //code
}
for (row = -5; row <-1; row++) {
     //code
}
DemonWasp




PostPosted: Fri Dec 05, 2008 11:51 pm   Post subject: RE:URGENT: Diamond Asterisk, I wish I kept that program!

Point of interest: it works fine either way, and there's no real speed gain for either method. In fact, the distinction probably gets compiled out in the majority of cases.

However, there is one critical point: try to make sure that loop counters are as local as they possibly can be. I wrote a program in grade 10 (a SimCity clone) in VB6. For some reason, I couldn't seem to declare my loop-counters locally, and the result was dozens of bugs from failing to initialize the loop-counter - I probably wasted about 5% of my time hunting down that kind of error.

Also, in Java, variables declared in a for loop are local to that loop. The following code works:
Java:

public class test {
        public static void main ( String[] args ) {
                for ( int i = 0; i < 10 ; ++i ) {
                        System.out.println ( i );
                }
                for ( int i = 10; i >= 0 ; --i ) {
                        System.out.println ( i );
                }
        }
}


Meaning you can easily redeclare i, or row, or whatever. It's local to the for loop.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: