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

I want to get the symmetric difference of two arrays in c++ . The symmetric diff

ID: 3790822 • Letter: I

Question

I want to get the symmetric difference of two arrays in c++ . The symmetric difference is the set of objects in : list1 union list2 minus the objects in list1 intersection of list2.
eg: If A = {1, 2, 3, 4, 5, 6, 7, 8} and B = {1, 3, 5, 6, 7, 8, 9}, then A union B = {1,2,3,4,5,6,7,8,9}, B difference A = {1,3,5,6,7,8} then A B = {2, 4, 9}
so i have implemented both the union and difference function and they work properly with ordered arrays, but i just failed on how to implement the setSymmetricDifference? by calling both functions inside the new function and return the correct values
here is union and intersection functions:
int setUnion (int list1[], int list2[], int m, int n) {

int i = 0, j = 0; while(i < m && j < n) {      
if (list1[i] < list2[j]) cout << list1[i++]<<","; else if (list2[j] < list1[i]) cout << list2[j++]<<","; else { cout << list2[j++]<<","; /* if list1[i] == list2[j] */ i++; } } while (i < m) cout << list1[i++]<< ","; while (j < n) cout << list2[j++]<< ","; }
int setIntersection (int list1[], int list2[], int m, int n) { int i = 0, j = 0; while(i < m && j < n) { if (list1[i] < list2[j]) i++; else if (list2[j] < list1[i]) j++;
else /* if list1[i] == list2[j] */ { cout << list2[j++]<<","; i++; } } } I want to get the symmetric difference of two arrays in c++ . The symmetric difference is the set of objects in : list1 union list2 minus the objects in list1 intersection of list2.
eg: If A = {1, 2, 3, 4, 5, 6, 7, 8} and B = {1, 3, 5, 6, 7, 8, 9}, then A union B = {1,2,3,4,5,6,7,8,9}, B difference A = {1,3,5,6,7,8} then A B = {2, 4, 9}
so i have implemented both the union and difference function and they work properly with ordered arrays, but i just failed on how to implement the setSymmetricDifference? by calling both functions inside the new function and return the correct values
here is union and intersection functions:
int setUnion (int list1[], int list2[], int m, int n) {

int i = 0, j = 0; while(i < m && j < n) {      
if (list1[i] < list2[j]) cout << list1[i++]<<","; else if (list2[j] < list1[i]) cout << list2[j++]<<","; else { cout << list2[j++]<<","; /* if list1[i] == list2[j] */ i++; } } while (i < m) cout << list1[i++]<< ","; while (j < n) cout << list2[j++]<< ","; }
int setIntersection (int list1[], int list2[], int m, int n) { int i = 0, j = 0; while(i < m && j < n) { if (list1[i] < list2[j]) i++; else if (list2[j] < list1[i]) j++;
else /* if list1[i] == list2[j] */ { cout << list2[j++]<<","; i++; } } } I want to get the symmetric difference of two arrays in c++ . The symmetric difference is the set of objects in : list1 union list2 minus the objects in list1 intersection of list2.
eg: If A = {1, 2, 3, 4, 5, 6, 7, 8} and B = {1, 3, 5, 6, 7, 8, 9}, then A union B = {1,2,3,4,5,6,7,8,9}, B difference A = {1,3,5,6,7,8} then A B = {2, 4, 9}
so i have implemented both the union and difference function and they work properly with ordered arrays, but i just failed on how to implement the setSymmetricDifference? by calling both functions inside the new function and return the correct values
here is union and intersection functions:
int setUnion (int list1[], int list2[], int m, int n) {

int i = 0, j = 0; while(i < m && j < n) {      
if (list1[i] < list2[j]) cout << list1[i++]<<","; else if (list2[j] < list1[i]) cout << list2[j++]<<","; else { cout << list2[j++]<<","; /* if list1[i] == list2[j] */ i++; } } while (i < m) cout << list1[i++]<< ","; while (j < n) cout << list2[j++]<< ","; }
int setIntersection (int list1[], int list2[], int m, int n) { int i = 0, j = 0; while(i < m && j < n) { if (list1[i] < list2[j]) i++; else if (list2[j] < list1[i]) j++;
else /* if list1[i] == list2[j] */ { cout << list2[j++]<<","; i++; } } }

Explanation / Answer

#include<iostream>
using namespace std;

int setUnion (int list1[], int list2[], int m, int n,int *l3)
{

int count=0;

int i = 0, j = 0;
while(i < m && j < n)
{
  
  

if (list1[i] < list2[j])
l3[count++]=list1[i++];
else if (list2[j] < list1[i])
   l3[count++]=list2[j++];
else
{
   l3[count++]=list2[j++]; i++;
}
}

while (i < m)
l3[count++]=list1[i++];
while (j < n)
l3[count++]=list2[j++];

return count;
}

int setIntersection (int list1[], int list2[], int m, int n, int *l3)
{
int count=0;

int i = 0, j = 0;
while(i < m && j < n)
{
if (list1[i] < list2[j])
i++;
else if (list2[j] < list1[i])
j++;

else /* if list1[i] == list2[j] */
{
l3[count++]=list2[j++]; i++;
}
}
return count;
}

int setSymDif(int l1[], int l2[],int m,int n,int* l3)
{
int count=0;

int i=0,j=0;
bool flag;

for(;i<m;i++)
{
   flag=false;
   for(j=0;j<n;j++)
   {
       if(l1[i]==l2[j]){
           flag=true;
           break;
       }
   }

   if(!flag) l3[count++]=l1[i];
}

return count;
  
}

int main(void)
{

int l1[]={1,2,3,4,5,6,7,8};
int l2[]={1,3,5,6,7,8,9};
int l3[20], l3_count;
int l4[20],l4_count;
int l5[20],l5_count;

l3_count=setUnion(l1,l2,8,7, l3);
l4_count=setIntersection(l1,l2,8,7,l4);

cout<<"Union"<<endl;
for(int i=0;i<l3_count;i++)
   cout<<l3[i]<<" ";

cout<<endl;

cout<<"Intersection"<<endl;
for(int i=0;i<l4_count;i++)
   cout<<l4[i]<<" ";
cout<<endl;

cout<<"Symmetric Difference"<<endl;
l5_count=setSymDif(l3,l4,l3_count,l4_count,l5);

for(int i=0;i<l5_count;i++)
   cout<<l5[i]<<" ";
cout<<endl;

return 0;
}

Output:

Union
1 2 3 4 5 6 7 8 9
Intersection
1 3 5 6 7 8
Symmetric Difference
2 4 9

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