0

So i am currently working on a project for a course that i am doing, and i am writing a method that needs to add two ArrayLists containing integers together, and sets the sum in a new ArrayList. currently i have this, which is working fine

    public BigInt add(BigInt otherBigInt) {
    BigInt result = new BigInt();
            int length = digitList.size() - 1;
            int lengthOther = otherBigInt.digitList.size() - 1;
            int temp = 0;
            int whole = 0;
            int carry = 0;



    for (int i = length; i >= 0; i--){
        temp = (digitList.get(i) + otherBigInt.digitList.get(i));
        temp += carry;
        // temp is equal to the sum of this(i) and otherBigInt(i), plus any number carried over.
        if (temp >= 10){
            whole = temp - 10;
            carry = 1;

            result.digitList.add(whole); 
        }
        else if (temp < 10){
            carry = 0;
            result.digitList.add(temp);
        }
    }

    if (carry == 1){
        result.digitList.add(carry);
    }
    //adds any remaining carried number after for loop
    // Supply this code.

    return result;
}

however, currently the method only works if the arrayLists are of the same size, how would i modify the method to accommodate lists of varying size? an example of this would be two lists containing 735934 and 68945 giving the result 804879.

p.s. not sure if it's needed, (still new to posting here) but the two lists I'm currently adding are 7359 and 6894, giving the answer 14253.

7
  • could you please post an example of the required case, the algorithm and the expected result. Commented May 30, 2015 at 13:49
  • we arent given any required case in the assignment brief, just that it needs to take lists of differing sizes. will post edit the post now with an example Commented May 30, 2015 at 13:53
  • One list have one element 7359 and the other list have the 6894? And there's a third list that will have 14253? Commented May 30, 2015 at 14:07
  • What should happen if the first list has 3 elements and the second list has 1? Commented May 30, 2015 at 14:08
  • in the case of first list having 3 elements e.g. 321 and the second having 1 element eg. 5 you should end up with 326 Commented May 30, 2015 at 14:10

3 Answers 3

0

If my assumption is correct, then you are trying to simulate a case where you add two numbers using two lists, where one digit of the number occupies one index of the list.

Simplest solution would be to assume that the shortest list has zero's instead of no value and add till the max of the two lists:

public BigInt add(BigInt otherBigInt) {
    BigInt result = new BigInt();
    int length = Math.max(digitList.size(), otherBigInt.digitList.size()) - 1;
    int lengthOther = otherBigInt.digitList.size() - 1;
    int temp = 0;
    int whole = 0;
    int carry = 0;

    for (int i = length; i >= 0; i--){
        temp = ( checkAndGet(digitList, i) + checkAndGet(otherBigInt.digitList, i) );
        temp += carry;
        // temp is equal to the sum of this(i) and otherBigInt(i), plus any number carried over.
        if (temp >= 10) {
            whole = temp - 10;
            carry = 1;

            result.digitList.add(whole); 
        }
        else {
            carry = 0;
            result.digitList.add(temp);
        }
    }

    if (carry == 1){
        result.digitList.add(carry);
    }

    //adds any remaining carried number after for loop
    // Supply this code.

    return result;
}

// if the index position being retrieved is larger than the size, assume 0    
private int checkAndGet(List<Integer> input, position) {
    return (input.size() < position) ? input.get(position) : 0;
}
Sign up to request clarification or add additional context in comments.

3 Comments

i'm a bit hesitant to assume things that arent specified in the brief, however i suppose i could make a separate method that adds zeros until their sizes are equal.
You do not have to zero fill the list, you just have to check if value at index is present and the use 0 if it is not present
i added my own version of your idea. at first glance it all seems to be working, will test it further tomorrow morning, thanks for your help!
0

Let the two digit array be a1[0 ... n1] and a2[0 ... n2]. Now your algorithm would be something like :

add(a1, a2):
   min_length = min{a1.length, a2.length}
   result[0 ... max{a1.length, a2.length} + 1]
   carry = 0
   for(i in [0 ... min_length - 1])
       result[i] = carry + a1[i] + a2[i] 
       carry = result[i] / 10 
       result[i] %= 10
   while(i < a1.length)
       result[i] = a1[i] 
   while(i < a2.length)
       result[i] = a2[i] 
   result[i] = carry

Notice that you have to add the remaining digits in case they are not of same size. I assume that the digits are stored in order i.e. a[0] is the 1st digit.

1 Comment

no, the digits are not stored in the same order. there is no assumed order or requirements for digitList. however it does go through a separate method to remove trailing zeros, so you can assume that there are no trailing zeros
0

I would iterate through the smaller array backwards, adding the indexes together as you go:

ArrayList<Integer> toIterate = (array1.size() > array2.size)? a1 : a2;
ArrayList<Integer> seperate = (array1.size() > array2.size)? a2 : a1;

for (int i = toIterate.size - 1; i >= 0; i --) {
if (seperate.get(i) != null) {
arrayResult.add(toIterate.get(i) + seperate.get(i));
}
else {

arrayResult.add(toIterate.get(i));

}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.