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
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
ADVERTISEMENT