Thread: Help with arrays

1. Senior Member
Join Date
Mar 2011
Posts
171
Rep Power
0

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. 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. Senior Member
Join Date
Mar 2011
Posts
171
Rep Power
0

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. Re: Help with arrays

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

5. Senior Member
Join Date
Mar 2011
Posts
171
Rep Power
0

Re: Help with arrays

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

6. Re: Help with arrays

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

7. Senior Member
Join Date
Mar 2011
Posts
171
Rep Power
0

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. 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. Senior Member
Join Date
Mar 2011
Posts
171
Rep Power
0

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. 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. Senior Member
Join Date
Mar 2011
Posts
171
Rep Power
0

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. 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. Senior Member
Join Date
Mar 2011
Posts
171
Rep Power
0

Re: Help with arrays

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

14. Re: Help with arrays

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

15. Senior Member
Join Date
Mar 2011
Posts
171
Rep Power
0

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. Senior Member
Join Date
Mar 2011
Posts
171
Rep Power
0

Re: Help with arrays

What about a linked list?

17. 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. Senior Member
Join Date
Mar 2011
Posts
171
Rep Power
0

Re: Help with arrays

yes

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

19. 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. Senior Member
Join Date
Mar 2011
Posts
171
Rep Power
0

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 First 1234 ... Last

Posting Permissions

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