Solved

Add and Subtract Elements of two arrays

Posted on 2004-03-22
9
352 Views
Last Modified: 2008-02-26
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)

I want Array a[] + Array b[] (means 123 + 1009) = 1132

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
0
Comment
Question by:Guru Ji
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 10654521
You mean you want to return 1132?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10654524
Sorry - must be getting tired - i see you do ;-)
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 65 total points
ID: 10654532
StringBuffer sb1 = new StringBuffer(arr1.length);
for(int i = 0;i < arr1.length;i++) {
  sb1.insert(0, arr1[i]);
}

StringBuffer sb2 = new StringBuffer(arr2.length);
for(int i = 0;i < arr2.length;i++) {
  sb2.insert(0, arr2[i]);
}

int sum = Integer.parseInt(sb1.toString()) + Integer.parseInt(sb2.toString());
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 10654535
Of course you could arrange things so that you only need one loop, called twice
0
 
LVL 13

Accepted Solution

by:
Webstorm earned 60 total points
ID: 10655936
Hi write2mohit,

If you mean array of integer value :

int[] a={3,2,1};
int[] b={9,0,0,1};

int digit(int[] a,int n)
{
    return ((n<0)||(n>=a.length))?0:a[n];
}

int[] add(int[] a,int[] b)
{
    int ml=(b.length>a.length)?b.length:a.length; // maximum array length
    int[] res=new int[ml+1]; // +1 to store the carry digit
    int c=0; // carry
    for (int i=0;i<res.length;i++)
    {
          res[i]=digit(a,i)+digit(b,i)+c;
          c=res[i]/10;
          res[i]%=10;
    }
    return res;
}


0
 
LVL 13

Expert Comment

by:Webstorm
ID: 10655944
unsigned substraction (a must be >= b) :

int[] substract(int[] a,int[] b)
{
    int ml=(b.length>a.length)?b.length:a.length; // maximum array length
    int[] res=new int[ml];
    int c=0; // carry
    for (int i=0;i<res.length;i++)
    {
          res[i]=digit(a,i)-digit(b,i)-c+10; // +10 for %
          c=1-(res[i]/10);
          res[i]%=10;
    }
    return res;
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10656453
>>If you mean array of integer value :

The code i already posted will work with an array of int or String
0
 
LVL 11

Author Comment

by:Guru Ji
ID: 10661602
Yes I tried everything

@CEHJ  your code works great... Thanks

@Webstorm - Your code is great too, I will use it for subtraction

Will assign points when I will be done

Thanks to everyone
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10793906
8-)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SHA2 certs for IIS AND Java? 2 122
How do I remove an object from a 3 51
How to log java errors in tomcat 8 24
How to fix  socket closed error 11 22
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question