I've got a project that requires me to write a method to sum two integer arrays into a single integer array. The requirements are that each value of the array may only have one numeral in it. For example "1" but not "11" and the original two arrays may be different lengths. The idea behind the project is that these arrays are supposed to have lengths longer than the allowed length for an integer variable. We are supposed to create this new array so we can print out each position, essentially doing math with large numbers without using longs. This is what I currently have.
public static int[] add ( int[] arr1, int[] arr2) {
int length;
if (arr1.length >= arr2.length) {
length = arr1.length;
}
else {
length = arr2.length;
}
int[] smallAr = new int [length];
int[] bigAr = new int [length];
if (arr1.length > arr2.length) {
for (int i = 0; i < length; i++) {
smallAr[i] = arr2[i];
bigAr[i] = arr1[i];
}
}
else {
for (int i = 0; i < length; i++) {
smallAr[i] = arr1[i];
bigAr[i] = arr2[i];
}
}
int[] addition = new int[length];
for (int i = 0; i < length; i++) {
if (i > smallAr.length -1) {
addition[i] = bigAr[i];
}
else {
addition[i] = bigAr[i] + smallAr[i];
}
}
return addition;
}
This currently gives an out of bounds error when ran. I also understand that this would not properly work if the array needed an extra space for a carry. Any help on this would be great!