Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 2 of 2 FirstFirst 12
Results 26 to 40 of 40

Thread: A little assignment involving arrays.

  1. #26
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: A little assignment involving arrays.

    Whats happening in sum[thirteen] = multiply * thirteen;? All elements in an array are numbered, so why doesn't sum[thirteen] give me an exception message or something?

  2. #27
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,360
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: A little assignment involving arrays.

    Quote Originally Posted by Melawe View Post
    Whats happening in sum[thirteen] = multiply * thirteen;? All elements in an array are numbered, so why doesn't sum[thirteen] give me an exception message or something?
    What? Why would it? Have you followed any of the advice I gave you so far?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #28
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: A little assignment involving arrays.

    Kevin I am trying to follow your advice but not understanding line 8
    sum[thirteen] = multiply * thirteen;
    is really being a hurdle. I really don't understand that line at all, it was used in another project from the Jin24 so I thought I'd try it and see if it works.

  4. #29
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,360
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: A little assignment involving arrays.

    Quote Originally Posted by Melawe View Post
    Kevin I am trying to follow your advice but not understanding line 8
    sum[thirteen] = multiply * thirteen;
    is really being a hurdle. I really don't understand that line at all, it was used in another project from the Jin24 so I thought I'd try it and see if it works.
    What about it don't you understand? Have you stepped through that line with a debugger? Have you at least used print statements to figure out what's going on?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #30
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: A little assignment involving arrays.

    Sorry I didn't post in about a week. Netbeans started to compile forever(at least for how long I let it), reinstalled it and it seams to be working as it should.
    Meanwhile I've been trying to figure out what was going on in line 8, did a few searches online, and found out what was happening. And after a few tries got it right!

    Kevin I think I got you a little angry with all the line 8 questions, sorry if so. Was trying everything I could think of and none worked, and I thought sum[thirteen] was causing the mistake.:/
    Here is the updated code and output below.

    Updated code:
    class ThirteenArray {
        public static void main(String[] args){
            //Hour 9, Second activity.
            int multiply = 13;
            int[] sum = new int[400];
            int num = 1;
           for(int thirteen = 0; thirteen < sum.length; thirteen++){
                sum[thirteen] = multiply * num;
                System.out.print(" " + sum[thirteen]);
                num++;
            }
            System.out.print("\n " + sum[0]);
        }
    }

    Output:

    run:
    13 26 39 52 65 78 91.... 5174 5187 5200
    13BUILD SUCCESSFUL (total time: 0 seconds)


    Thanks everyone for taking the time to help me!!
    Last edited by Melawe; March 27th, 2011 at 09:33 PM.

  6. #31
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,360
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: A little assignment involving arrays.

    I'm glad you got it working, but for posterity's sake, I should just point out that your "num" variable isn't really necessary.

    Hint- What is sum when compared to thirteen each iteration?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #32
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: A little assignment involving arrays.

    When I use the code below sum[0] is always being set to 0.

    class ThirteenArray {
        public static void main(String[] args){
            //Hour 9, Second activity.
            int multiply = 13;
            int[] sum = new int[400];
            //int num = 1;
           for(int thirteen = 0; thirteen < sum.length; thirteen++){
                sum[thirteen] = multiply * thirteen;
                System.out.print(" " + sum[thirteen]);
                //num++;
            }
            System.out.print("\n " + sum[0]);
        }
    }

    Output:
    0 13 26 39 52 65 78 91.... 5148 5161 5174 5187
    0BUILD SUCCESSFUL (total time: 1 second)

  8. #33
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,360
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: A little assignment involving arrays.

    Quote Originally Posted by Melawe View Post
    When I use the code below sum[0] is always being set to 0.
    Yes, that's what I would expect to happen.

    Maybe it would be easier to see what was going on if you only did, say, the first 10 or less iterations. What is num each time? What is thirteen (which is really not a great name for an incrementing variable, by the way)?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #34
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: A little assignment involving arrays.

    Hopefully you can print the table (2X1=2, 2X2=4 ..............), just use a little logic and relationships of the indexes being transfered through the loop and you will be able to do that...

  10. #35
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: A little assignment involving arrays.

    Quote Originally Posted by Melawe View Post
    When I use the code below sum[0] is always being set to 0.

    class ThirteenArray {
        public static void main(String[] args){
            //Hour 9, Second activity.
            int multiply = 13;
            int[] sum = new int[400];
            //int num = 1;
           for(int thirteen = 0; thirteen < sum.length; thirteen++){
                sum[thirteen] = multiply * thirteen;
                System.out.print(" " + sum[thirteen]);
                //num++;
            }
            System.out.print("\n " + sum[0]);
        }
    }

    Output:
    0 13 26 39 52 65 78 91.... 5148 5161 5174 5187
    0BUILD SUCCESSFUL (total time: 1 second)
    The reason is you start with 0 and anything when comes to multiply with zero, results in zero.

  11. #36
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: A little assignment involving arrays.

    What would happen if you started with

    sum[thirteen] = multiply * (thirteen + 1);

    ?

    The first value of thirteen is 0, so sum[0] = multiply * 0 = 0 the way you have it. (I think Mr. 777 said that above already.)

    If you change it like I recommend you do, it will do:

    sum[0] = multiply * (0+1) = 13

  12. #37
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: A little assignment involving arrays.

    Sorry I haven't been posted any updates. PC is acting up keeps giving a nasty "Error loading os." message. So I've been backing up and hadn't much time for coding. I did get a chance to update the code and I think I got it this time.

    class ThirteenArray {
        public static void main(String[] args){
            //Hour 9, Second activity.
            int thirteen = 1;
            int[] sum = new int[400];
           for(int multiply = 0; multiply < sum.length; multiply++){
                sum[multiply] = thirteen * 13;
                System.out.print(" " + sum[multiply]);
                thirteen++;
            }
        }
    }

    Output: 13 26 39 52....5161 5174 5187 5200

    Does that do it with out anything unnecessary?

  13. #38
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: A little assignment involving arrays.

    Changed the names of the variables to something more suitable.

    class ThirteenArray {
        public static void main(String[] args){
            //Hour 9, Second activity.
            int multiply = 1;
            int[] sum = new int[400];
           for(int aryelement = 0; aryelement < sum.length; aryelement++){
                sum[aryelement] = multiply * 13;
                System.out.print(" " + sum[aryelement]);
                multiply++;
            }
        }
    }

    Anything I could do better?

  14. #39
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,360
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: A little assignment involving arrays.

    I would still say your multiply variable is pretty unnecessary.

    But if I were you, I wouldn't spend so much time making sure everything is perfect. Does it work? Does it do what you expect? If so, kudos. Move on to the next thing. You'll drive yourself crazy if you keep trying to make your code perfect this early in the game.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  15. #40
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: A little assignment involving arrays.

    Thanks all!

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Really need help on assignment
    By coorscollector in forum Java Theory & Questions
    Replies: 2
    Last Post: January 16th, 2012, 05:08 PM
  2. need help on an assignment :(
    By gamfreak in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 23rd, 2010, 03:20 PM
  3. Replies: 1
    Last Post: February 22nd, 2010, 07:20 AM
  4. Need help with assignment
    By TonyL in forum Loops & Control Statements
    Replies: 2
    Last Post: February 20th, 2010, 08:44 PM
  5. Connect 4 involving minimax.
    By mandar9589 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 21st, 2009, 10:52 AM