In: Computer Science
Write a method called splitArray which will receive four parameters as follows:
Assuming that always the original array will contain half values as multiples and half as not multiples. Your method should return the numbers within the original array that are multiples of the last parameter in the array of multiples (second parameter). Furthermore, your method should return the array of not multiples in the third parameter received.
As an example, if the input was 6 7 8 9 3 6 10 3. The Array of multiples would be conformed as 9 3 6 and the array of not multiples would be conformed by 7 8 10. And the output should be as follows.
Multiple[0] = 9 Multiple[1] = 3 Multiple[2] = 6 Not multiple[0] = 7 Not multiple[1] = 8 Not multiple[2] = 10
Keep in mind that you will create the other arrays and they will start without having any element
import java.util.*;
public class Main{
public static void splitArray(int[] ori, int[] mul, int[] not, int
val){
// Fill your multiple and not multiple arrays here based on the
original array
}
public static void main(String[] args){
Scanner scnr = new Scanner(System.in);
int oSize = scnr.nextInt();
int[] original = new int[oSize]; // Declaring the original
array
int[] multiple = new int[oSize/2]; // Declaring the array of
multiple numbers
int[] notMultiple = new int[oSize/2]; // Declaring the array of not
multple numbers
int value;
// Getting the values for the original array
for (int i = 0; i < oSize; i++){
original[i] = scnr.nextInt();
}
value = scnr.nextInt();
splitArray(original, multiple, notMultiple, value);
for (int i = 0; i < oSize / 2; i++){
System.out.printf("Multiple[%d] = %d%n", i, multiple[i]);
}
for (int i = 0; i < oSize / 2; i++){
System.out.printf("Not multiple[%d] = %d%n", i,
notMultiple[i]);
}
}
}
If you have any doubts, please give me comment...
import java.util.*;
public class Main{
public static void splitArray(int[] ori, int[] mul, int[] not, int val){
// Fill your multiple and not multiple arrays here based on the original array
int j=0, k=0;
for(int i=0; i<ori.length; i++){
if(ori[i]%val==0){
mul[j] = ori[i];
j++;
}
else{
not[k] = ori[i];
k++;
}
}
}
public static void main(String[] args){
Scanner scnr = new Scanner(System.in);
int oSize = scnr.nextInt();
int[] original = new int[oSize]; // Declaring the original array
int[] multiple = new int[oSize/2]; // Declaring the array of multiple numbers
int[] notMultiple = new int[oSize/2]; // Declaring the array of not multple numbers
int value;
// Getting the values for the original array
for (int i = 0; i < oSize; i++){
original[i] = scnr.nextInt();
}
value = scnr.nextInt();
splitArray(original, multiple, notMultiple, value);
for (int i = 0; i < oSize / 2; i++){
System.out.printf("Multiple[%d] = %d%n", i, multiple[i]);
}
for (int i = 0; i < oSize / 2; i++){
System.out.printf("Not multiple[%d] = %d%n", i, notMultiple[i]);
}
}
}