# Help with arrays

Show 40 post(s) from this thread on one page
Page 1 of 7 123 ... Last
• 03-06-2012, 06:41 PM
Help with arrays
I have an array of length 6 and I would like to search through it and remove one of the integers that is in the array. It is an array made up of 6 digits, i.e. 123456.

So, I would also like to possibly change it to a string and have the ability to change that string back into an array.

Ideas anyone? do you think it might be easier to deal with using an arraylist? I'm open for any idea
• 03-06-2012, 06:51 PM
KevinWorkman
Re: Help with arrays
Which part of this are you having trouble with? You've outlined several steps here, which one are you stuck on? What do you have so far?
• 03-06-2012, 07:15 PM
Re: Help with arrays
So I am not sure really. I created a random number generator so that I can create a 6 digit number where I can add up all the numbers that are in it. The best way I figured to do that is using an array. Now what I am trying to do is figure out how to search that array for a certain number and if it is there, remove it.

I am trying to create a simple math game, so eventually I will have to figure out how to put the input into strings so the user can input a 6 digit number and then I will change that to an array of length 6. But for now I am just trying to figure out how to get the program to give me the proper output that I want. I will worry about the input and strings and such later
• 03-06-2012, 07:19 PM
Norm
Re: Help with arrays
A question: what do you mean by a digit?
'1' is a character digit. "2" is a String
3 is an int value that takes 2 binary bits to hold its value.
• 03-06-2012, 07:20 PM
Norm
Re: Help with arrays
Quote:

how to get the program to give me the proper output that I want
Post the code and its output and explain what the problem is.
• 03-06-2012, 07:37 PM
Re: Help with arrays
Code:

```import java.util.*; public class Hardcode {                 public static void main(String[] args)                 {                         randomNumber();                 }                                 //start of hard coding                 private static int sumOfNums = 0;                 public static void randomNumber()                 {                         int x[] = new int[6];                         int index = 0;                         Random generator = new Random();                         for(int i = 0; i<6; i++)                         {                                 int randomInt = generator.nextInt(10);                                 System.out.print(x[i] = randomInt);                                 index++;                         }                         checkDivisibility(x);                 }                                 public static void checkDivisibility(int[] x)                 {                         System.out.println();                         sumOfNums = (x[0]+x[1]+x[2]+x[3]+x[4]+x[5]);                         System.out.print(sumOfNums);                         if(sumOfNums%3 != 0)                         {                                 greatestToLeast(x);                         }                         else{}//895396-998653 ==40 need to take away 2 numbers                 }                                 public static void greatestToLeast(int[] x)                 {                         Arrays.sort(x);                         int sorted[] = new int[6];                         sorted[0] = x[5];                         sorted[1] = x[4];                         sorted[2] = x[3];                         sorted[3] = x[2];                         sorted[4] = x[1];                         sorted[5] = x[0];                         System.out.println();                         for(int i = 0; i < sorted.length; i++)                         {                                 System.out.print(sorted[i]);                         }                         removeDigit(sorted);                 }                                 public static int removeDigit(int[] x)                 {                         if(sumOfNums%3 == 1)                         {                                 for(int i = 0; i < 6; i++)                                 {                                 }                         }                 } }```
So what I need help with is the removeDigit method.

Lets say I get a random number like 897261. The program then checks if it is divisible by 3. If not it sorts it from least to greatest, and then moves it into a new array that makes it greatest to least. So now the output after the greatest to least method for this number would be 987621. This number is equal to 33, so it is divisible by 3, but if it wasn't I would want to remove the smallest possible number to make it divisible by 3. so lets say the number was the one that i just noted in my code...998653. In this case it equals 40, with a remainder of 1. so you either want to remove a 1, 4, 7 etc... but in this case we would have to remove 2 of those numbers there the 8 and 5 will give you the largest possible number from those that is divisible by 3. 9963. Granted I am sure there are other possibilities, but I want the biggest number that can be made.
So for a simpler example, 987622. That is equal to 34. You would think just take out the two 2's, but in fact you want to remove the 7 because 98622>9876.

That is as best as I can explain it. Not as easy as said as done lol
• 03-06-2012, 07:44 PM
Norm
Re: Help with arrays
Do you have a problem with coding your algorithm in java
or are you having problems designing an algorithm for your program?

If the problem is with java coding, please explain.
• 03-06-2012, 07:48 PM
Re: Help with arrays
i guess more coming up with the algorithm to execute it that i need the help with
• 03-06-2012, 09:05 PM
Re: Help with arrays
can someone help me with this algorithm?
• 03-06-2012, 09:10 PM
Norm
Re: Help with arrays
Can you explain the problem and give some examples of what needs to be done?
• 03-06-2012, 09:22 PM
Re: Help with arrays
I am having trouble figuring out how to execute what I want to be done. as I said in the previous posts take a number, see if it is divisible by 3, if it is then done, if not, sort it from greatest to least, and remove 1 or 2 of the digits to make it divisible by 3, but it be the highest possible number prodoced.

Lets say I get a random number like 897261. The program then checks if it is divisible by 3. If not it sorts it from least to greatest, and then moves it into a new array that makes it greatest to least. So now the output after the greatest to least method for this number would be 987621. This number is equal to 33, so it is divisible by 3, but if it wasn't I would want to remove the smallest possible number to make it divisible by 3. so lets say the number was the one that i just noted in my code...998653. In this case it equals 40, with a remainder of 1. so you either want to remove a 1, 4, 7 etc... but in this case we would have to remove 2 of those numbers there the 8 and 5 will give you the largest possible number from those that is divisible by 3. 9963. Granted I am sure there are other possibilities, but I want the biggest number that can be made.
So for a simpler example, 987622. That is equal to 34. You would think just take out the two 2's, but in fact you want to remove the 7 because 98622>9876.
• 03-06-2012, 09:30 PM
Norm
Re: Help with arrays
Is this the problem?
Quote:

take a random 6 digit number,
see if it is divisible by 3,
if it is then done,
if not, sort the digits from greatest to least: For example: 234567 goes to 765432

and remove 1 or 2 of the digits to make it divisible by 3, <<<<<<< THIS LOOKS like the hard part

but it be the highest possible number produced <<<< What does this mean??? Show an example
Could you show an example?
• 03-06-2012, 09:44 PM
Re: Help with arrays
That is the hard part! hahaha

So, a number like 234567 right? not divisible by 3 as the digits in it add up to 27.
So now we have the number sorted. 765432.
In order for this number to become divisible by 3 we will have to remove some digits. There are numerous combinations of digits that we can remove. However, I want to remove only the number(s) so that while still ordered it is the highest number possible.

i.e. for this example I would remove the 3 as opposed to the 6, even though they both give us a number divisible by 3. So, I want 76542 because that is > 75432.

Is it starting to make some sense now? hahaa
• 03-06-2012, 09:51 PM
Norm
Re: Help with arrays
use a loop and build a number by dropping out one of the 6 digits each time around the loop
• 03-06-2012, 09:54 PM
Re: Help with arrays
can you give me some sort of example please?
• 03-06-2012, 09:55 PM
JosAH
Re: Help with arrays
Quote:

Originally Posted by adjit
In order for this number to become divisible by 3 we will have to remove some digits. There are numerous combinations of digits that we can remove. However, I want to remove only the number(s) so that while still ordered it is the highest number possible.

i.e. for this example I would remove the 3 as opposed to the 6, even though they both give us a number divisible by 3. So, I want 76542 because that is > 75432.

Is it starting to make some sense now? hahaa

Not really; if a number can not be divided by three and one of the digits is a three, removing it doesn't make the resulting number dividable by three; there's nothing laughable about it.

kind regards,

Jos
• 03-06-2012, 09:57 PM
Norm
Re: Help with arrays
Break up the number into the individual digits -> into an array??
Then write a loop to build a number using 5 of the 6 digits leaving one out each time.
• 03-06-2012, 09:59 PM
Re: Help with arrays
Quote:

Originally Posted by JosAH
Not really; if a number can not be divided by three and one of the digits is a three, removing it doesn't make the resulting number dividable by three; there's nothing laughable about it.

kind regards,

Jos

I wasn't saying take out the 3 because of the fact that it is a 3. I was saying take out the 3 because it will give us the desired number that is divisible by 3.

It could be any number so long as the final product is divisible by 3
• 03-06-2012, 10:00 PM
Re: Help with arrays
Quote:

Originally Posted by Norm
Break up the number into the individual digits -> into an array??
Then write a loop to build a number using 5 of the 6 digits leaving one out each time.

Don't really know what you mean...
• 03-06-2012, 10:03 PM
Norm
Re: Help with arrays
which part don't you understand?
Show 40 post(s) from this thread on one page
Page 1 of 7 123 ... Last