Question

In: Computer Science

Write a method called splitArray which will receive four parameters as follows: Original array Array of...

Write a method called splitArray which will receive four parameters as follows:

  • Original array
  • Array of multiples
  • Array of not multiples
  • Number to compare if the values within the original array are multiples of

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]);
}
  
}

}

Solutions

Expert Solution

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]);

}

}

}


Related Solutions

Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer called removeItem.
Java programming:Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer called removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (Note that after deleting the element, the array size is reduced by 1.) You may assume that the array is unsorted.Now re-do this exercise and name it ExInsertionSort....
In Java: Write a method called copy, which is passed A[], which is an array of...
In Java: Write a method called copy, which is passed A[], which is an array of int, and an integer n. The method returns a new array consisting of the first n items in A[]. Write a method called slice, which is passed A[], which is an array of int, an integer i and an integer j. The method returns a new array consisting of all of the items in A from A[i] to A[j] inclusive.
In PYTHON Write an algorithm for a function called removeAll which takes 3 parameters: an array...
In PYTHON Write an algorithm for a function called removeAll which takes 3 parameters: an array of array type, a count of elements in the array, and a value. As with the remove method we discussed in class, elements passed the count of elements are stored as None. This function should remove all occurrences of value and then shift the remaining data down. The last populated element in the array should then be set to None. The function then returns...
Write a Java method that takes an array of char and a String as input parameters...
Write a Java method that takes an array of char and a String as input parameters and and returns an boolean. The method returns true if we can find the input string inside the array by starting at any position of the array and reading either forwards or backwards.
Write a function called printChList that takes as its parameters a character array, its size, and...
Write a function called printChList that takes as its parameters a character array, its size, and output file stream. The function should print the contents of the array to the output file. code with c++ and detail explaination
Write a function called fillList that takes three parameters, an integer array, input file, and size....
Write a function called fillList that takes three parameters, an integer array, input file, and size. The function should fill the integer array with randomly generated values between two numbers lowLim and highLim read from the input file. in C++
#Write a function called wish_list. wish_list should have #four parameters, in this order: # # -...
#Write a function called wish_list. wish_list should have #four parameters, in this order: # # - a list of strings, representing a list of items on a # wish list # - a string, representing a particular item # - a float, representing the cost of this item # - a float, representing your budget # #If the item is on the list and you can afford it (cost is #less than or equal to budget), return the string, #"You...
Write a method which is passed A[], which is an array of int, and an int...
Write a method which is passed A[], which is an array of int, and an int passingScore. The method returns the number of items in A[] which are greater than or equal to passingScore. Write a method which is passed an array of int A[]. The method returns true if A[] is the same backwards and forwards. Write a method same( ), which is passed two arrays of int. The method returns true if the two arrays contain exactly the...
In C++ Write a function which takes two parameters: an array of ints and an int...
In C++ Write a function which takes two parameters: an array of ints and an int size of the array and prints every element greater than 5 to the screen. As an example, if the array has the following 10 elements: 2 5 8 9 7 1 0 2 6 3, your function should print out 8 9 7 6. You may assume that the parameters passed to the function are valid. Your function must have the following signature: void...
Part A) Java Programming Exercise #2: Write a method, remove, that takes three parameters: an array...
Part A) Java Programming Exercise #2: Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer, say, removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (Note that after deleting the element, the array size is reduced by 1.) Here you assume the array is not sorted. Do not...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT