wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes How to add two elements of two arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to add two elements of two arrays" Watch "How to add two elements of two arrays" New topic
Author

How to add two elements of two arrays

Priya Ahuja
Greenhorn

Joined: Mar 22, 2004
Posts: 2
I don't know how easy or hard this is, but I am so confused and cannot think of it.
I have two arrays in reverse order.
So basically
Array a[]=321 ( ie the number is 123)
Array b[] = 9001 (i.e the number is 1009)
So I have to make a add method which accepts these two arrays as arguments.
Then I add those two numbers and return the value in string or an array, doesn't matter.
How do I do add and subtract method for this problem. As I have to make sure about the carry digit as if we add 9 and 3 its 12, so 1 is carry or so.
I hope you guys/gals understand my problem and if you have any questions then please let me know
Thanks in advance
Priya


Help this poor girl...
Ravi Srinivas
Greenhorn

Joined: Sep 04, 2003
Posts: 26
When you say Array a[]=321 , Array b[] = 9001 do you mean
a[0] = 3, a[1] = 2, a[2] = 1
b[0] = 9 , b[1] = 0, b[2] = 0, b[3] = 1 ?
And what is Array? Is it a class you've defined? Could you just use int[] instead?
[ March 22, 2004: Message edited by: Ravi Srinivas ]
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
321 and 9001 are elements of an array?
If so, something like this perhaps (output is 1132)
Priya Ahuja
Greenhorn

Joined: Mar 22, 2004
Posts: 2
Thanks Michael
How about for the subtraction...
Do I have to do just change the sign like below
return Integer.parseInt(sb1.reverse().toString()) - Integer.parseInt(sb2.reverse().toString());
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
The best way to find out is by testing it.
 
 
subject: How to add two elements of two arrays
 
Similar Threads
sum of two int arrays
maintaining a set of arrays of doubles
Getting dimensions of Array[][]
Binary numbers
Algoritme for searching differnce in array's