Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Complete the following method that merges 2 rectangular Int32 arrays: public Int

ID: 3905029 • Letter: C

Question

Complete the following method that merges 2 rectangular Int32 arrays:

        public Int32[,] MergeArrays( Int32[,] Array1, Int32[,] Array2, MergeType Type)

        {

            Int32[,] retVal=null;

            //--------------------------------------------------------

            //Your code here!

            //--------------------------------------------------------

            return retVal;

Example: There are two arrays , array 1 has 1,2,3,3 and array 2 has 5,6,7,8 and we have to combine both array one and array two

1 2 5 6 = 1 2 5 6

3 4 7 8 3 4 7 8

Explanation / Answer

public int[] MergeArrays(int[] a1, int[] a2){

     int count1 = 0;
     for (int i = 0; i<a1.length; i++){
        for (int j = 0; j<a1.length; j++){
            if (i != j && a1[i] == a1[j]){
               a1[j] = -1;
               count1++;
            }
        }
     }
     int count2 = 0;
     for (int i = 0; i<a2.length; i++){
        for (int j = 0; j<a2.length; j++){
            if (i != j && a2[i] == a2[j]){
               a2[j] = -1;
               count2++;
            }
        }
     }
     int n = a1.length-count1 + a2.length-count2;
     int[] a3 = new int[n];
     int count = 0;
     int i = 0;
     int j = 0;
     while(i < a1.length && j < a2.length){
           if (a1[i] == -1){
               i++;
               continue;
           }
           if (a2[j] == -1){
               j++;
               continue;
           }

           if (a1[i] <= a2[j]){
              a3[count] = a1[i];
              count++;
              i++
           }
           if (a1[i] > a2[j]){
              a3[count] = a2[i];
              count++;
              j++
           }
     }
    
     while (i < a1.length){
         if (a1[i] != -1){
              a3[count] = a1[i];
              count++;
         }
         i++;
     }
     while (j < a2.length){
         if (a2[j] != -1){
              a3[count] = a2[j];
              count++;
         }
         j++;
     }
     return a3;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote