Question

Write a method that takes 3 double arrays, merges all 3 arrays and returns a new...

Write a method that takes 3 double arrays, merges all 3 arrays and returns a new array which stores all the values of the 3 arrays.

Please use Java and comment the code thank you

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Java program

public class ThreeMerge {
   public static void main(String args[]) {
       double arr1[] = {1,3};
       double arr2[] = {2,5};
       double arr3[] = {4,8};
      
       double arr[] = mergeThree(arr1,arr2,arr3);
      
       for(int i=0;i<arr.length;i++)
           System.out.print(arr[i]+" ");
   }
//function to merge three array
   private static double[] mergeThree(double[] arr1, double[] arr2, double[] arr3) {
       int l1=arr1.length;
       int l2 = arr2.length;
       int l3 = arr3.length;
       double A[] = new double[l1+l2];
       merge(arr1,arr2,A);
       double B[] = new double[l1+l2+l3];
       merge(arr3,A,B);
       return B;
   }
  
   //function to merge two array at a time into third array
   public static void merge(double [] A ,double B[],double C[]) {
  
   int first1= 0;
   int last1= A.length;
   int first2 = 0;
   int last2 = B.length;
   int index=0;
  
   while(first1<last1 && first2<last2) {
      
       if(A[first1]<B[first2]) {
           C[index] = A[first1];
           first1++;
       }
       else {
           C[index] = B[first2];
           first2++;
          
       }
       index++;
      
   }
   while(first1<last1) {
       C[index] = A[first1];
           first1++;
      
   }
   while(first2<last2) {
       C[index] = B[first2];
           first2++;
   }
  
}
}
//sample output

Add a comment
Know the answer?
Add Answer to:
Write a method that takes 3 double arrays, merges all 3 arrays and returns a new...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1.Write code for a Java method, printArray, that takes an int array as parameter and prints...

    1.Write code for a Java method, printArray, that takes an int array as parameter and prints it out, one element per line. public static void printArray (int[] values){ Methods that do NOT return a result have “void” as return type. Notice how an array parameter type is passed, int [] }// end printArray 2.Write code for a Java method that takes in as input parameter an integer number, say num, and returns a double array of size num with all...

  • Write a method booleanisValid() that takes a list of 3 boolean values as argument and returns...

    Write a method booleanisValid() that takes a list of 3 boolean values as argument and returns true if any of the entries in the array is true, and false otherwise. This can be done in 4 lines (2 of the lines are { and }   )               Fill in the Solution:                              public static boolean isValid(______ _______, ______ _______, ______ _______)                              {                                 return(true);                              } Java language, thank you.

  • Write a java method merge that accepts two arrays of integers and returns a new array...

    Write a java method merge that accepts two arrays of integers and returns a new array containing all elements of the first array followed by all elements of the second. int[] a1 = {12, 34, 56}; int[] a2 = {7, 8, 9, 10};   int[] a3 = merge(a1, a2); System.out.println(Arrays.toString(a3)); // [12, 34, 56, 7, 8, 9, 10]

  • 3. Write Java methods to accomplish each of the following Write a method named simpleAry which...

    3. Write Java methods to accomplish each of the following Write a method named simpleAry which accepts and return nothing. It creates an array that can hold ten integers. Fill up each slot of the array with the number 113. Then, display the contents of the array on the screen. You must use a loop to put the values in the array and also to display them. Write a method named sury which accepts an array of type double with...

  • JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a...

    JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...

  • write a c++ program that merges two arrays a and b. You may use the following...

    write a c++ program that merges two arrays a and b. You may use the following array contents A=5,7,8,6,3,3 and B=4,5,6,1,2,3.The merged array should exclude all repeating numbers

  • Write a static method that takes an array of Strings and returns a double. Determine the...

    Write a static method that takes an array of Strings and returns a double. Determine the average number of characters for the Strings assigned to the array. Return the average.

  • Write a method named avgLength which takes an array of Strings as an input parameter and...

    Write a method named avgLength which takes an array of Strings as an input parameter and returns a double. The method should calculate and return the average length of all the strings in the array (in java langauge)

  • Need help with java code, this is all in a method 1) 3 different arrays are...

    Need help with java code, this is all in a method 1) 3 different arrays are brought in 2) I create local array called arrayWithLargestValues which is the size of the largest array from the 3 brought in 3) I need to somehow fill the local 'arrayWithLargestValues' with the all different largest values from the 3 different arrays hopefully I made sense sample input/output int [ ] arrayWithLargestValues = new int [ ] // this has to be set to...

  • Write a method public static ArrayList merge(ArrayList a, ArrayList b) that merges two array lists, alternating...

    Write a method public static ArrayList merge(ArrayList a, ArrayList b) that merges two array lists, alternating elements from both array lists. If one array list is shorter than the other, then alternate as long as you can and then append the remaining elements from the longer array list. For example, if a is 1 4 9 16 and b is 9 7 4 9 11 then merge returns the array list 1 9 4 7 9 4 16 9 11...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT