Java: Get two arrays and if length of one is smaller than the length of the other, replace missing values with 1 -


hi question is: if array1 smaller array2 replace missing values number 1, array1 missing values can multiplied array2 if number 1? here idea of how 1 go it... correct syntax? best way go it?

public addmissingnumber(int[] array1, int[] array2){  (int 1 = array1.length; > 0; i--)    for(int j = array2.length; j > 0; j--){        if(array1[i] < array2[j]) {          array1[i].[j] = 1;   /*what want here empty position of array1[i] equivalent position of array2[j] contains number should become number 1. how can done?*  } }  } 

here original exam question, second part i'm having trouble with: write method called add takes 2 arrays v, w of type double parameters. method should return new array of double formed adding corresponding elements of input arrays. say, element of output array should equal v[i]+w[i].

if lengths of v , w different, output array should have length maximum of (v.length, w.length). missing elements of shorter array should assumed 1 in calculation of output.

try this:

public double[] addmissingnumber(double[] array1, double[] array2){                 double[] arraynew = new double[max(array1.length,array2.length)];               int = 0;               int j = 0;               int k = 0;               while ( < 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 put 1 array. traverse through both arrays till equal , store sum . after longer array traversed , 1 added everytime.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -