Program will find the union and intersection of the two arrays (I wrote the firs
ID: 3700810 • Letter: P
Question
Program will find the union and intersection of the two arrays (I wrote the first two functions, I really need help with the last two, its 25% of my grade)
Write 2 more functions that will complete the program.
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.
Code given by professor
#include
#include // header file required for dynamic allocation of memory
using namespace std;
void inputData(short *data, short size); // function to enter data into the array
void displayData(short *data, short size); // function to display data in an array
void get_union(short *set1, short size1, short *set2, short size2, short *union_array, short size_union);
short *get_intersection(short *set1, short size1, short *set2, short size2, short *intersection, short size_intersection);
int main()
{
short *set1, size1, // first data set and size; do not put duplicate values in the data set
*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
cout<<"Program will find the union and intersection of two sets of data ";
cout<<"enter the number of values to store in the data set 1 or zero to terminate the program ";
cin>>size1;
while(size1) // loop to permit user to test many data sets
{
set1 = new(nothrow)short[size1];
// test pointer to verify memory was allocated
if(!set1) { cout<<"Memory allocation error, program will terminate "; system("pause"); exit(0); }
inputData(set1, size1);
// print the contents of the array
cout<<" there are "<
void inputData(short *data, short size) // function to enter data into the array
{
short count=1;
short *end=(data+size);
for( ;data<end;data++)
{
cout<<"Enter Number "<<count++<<" : ";
cin>>*data;
cout<<endl;
}
}
void displayData(short *data, short size) // function to display data in an array
{
short count;
short *end=(data+size);
for(;data<end;data++)
{
cout<<"Number "<<count++<<" : "<<*data<<endl;
}
}
void get_union(short *set1,short size1,short *set2,short size2,short *union_array,short size_union) // look at call statement to assist in completing this statement
{
}
short *get_intersection(short *set1, short size1, short *set2, short size2, short *intersection, short size_intersection) // look at call statement to assist in completing this statement
{
}
Explanation / Answer
If you have any doubts, please give me comment...
#include <iostream>
#include <cstdlib> // header file required for dynamic allocation of memory
using namespace std;
void inputData(short *data, short size); // function to enter data into the array
void displayData(short *data, short size); // function to display data in an array
void get_union(short *set1, short size1, short *set2, short size2, short *union_array, short &size_union);
void get_intersection(short *set1, short size1, short *set2, short size2, short *intersection, short &size_intersection);
int main()
{
short *set1, size1, // first data set and size; do not put duplicate values in the data set
*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
cout << "Program will find the union and intersection of two sets of data ";
cout << "enter the number of values to store in the data set 1 or zero to terminate the program ";
cin >> size1;
while (size1) // loop to permit user to test many data sets
{
set1 = new (nothrow) short[size1];
// test pointer to verify memory was allocated
if (!set1)
{
cout << "Memory allocation error, program will terminate ";
exit(0);
}
inputData(set1, size1);
// print the contents of the array
cout << " there are " << endl;
displayData(set1, size1);
cout << "enter the number of values to store in the data set 2 ";
size2 = size1;
set2 = new (nothrow) short[size2];
// test pointer to verify memory was allocated
if (!set2)
{
cout << "Memory allocation error, program will terminate ";
exit(0);
}
inputData(set2, size2);
// print the contents of the array
cout << " there are " << endl;
displayData(set2, size2);
union_array = new (nothrow) short[size2+size1];
get_union(set1, size1, set2, size2, union_array, size_union);
cout<<" UNION "<<endl;
displayData(union_array, size_union);
intersection = new (nothrow) short[size1];
get_intersection(set1, size1, set2, size2, intersection, size_intersection);
cout<<" INTERSECTION "<<endl;
displayData(intersection, size_intersection);
cout<<endl;
cout<<endl;
}
}
void inputData(short *data, short size) // function to enter data into the array
{
short count = 1;
short *end = (data + size);
for (; data < end; data++)
{
cout << "Enter Number " << count++ << " : ";
cin >> *data;
}
}
void displayData(short *data, short size) // function to display data in an array
{
short count;
short *end = (data + size);
for (; data < end; data++)
{
cout << "Number " << count++ << " : " << *data << endl;
}
}
void get_union(short *set1, short size1, short *set2, short size2, short *union_array, short &size_union) // look at call statement to assist in completing this statement
{
int i = 0;
size_union=0;
while((set1+i)!=(set1+size1)){
*(union_array+size_union) = *(set1+i);
i++;
size_union++;
}
i=0;
while((set2+i)!=(set2+size2)){
int j=0;
while((set1+j)!=(set1+size1)){
if(*(set1+j)==*(set2+i)){
break;
}
j++;
}
if(j==size1){
*(union_array+size_union) = *(set2+i);
size_union++;
}
i++;
}
}
void get_intersection(short *set1, short size1, short *set2, short size2, short *intersection, short &size_intersection) // look at call statement to assist in completing this statement
{
size_intersection = 0;
int i=0;
while((set2+i)!=(set2+size2)){
int j=0;
while((set1+j)!=(set1+size1)){
if(*(set1+j)==*(set2+i)){
break;
}
j++;
}
if(j!=size1){
*(intersection+size_intersection) = *(set2+i);
size_intersection++;
}
i++;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.