int [] a = {1234}; //this represents 1,234
int [] b = {7777}; //this represents 7,777
the reason for this notation is to represent very large numbers i.e. 1,111,111,111,111. instead of it being rep as a "long" it is put into an array {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1} or whatever. you get the idea.
my question.... adding "a" and "b" arrays to form a number like 9,011.
does that make sense?
Matt Wilde wrote:This is not quite like it sounds.
int [] a = {1234}; //this represents 1,234
int [] b = {7777}; //this represents 7,777
the reason for this notation is to represent very large numbers i.e. 1,111,111,111,111. instead of it being rep as a "long" it is put into an array {111111111111111} or whatever. you get the idea.
my question.... adding "a" and "b" arrays to form a number like 9,011.
does that make sense?
Not really. {111111111111111} is an array with one element, which is a very big number.
Henry
ya but what happens if i try to do math on java with an number that has 100 integers or more? thats why i want to represent each integer in a larger number as a spot in one array
Matt Wilde wrote:sorry Henry i edited the first post i didn't realize that {1111111111111} was just one value. forgot.
ya but what happens if i try to do math on java with an number that has 100 integers or more? thats why i want to represent each integer in a larger number as a spot in one array
Oh, I see... well, if you are looking for a way to represent large integers, you should look at the BigInteger class. If you are doing this as an exercise, then the answer is, you will add those two arrays the same way that you would do it on paper. Do the units digit, if it overflows, then truncate and carry the overflow to the next digit, repeat.
Henry
something vaguely along these lines?: (i know this must look disgusting but i'm a noob so ya)
BUT of course this does not compile because i get outofbounds exception. because h.b.length is not the same as b.length (current obj). tips?
Matt Wilde wrote:yes i would use BigInteger in a heartbeat if it weren't that we had to use arrays.
Right. So you want to wrap an int[] and use it as though each element is a 'digit', right?
First, your code: You should try not to make the language perform any more calculations than it needs to; and yours does b[i]+h.b[i] at least twice, and in the worst case 4 times. Try something like:(NOTE: I haven't corrected your outofbounds error)
I would also suggest that you come up with proper names for your arrays. You should really only use single letters for indexes in a loop. Also: spread your code out a bit; it makes it much easier to read.
BUT of course this does not compile because i get outofbounds exception. because h.b.length is not the same as b.length (current obj). tips?
Yes. Think about what you'd have do if you were adding two numbers together. What would you do on paper?
It's a very good tip to remember: Don't start coding until you understand the problem.
And if that means spending a few hours with a pencil and paper, DO IT.
Some other tips for you (but DON'T use them until you've got your current code working first):
Tip #1: You can avoid some out of bounds issues by making the array 'littleendian' (ie, reversing the order of the digits).
Tip #2: Using a whole int for each decimal digit is a bit wasteful. You could get something very similar by using base 1000.
Tip #3: BigInteger uses a storage system called 'signmagnitude' (you might want to look it up), where the sign is kept separate from the value. The nice thing about that is that you only have to worry about positive numbers in your array.
HIH
Winston
"Leadership is nature's way of removing morons from the productive flow"  Dogbert
Articles by Winston can be found here
1234567890
987654234+
xxxxxxxxxx
Can you remember how to add them? Can you remember that the biggest possible number in the result will start with 19... and will have 1 digit more than the larger operand? Can you remember about carry digits? When you remember how to do that, you will remember how to do this arithmetic.
How do you know you will need a 100element array? Why not use the same size as the input array? Why are you not cloning the array, because the clone() method on a primitive array does what you want. There is no such thing as a deep copy of a primitive. There is only a copy.Matt Wilde wrote: . . .
Paul Clapham wrote:Unorthodox? Nothing really offthewall there. It's just that beginners tend to write too much code. Example:
Another is all those 'less than or equal', 'greater than or equal... etc.
My suggestion would be to look at the java.lang.Comparable interface  a very useful pattern.
Other than that: WELL DONE...but please get out of the habit of using single character variable names. It will hurt you in the long run.
Winston
"Leadership is nature's way of removing morons from the productive flow"  Dogbert
Articles by Winston can be found here
@Campbell: Yes I heard of the compareTo()...etc. and I will look into those more tonight! Also, I at first made "b" a deep copy of "a". but when i was using the add/subtract methods...i continued to receive outofboundsexceptions...?? so i just made it all 100 length and started from one end and filtered through the zeros. easier for me to grasp.
@Winston: yes good point! the single char variables were starting to confuse me! and again...i will look into comparable.
Thanks for all you help! you guys are awesome. I'm starting to work on GUI's now so i might have a couple questions but nothin major
If you add two arrays together, the length of the resultant array is always ≤ Math.max(array1.length, array2.length) + 1.
If you subtract two arrays, the length of the resultant array is always ≤ Math.max(array1.length, array2.length).
If you multiply two arrays, the length of the resultant array is always ≤ array1.length + array2.length.
If you divide two arrays or take their remainder, the length of the resultant array is always ≤ Math.max(array1.length, array2.length).
I think those figures are correct.
They allow you to work out the length of array for the results. So if you multiply two numbers length 14 and 12 digits, you will never need an array longer than 26 elements. Create a 26element array, fill it from the multiplication, and go through from left to right looking for leading 0s. When you have found how many leading 0s there are, you can copy the array into a shorter array. That is best done in a utility method, possibly in your HugeInteger class, possibly with private access. Note utility methods are usually static because they do not alter instance fields. You should look through classes eg System., and see whether any of them have any methods for copying arrays.
You might find arithmetic easier to do if your arrays run from right to left, but they displaying will have to be done backwards.
The government thinks you are too stupid to make your own lightbulb choices. But this tiny ad thinks you are smart:
The WEB SERVICES and JAXRS Course
https://coderanch.com/t/690789/WEBSERVICESJAXRS
