In: Computer Science
program will enter data into two single dimension arrays (do not store duplicate values in arrays)
program will find the union and intersection of the two arrays using one function
program will find the symmetric difference of two arrays
program will display the union, intersection, and symmetric difference */
short* input_data(short size); // function to dynamically allocate and array and enter data into the array
void display_data(short *data, short size); // function to display data in an array
void get_union_intersection(short *set1, short size1, short *set2, short size2, short *&union_array, short &size_union, short *&intersection, short &size_intersection);
void get_SymmetricDifference(short *set1, short size1, short *set2, short size2, short *&SD_array, short &size_SD);
int main()
{
short *set1, size1, // first data set and size
*set2, size2, // second data set and size
*union_array, size_union, // array to store the union of the two sets
*intersection, size_intersection, // array to store the intersection of the two sets
*symmetricdifference, size_SD; // array to store the symmetric difference of the two sets
cout<<"Program will find the union, intersection and symmetric difference of two sets of data\n";
cout<<"enter the number of values to store in the data set 1 \n or zero to terminate the program\n";
cin>>size1;
while(size1) // loop to permit user to test many data sets
{
cout<<"Enter "<<size1<<" values into the first set\n";
set1 = input_data(size1);
// print the contents of the array
cout<<"\nthere are "<<size1<<" values in the array set1\n";
display_data(set1, size1);
cout<<"enter the number of values to store in the data set 2\n";
cin>>size2;
cout<<"Enter "<<size1<<" values into the second set\n";
set2 = input_data(size2);
// test pointer to verify memory was allocated
cout<<"\nthere are "<<size2<<" values in the array set2\n";
display_data(set2, size2);
get_union_intersection(set1, size1, set2, size2, union_array, size_union, intersection, size_intersection);
cout<<"\nthe union array contains "<<size_union<<" values\n";
display_data(union_array, size_union);
if(size_intersection)// intersection array may be empty, must test size
{
cout<<"the intersection array contains "<<size_intersection<<" values\n";
display_data(intersection, size_intersection);
}
else
cout<<"there are no values in the intersection of the two data sets\n";
get_SymmetricDifference(set1, size1, set2, size2, symmetricdifference, size_SD);
if(size_SD)
{
cout<<"the symmetric difference array contains "<<size_SD<<" values\n";
display_data(symmetricdifference, size_SD);
}
else
cout<<"there are no values in the symmetric difference of the two data sets\n";
cout<<"\nenter the number of values to store in the data set 1 \n or zero to terminate the program\n";
cin>>size1;
// delete memory previously allocated
delete [] union_array;
delete [] intersection;
delete [] symmetricdifference;
delete [] set1;
delete [] set2;
}
// pause the program to see the results
system("pause");
return 0;
}
No subscripts are permitted in any function for this program including the inputData and displayData functions. You must use pointers to access dynamically allocated arrays and to control loop termination.
Write a function, input_data, to dynamically allocate an array of the size passed to the function. Store the data in the array from the keyboard and return the address allocated in the function to main.
Write one function to obtain the union and intersection in the same algorithm/function. Use the call statement given above to write the function definition. The union of two sets of data is all the values in set1 and all the values in set2 minus any duplicate values. Each set of data will contain no duplicate values. Dynamically allocate the union array to be the size of the two sets of data combined. Store the union of the two arrays in the union_array and assign the variable size_union a value which is the number of data values stored in the union_array. The intersection of two data sets is all the values that are found in both sets of data. Each set of data will contain no duplicate values. Dynamically allocate the intersection array to be the size of the smaller of the two data arrays passed to the function. Store the intersection of the two arrays in the intersection array and assign the variable size_intersection a value which is the number of data values stored in the intersection array. Be sure to pass each argument by the correct parameter passing mode.
Write a function, get_SymmetricDifference, to dynamically allocate an array in which to store the symmetric difference between two sets of data. This can be defined as the union of the two sets of data minus the intersection of the two data sets. However, the best solution would not be to find the union and the intersection, and then remove all values from the union that are found in the intersection. Dynamically allocate the size of the symmetric difference array to be the size of the two sets of data combined and store the address allocated in the SD_array parameter. Assign the variable size_SD a value which is the number of data values stored in the SD_array. Be sure to pass each argument by the correct parameter passing mode.
This function can be written without using any extra memory (dynamically allocated or compile time arrays). A faster algorithm would use a flag array the same size as set2 assuming that you will take each element from set1 and compare that value with the values in set2. Any local arrays used to solve this problem must be dynamically allocated and then deleted before the function terminates.
Test the following data sets: data sets of different sizes; data sets where the values in the two sets are completely different (empty intersection, the union and symmetric difference would be the same); data sets where the values in the two sets are exactly the same but in different orders (union and intersection are the same, empty symmetric difference); some combination of the previous sets.
Note – the following statement can be used at execution time to assign all values of a block of memory the same value
fill(base address, base address+ number of elements, value to assign to every element);
example: fill(data, data+size, 0);
PROGRAM:
#include <iostream>
#include <cstdlib>
using namespace std;
short* input_data(short size); // function to dynamically allocate and array and enter data into the array
void display_data(short *data, short size); // function to display data in an array
void get_union_intersection(short *set1, short size1, short *set2, short size2, short *&union_array, short &size_union, short *&intersection, short &size_intersection);
void get_SymmetricDifference(short *set1, short size1, short *set2, short size2, short *&SD_array, short &size_SD);
int main()
{
short *set1, size1, // first data set and size
*set2, size2, // second data set and size
*union_array, size_union, // array to store the union of the two sets
*intersection, size_intersection, // array to store the intersection of the two sets
*symmetricdifference, size_SD; // array to store the symmetric difference of the two sets
cout<<"Program will find the union, intersection and symmetric difference of two sets of data\n";
cout<<"enter the number of values to store in the data set 1 \n or zero to terminate the program\n";
cin>>size1;
while(size1) // loop to permit user to test many data sets
{
cout<<"Enter values"<<endl;
set1 = input_data(size1);
// print the contents of the array
cout<<"\nthere are "<<endl;
display_data(set1, size1);
cout<<"enter the number of values to store in the data set 2\n";
cin>>size2;
cout<<"Enter values"<<endl;
set2 = input_data(size2);
// test pointer to verify memory was allocated
cout<<"\nthere are "<<endl;
display_data(set2, size2);
get_union_intersection(set1, size1, set2, size2, union_array, size_union, intersection, size_intersection);
cout<<"\nthe union array contains "<<endl;
display_data(union_array, size_union);
if(size_intersection)// intersection array may be empty, must test size
{
cout<<"the intersection array contains "<<endl;
display_data(intersection, size_intersection);
}
else
cout<<"there are no values in the intersection of the two data sets\n";
get_SymmetricDifference(set1, size1, set2, size2, symmetricdifference, size_SD);
if(size_SD)
{
cout<<"the symmetric difference array contains "<<endl;
display_data(symmetricdifference, size_SD);
}
else
cout<<"there are no values in the symmetric difference of the two data sets\n";
cout<<"\nenter the number of values to store in the data set 1 \n or zero to terminate the program\n";
cin>>size1;
// delete memory previously allocated
delete [] union_array;
delete [] intersection;
delete [] symmetricdifference;
delete [] set1;
delete [] set2;
}
// pause the program to see the results
system("pause");
return 0;
}
//Define function
short* input_data(short size)
{
//Declare variable
short *set = new short[size];
//Declare variable
int kk=0;
//Loop
while((set+kk)!=(set+size))
{
//REad
cin>>*(set+kk);
//Increment value
kk++;
}
//Return
return set;
}
//Define function
void display_data(short *data, short size)
{
//Declare variable
short *ed = (data +size-1);
//Loop
for(;data<ed; data++)
{
//Display value
cout<<*(data)<<",";
}
//display last value
cout<<*data;
//new line
cout<<endl;
}
//Define function
void get_union_intersection(short *set1, short size1, short *set2, short size2, short *&union_array, short &size_union, short *&intersection, short &size_intersection)
{
//Declare variable
int kk=0;
//declare variable
size_union = 0;
//declare variable
union_array = new short[size1+size2];
//Find union
while((set1+kk)!=(set1+size1))
{
//Add values
*(union_array+ (size_union)) = *(set1+kk);
//Increment value
kk++;
//Increment value
size_union++;
}
//Declare variable
kk=0;
//Loop
while((set2+kk)!=(set2+size2))
{
//declare variable
int aa=0;
//Loop
while((set1+aa)!=(set1+size1))
{
//Check condition
if(*(set1+aa)==*(set2+kk))
{
//Break
break;
}
//Increment value
aa++;
}
//Check condition
if(aa==size1)
{
//Add value
*(union_array+size_union) = *(set2+kk);
//Increment value
size_union++;
}
//Increment value
kk++;
}
//Find intersection
int sm=0;
//Check condition
if(size1<size2)
//Set value
sm=size1;
//Else
else
//set value
sm=size2;
//declare space
intersection = new short[sm];
//Set value
size_intersection = 0;
//set value
kk=0;
//Loop
while((set2+kk)!=(set2+size2))
{
//Set value
int aa=0;
//Loop
while((set1+aa)!=(set1+size1))
{
//Check condition
if(*(set1+aa)==*(set2+kk))
{
//add value
*(intersection + size_intersection) = *(set2+kk);
//Increment value
size_intersection++;
//Break
break;
}
//Increment value
aa++;
}
//Increment value
kk++;
}
}
//define function
void get_SymmetricDifference(short *set1, short size1, short *set2, short size2, short *&SD_array, short &size_SD)
{
//Declare array
SD_array = new short[size1+size2];
//declare variable
int kk=0, jj=0;
//Declare variable
int fg=0;
//set value
size_SD = 0;
//Include elements from set1
//loop
while((set1+kk)!=(set1+size1))
{
//Set value
fg=0;
//sEt value
jj=0;
//Loop
while((set2+jj)!=(set2+size2))
{
//Check condition
if(*(set1+kk)==*(set2+jj))
{
//Set value
fg=1;
//Break
break;
}
//Increment value
jj++;
}
//Check condition
if(fg==0)
{
//Add value
*(SD_array+size_SD) = *(set1+kk);
//Increment value
size_SD ++;
}
//Increment value
kk++;
}
//Include elements from set2
jj=0;
//Loop
while((set2+jj)!=(set2+size2))
{
//Set value
fg=0;
//set value
kk=0;
//Loop
while((set1+kk)!=(set1+size1))
{
//Check condition
if(*(set1+kk)==*(set2+jj))
{
//Set value
fg=1;
//Break
break;
}
//Increment value
kk++;
}
//check condition
if(fg==0)
{
//set value
*(SD_array+size_SD) = *(set2+jj);
//Increment value
size_SD ++;
}
//Increment value
jj++;
}
}