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

Assume we already have a Bag Array class. The goal of this program is to write t

ID: 3836182 • Letter: A

Question

Assume we already have a Bag Array class. The goal of this program is to write the main code. You must declare two Bag Array objects of type Integer. Place some integer values in the first Bag Array object and some of the numbers maybe duplicates. After the numbers placed in the first Bag Array, transfer all the numbers from the first Bag Array to the second Bag Array without having any duplicates in the second Bag Array. Note that after this transfer, the first Bag Array should still have all the items placed in it originally.

Explanation / Answer

C++ code:

#include <bits/stdc++.h>
using namespace std;

int main()
{
   int n = 10;
   int Bagarray1[n];
   int Bagarray2[n];  

   cout << "Bagarray 1 is ";
   for (int i = 0; i < n; ++i)
   {
       Bagarray1[i] = rand()%n + 1;
       cout << Bagarray1[i] << " ";      
   }
   cout << endl;
//Remove Duplicates
   int array2size = 0;
   for (int i = 0; i < n; ++i)
   {
       bool flag = true;
       for (int j = 0; j < array2size; ++j)
       {
           if(Bagarray2[j] == Bagarray1[i])
           {
               flag = false;
               break;
           }
       }
       if(flag == true)
       {
           Bagarray2[array2size] = Bagarray1[i];
           array2size++;
       }
   }

   cout << "Bagarray 2 is ";
   for (int i = 0; i < array2size; ++i)
   {
       cout << Bagarray2[i] << " ";      
   }  
   cout << endl;
   return 0;
}

Sample Output:

Bagarray 1 is
4 7 8 6 4 6 7 3 10 2
Bagarray 2 is
4 7 8 6 3 10 2