Page 2 of 7 FirstFirst 1234 ... LastLast
Results 21 to 40 of 136
  1. #21
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: Help with arrays

    So the number is already in an array. Are you saying like a nested loop? i.e. one for loop that counts up to 6 and the other that counts up to 5?

  2. #22
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: Help with arrays

    To work with the individual digits in the number you could split the digits out into a 6 element array. If the number is already in a 6 element array, then you won't have to do that step.

    To see what I'm saying about taking 5 of the 6, write down a 6 digit number on a line, then on the lines underneath that number write all of the numbers that use 5 of the 6 digits. The process you use to generate each of those lines would be what you would code in your program.

  3. #23
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: Help with arrays

    But I don't even know what I am checking for!
    It wouldn't be searching through the array thats the problem, its getting it to do what I want... i.e. find the smallest number to pull out

  4. #24
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: Help with arrays

    Finding the smallest digit in the array would be very easy to do in a loop

  5. #25
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: Help with arrays

    But taking out the smallest digit doesn't necessarily make the new number divisible by 3

  6. #26
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: Help with arrays

    That's why there is a loop. The numbers are sorted high to lowest.

  7. #27
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: Help with arrays

    But now what if you need to take out 2 numbers in order for the new number to be divisible by 3? if statement and a new loop?

  8. #28
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: Help with arrays

    Do it one thing at a time. Worry about removing 2 digits after you get it working for 1 digit.

  9. #29
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: Help with arrays

    Well actually, how can I do that? How do I remove one of the digits from the array and then add up all of the digits less that one that we are checking?

  10. #30
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: Help with arrays

    You can leave it in the array and just not use it. The loop index points to the digit not to be used when adding up.

  11. #31
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: Help with arrays

    So then how do I go about adding up the others? do I have to add another index or something?

  12. #32
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: Help with arrays

    Make a small simple program to add together the contents of an int array with 3 elements using a loop.

    When that works, add logic that only adds two of the three elements.

  13. #33
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: Help with arrays

    What about using a recursive loop? would that be more effective?

  14. #34
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: Help with arrays

    Try it and see what happens.
    Loops are not recursive, methods are.

  15. #35
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: Help with arrays

    I'm honestly at a loss... I am thinking right now set up 5 index's and just going from there

  16. #36
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: Help with arrays

    What about a linked list?

  17. #37
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: Help with arrays

    Can you do this:
    Make a small simple program to add together the contents of an int array with 3 elements using a loop.

  18. #38
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: Help with arrays

    yes

    Java Code:
    int sum;
    for(int i = 0; i<x.length; i++)
     {
         sum += x[i]
     }

  19. #39
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: Help with arrays

    Now add another outer loop that iterates over the size of the array and in the inner loop skip adding x[i] if the index of the outer loop == i

    Your test code needs to define a real array with contents and print out the value of sum when the inner loop completes.

  20. #40
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: Help with arrays

    The problem isn't the loop itself... the problem is what goes inside the loop. I don't know how to add up the array and skip over that one element.
    Also I need to do something that will output the new number as a 5 digit number and not just as a sum, but I figure that will be easy once I figure this out... Could you by any chance show me some sample code to work off of?

Page 2 of 7 FirstFirst 1234 ... LastLast

Similar Threads

  1. Casting Enum Type arrays to object type arrays
    By nmvictor in forum Advanced Java
    Replies: 4
    Last Post: 02-17-2012, 12:49 PM
  2. arrays and multidimensional arrays
    By belfast09 in forum New To Java
    Replies: 5
    Last Post: 06-14-2011, 01:28 PM
  3. help with arrays
    By droidus in forum New To Java
    Replies: 20
    Last Post: 04-19-2011, 06:29 AM
  4. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 10:32 AM
  5. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •