• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Adding 2 int arrays together

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Matt Wilde
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another way to work out signed arithmetic would be to use ten’s complement.
 
Matt Wilde
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ 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

 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unorthodox? Nothing really off-the-wall there. It's just that beginners tend to write too much code. Example:


Less wordy code:
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ 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
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic