Hi so the question is: if array1 is smaller than array2 replace the missing values with number 1, so that array1 missing values can be multiplied by array2 as if they were the number 1? Here is my idea of how one could go about it... help with the correct syntax? Best way to go about it?
public addMissingNumber(int[] array1, int[] array2){
for (int 1 = array1.length; i > 0; i--)
for(int j = array2.length; j > 0; j--){
if(array1[i] < array2[j]) {
array1[i].[j] = 1;
/*what I want to say here is that the empty position of the array1[i] that is equivalent to the position of array2[j] that contains a number should become the number 1. How can this be done?*
}
}
}
Here is the original exam question, the second part is what I'm having trouble with: Write a method called add that takes two arrays v, w of type double as its parameters. The method should return a new array of double formed by adding the corresponding elements of the input arrays. That is to say, element i of the output array should be equal to v[i]+w[i].
If the lengths of v and w are different, the output array should have as length the maximum of (v.length, w.length). The missing elements of the shorter array should be assumed to be one in the calculation of the output.
Try this:
public double[] addMissingNumber(double[] array1, double[] array2){
double[] arraynew = new double[max(array1.length,array2.length)];
int i = 0;
int j = 0;
int k = 0;
while ( i < array1.length && j < array2.length) {
arraynew[k] = array1[i] + array2[j];
i++;
j++;
k++
}
while(i < array1.length) {
arraynew[k] = array1[i]+1;
i++;
k++;
}
while(j < array2.length){
arraynew[k] = array2[j]+1;
j++;
k++;
}
return arraynew;
}
You don't need to put 1 into the array. Just traverse through both the arrays till they are equal and store the sum . After that the longer array is traversed and 1 is added to it everytime.