Computer Science Canada Determining the amount of digits, and storing the values in array |
Author: | CooKieLord [ Fri Oct 12, 2007 7:08 pm ] | ||
Post subject: | Determining the amount of digits, and storing the values in array | ||
Situation: I want to break down an integer number into a bunch of digits, and store the value in an array. E.g. The user enters 1 337. The program will break it down to 1, 3, 3, 7 and store each digit in an array such that: integerArray[0] = 1 integerArray[1] = 3 integerArray[2] = 3 integerArray[3] = 7 I understand that I can use a combination of divisions and modulo in order to determine this, but the problem I have is that I need to determine the number of digits. This is what I have, but there's are logic errors and I can't figure them out.
|
Author: | HeavenAgain [ Fri Oct 12, 2007 7:14 pm ] |
Post subject: | RE:Determining the amount of digits, and storing the values in array |
try making the number into a string first, then its automatically in an array just have to convert |
Author: | Euphoracle [ Fri Oct 12, 2007 7:19 pm ] | ||
Post subject: | RE:Determining the amount of digits, and storing the values in array | ||
Well, you could do it that way, or you could do it the easy way and use a string.
Of course, if you're not allowed to do that, this won't help much ![]() |
Author: | richcash [ Fri Oct 12, 2007 7:44 pm ] | ||
Post subject: | Re: Determining the amount of digits, and storing the values in array | ||
If you take the log (base 10) of a number and round down, the result will be 1 less than the number of digits in that number. Remember, Math.log() in java.lang.Math is the natural log, so you have to use the base changing formula :
Do you know how to get the number of digits now? (Note, you'll have to make adjustments for 0 and negative numbers if you choose to include them). As you said, you can use division and modulus once you know the number of digits. |
Author: | richcash [ Sat Oct 13, 2007 12:08 pm ] | ||
Post subject: | Re: Determining the amount of digits, and storing the values in array | ||
In case you're having trouble :
Post if you need help with storing the individual digits. |
Author: | wtd [ Sat Oct 13, 2007 4:05 pm ] | ||
Post subject: | RE:Determining the amount of digits, and storing the values in array | ||
Recursion FTW! |
Author: | richcash [ Sat Oct 13, 2007 5:21 pm ] | ||
Post subject: | Re: Determining the amount of digits, and storing the values in array | ||
I would rather write your digits() method like this.
But I guess thinking recursively about it is cool too. |
Author: | wtd [ Sat Oct 13, 2007 6:03 pm ] |
Post subject: | RE:Determining the amount of digits, and storing the values in array |
You will also notice that my digitsHelper method is tail-recursive. |
Author: | CooKieLord [ Tue Oct 16, 2007 2:18 pm ] |
Post subject: | RE:Determining the amount of digits, and storing the values in array |
Thank for all the help guys, and sorry I'm a little late in replying. I'm mainly looking at the way richcash showed me. It's simpler for me, because I don't quite understand how to get recursion to work yet. Now, the only problem I have is storing the broken down values in the array. With the way you're shown to me, it would place the units in [0], the 10s in [1], the 100s in [2], etc. I need the highest digit, let's say 1000s in [0], the 100s in [1], the 10s in [2] and the 1s in [3] (I'm talking about array subscripts right? or is it called index?) Don't tell me how to do it. I'll edit this post if I can't figure it out. I just wanted to let you guys know that I read the posts. Thanks again! |
Author: | Euphoracle [ Tue Oct 16, 2007 2:26 pm ] | ||
Post subject: | RE:Determining the amount of digits, and storing the values in array | ||
Reverse the array:
|
Author: | CooKieLord [ Tue Oct 16, 2007 2:36 pm ] | ||
Post subject: | RE:Determining the amount of digits, and storing the values in array | ||
Well this is what I did
I had two variables, one for the array and the other for the modulo. Is that what you meant by "reversing"? |
Author: | Euphoracle [ Tue Oct 16, 2007 9:34 pm ] |
Post subject: | RE:Determining the amount of digits, and storing the values in array |
Your method does work, yes. |