code here for getting the union and intersection of two sorted arrays. My problem is, i am a bit confused on the difference and the symmetric difference of those two arrays. Please help me with the difference and correct me if i made a mistake on the symmetric one. I would greatly appreciate it, thank you! :) Here's my code: void getUnion(int s1[], int count1, int s2[], int count2) { printf("\n\nARRAY SETS: \n"); printf("\nArray1: {1, 3, 5, 6, 8}\n"); printf("\nArray2: {2, 3, 5, 7}\n"); printf("\n---------------------\n"); printf("\nUNION\n"); int a = 0, b = 0; printf("\nThe union of two arrays are: \n"); while(a < count1 && b < count2) { if (s1[a] < s2[b]) printf("%d ", s1[a++]); else if (s2[b] < s1[a]) printf("%d ", s2[b++]); else { printf("%d ", s2[b]); a++; b++; } } while(a < count1) printf("%d ", s1[a++]); while(b < count2) printf("%d ", s2[b++]); } void intersection(int *s1, int count1, int *s2, int count2) { printf("\n\nINTERSECTION\n"); printf("\nIntersection of two arrays are: \n\n"); int c = 0, d = 0; while(c < count1 && d < count2) { if (s1[c] < s2[d]) c++; else if (s2[c] < s1[d]) d++; else { printf(" %d ", s2[c]); c++; d++; }}} void symmetricDifference(int *s1, int *s2, int count1, int count2) { printf("\n\nSYMMETRIC DIFFERENCE\n"); printf("\nDifference of two arrays are: \n\n"); int i = 0, j = 0; while (i < count1 && j < count2) { if (s1[i] < s2[j]) { printf("%d ", s1[i]); i++; } else if (s2[j] < s1[i]) { printf("%d ", s2[j]); j++; } else { i++; j++; } } } int main(){ //get the union of two arrays getUnion(s1, 5, s2, 4); //get the intersection of two arrays intersection(s1, 5, s2, 4); //difference //-------------------code //symmetric difference symmetricDifference(s1, s2, 5, 4); }
Hello, i have a code here for getting the union and intersection of two sorted arrays. My problem is, i am a bit confused on the difference and the symmetric difference of those two arrays. Please help me with the difference and correct me if i made a mistake on the symmetric one. I would greatly appreciate it, thank you! :)
Here's my code:
void getUnion(int s1[], int count1, int s2[], int count2) {
printf("\n\nARRAY SETS: \n");
printf("\nArray1: {1, 3, 5, 6, 8}\n");
printf("\nArray2: {2, 3, 5, 7}\n");
printf("\n---------------------\n");
printf("\nUNION\n");
int a = 0, b = 0;
printf("\nThe union of two arrays are: \n");
while(a < count1 && b < count2) {
if (s1[a] < s2[b])
printf("%d ", s1[a++]);
else if (s2[b] < s1[a])
printf("%d ", s2[b++]);
else {
printf("%d ", s2[b]);
a++;
b++;
}
}
while(a < count1)
printf("%d ", s1[a++]);
while(b < count2)
printf("%d ", s2[b++]);
}
void intersection(int *s1, int count1, int *s2, int count2) {
printf("\n\nINTERSECTION\n");
printf("\nIntersection of two arrays are: \n\n");
int c = 0, d = 0;
while(c < count1 && d < count2) {
if (s1[c] < s2[d])
c++;
else if (s2[c] < s1[d])
d++;
else {
printf(" %d ", s2[c]);
c++;
d++;
}}}
void symmetricDifference(int *s1, int *s2, int count1, int count2)
{
printf("\n\nSYMMETRIC DIFFERENCE\n");
printf("\nDifference of two arrays are: \n\n");
int i = 0, j = 0;
while (i < count1 && j < count2)
{
if (s1[i] < s2[j])
{
printf("%d ", s1[i]);
i++;
}
else if (s2[j] < s1[i])
{
printf("%d ", s2[j]);
j++;
}
else
{
i++;
j++;
}
}
}
int main(){
//get the union of two arrays
getUnion(s1, 5, s2, 4);
//get the intersection of two arrays
intersection(s1, 5, s2, 4);
//difference
//-------------------code
//symmetric difference
symmetricDifference(s1, s2, 5, 4);
}
Step by step
Solved in 2 steps with 1 images