*
The moose likes Beginning Java and the fly likes Adding 2 int arrays together Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Adding 2 int arrays together" Watch "Adding 2 int arrays together" New topic
Author

Adding 2 int arrays together

Matt Wilde
Ranch Hand

Joined: Feb 27, 2012
Posts: 34
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 {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?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18523
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Matt Wilde
Ranch Hand

Joined: Feb 27, 2012
Posts: 34
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
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18523
    
  40

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
Matt Wilde
Ranch Hand

Joined: Feb 27, 2012
Posts: 34
yes i would use BigInteger in a heartbeat if it weren't that we had to use arrays.
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?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7545
    
  18

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 out-of-bounds 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 'little-endian' (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 'sign-magnitude' (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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38001
    
  22
You need to do it exactly as you would do it on paper. Do you remember when you were at primary school and had to learn addition? You had exercises like
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38001
    
  22
Another way to work out signed arithmetic would be to use ten’s complement.
Matt Wilde
Ranch Hand

Joined: Feb 27, 2012
Posts: 34
@ Winston and Campbell: Thanks so much for your input it REALLY did help.

this is my completed code/assignment for full points. Some of these methods may be unorthodox to you people, but whatever works

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Unorthodox? Nothing really off-the-wall there. It's just that beginners tend to write too much code. Example:


Less wordy code:
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38001
    
  22
Unless you have been told to write an isNotEqualTo() method, you should use !myHugeInteger.isEqualTo(yourHugeInteger);. Those lessThan methods should probably be replaced by compareTo() because your class is a prime candidate for implementing the Comparable<T> interface.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38001
    
  22
Matt Wilde wrote: . . .
How do you know you will need a 100-element 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.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7545
    
  18

Paul Clapham wrote:Unorthodox? Nothing really off-the-wall 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
Matt Wilde
Ranch Hand

Joined: Feb 27, 2012
Posts: 34
@ Paul: thanks for the example. There are always ways I need to learn to shorten the code I write.
@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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38001
    
  22
Your input arrays are identical to the field arrays. Explore clone(), which will give a copy containing the same ints. If you pass an array like new int{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5,}, the field will have exactly the same length.
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 26-element 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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding 2 int arrays together
 
Similar Threads
Byte Manipulation
Reg :- Java Multi Dimension Array
arrays
Array Questions
Question on Multidimensional Array