Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a method called addUnevenArrays that takes two arrays, a and b, and return

ID: 3553183 • Letter: W

Question

Write a method called addUnevenArrays that takes two arrays, a and b, and returns a new array, c, with a

length that is the maximum of the lengths of a and b. Each c[i] is the sum of the corresponding elements

of a and b if both elements exist. If one of the elements does not exist, however, c[i] is a copy of the

element from a or b that does exist. For example, given

a == {0, 1, 2, 3}, and

b == {100, 101, 102, 103, 104, 105},

then addUnevenArrays(a,b) should return the array

{100 102, 104, 106, 104, 105}.

Use the method definition below.

public static int[] addUnevenArrays(int[] a, int[] b)

Explanation / Answer


public class AddingArrays {


public static void main(String[] args) {


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

int[] b = {100, 101, 102, 103, 104, 105};

int[] c = addUnevenArrays(a, b);

for(int i=0; i<c.length; i++){

System.out.print(c[i]+",");

}

}

public static int[] addUnevenArrays(int[] a, int[] b){

int sizeOfA = a.length;

int sizeOfB = b.length;

int[] biggerLengthArray;

int minSize;

int maxSize;

if(sizeOfA>sizeOfB){

minSize = sizeOfB;

maxSize = sizeOfA;

biggerLengthArray = a;

}

else{

minSize = sizeOfA;

maxSize = sizeOfB;

biggerLengthArray = b;

}

int[] c = new int[maxSize];

for(int i=0; i<minSize; i++){

c[i] = a[i]+b[i];

}

for(int i=minSize; i<maxSize; i++){

c[i] = biggerLengthArray[i];

}

return c;

}


}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote