In: Computer Science
Don't use vectors use pointers ,classes & objects, functions and loop etc only
C++ PROGRAM
Following is a partial implementation of Set class.
You are required to enhance and implement the
following missing functions from the implementation:
A) UNION
B) reset
C) intersection
D) difference
A SAMPLE driver program :
int a1[] = {10,5,7,3,9};
Set s1(5);
s1.insert(a1,5);
s1.print("s1");
int a2[] = {2,9,6};
Set s2(3);
s2.insert(a2,3);
s2.print("s2");
Set s3 = s1.unionset(s2);
Set s4 = s1.intersection(s2);
Set s5 = s1.difference(s2);
s3.print("s3");
s4.print("s4");
s5.print("s5");
N.B
Don't use vectors use pointers ,functions and loop only
do display output
DO ADD COMMENTS WITH EACH LINE FOR BETTER UNDERSTANDING
C++ by calling function,loops,etc and without using vectors as mentioned
here is the which i have written
I have provided code and output of the code for proof just run and check it out
Sorry for output its not good enough to see but its correct for sure
#include <bits/stdc++.h>
using namespace std;
/* Function prints union of arr1[] and arr2[]
m is the number of elements in arr1[]
n is the number of elements in arr2[] */
int printUnion(int arr1[], int arr2[], int m, int n )
{
int i = 0, j = 0;
while (i < m && j < n) {
if (arr1[i] < arr2[j])
cout << arr1[i++] << " ";
else if (arr2[j] < arr1[i])
cout << arr2[j++] << " ";
else {
cout << arr2[j++] << " ";
i++;
}
}
/* Print remaining elements of the larger array */
while (i < m)
cout << arr1[i++] << " ";
while (j < n)
cout << arr2[j++] << " \n";
}
int printIntersection(int arr1[], int arr2[], int m, int n)
{
int i = 0, j = 0;
while (i < m && j < n) {
if (arr1[i] < arr2[j])
i++;
else if (arr2[j] < arr1[i])
j++;
else /* if arr1[i] == arr2[j] */
{
cout << arr2[j] <<" \n";
i++;
j++;
}
}
}
void symmDiff(int arr1[], int arr2[], int n, int m)
{
// Traverse both arrays simultaneously.
int i = 0, j = 0;
while (i < n && j < m)
{
// Print smaller element and move
// ahead in array with smaller element
if (arr1[i] < arr2[j])
{
cout << arr1[i]<<" ";
i++;
}
else if (arr2[j] < arr1[i])
{
cout << arr2[j]<<" ";
j++;
}
// If both elements same, move ahead
// in both arrays.
else
{
i++;
j++;
}
}
}
/* Driver program to test above function */
int main()
{
int arr1[] = { 1, 2, 4, 5, 6 };
int arr2[] = { 2, 3, 5, 7 };
int m = sizeof(arr1) / sizeof(arr1[0]);
int n = sizeof(arr2) / sizeof(arr2[0]);
// Function calling symmDiff(arr1, arr2, n, m);
printUnion(arr1, arr2, m, n );
printIntersection(arr1, arr2, m, n);
symmDiff(arr1, arr2, n, m);
return 0;
}
Attaching the output and program sample Screenshot just check it out for reference
Drop a comment if you have any doubt