Question: Program uses pointers for array access and dynamic memory allocation program will enter data into two single dimension arrays (no duplicate values in arrays) program

Program uses pointers for array access and dynamic memory allocation

program will enter data into two single dimension arrays (no duplicate values in arrays)

program will find the union and intersection of the two arrays

program will display the union and intersection */

#include

#include // header file may be 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(complete this prototype); // look at call statement to assist in completing this statement

short *get_intersection(complete this prototype); // look at call statement to assist in completing this statement

//void get_union_intersection(complete this prototype); // look at call statement to assist in completing this statement

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 "<

displayData(set1, size1);

cout<<"enter the number of values to store in the data set 2 ";

cin>>size2;

set2 = new(nothrow)short[size2];

// test pointer to verify memory was allocated

if(!set2) { cout<<"Memory allocation error, program will terminate "; system("pause"); exit(0); }

inputData(set2, size2);

cout<<" there are "<

displayData(set2, size2);

// comment out the next two lines to compute union and intersection in one function

get_union(set1, size1, set2, size2, union_array, size_union);

intersection = get_intersection(set1, size1, set2, size2, size_intersection);

// uncomment following line if you want to write 1 function to get the union and intersection

//get_union_intersection(set1, size1, set2, size2, union_array, size_union, intersection, size_intersection);

cout<<"the union array contains "<

displayData(union_array, size_union);

if(size_intersection)// intersection array may be empty, must test size

{

cout<<"the intersection array contains "<

displayData(intersection, size_intersection);

}

else

cout<<"there are no values in the intersection of the two data sets ";

cout<<" enter the number of values to store in the data set 1 or zero to terminate the program ";

cin>>size1;

// delete memory previously allocated

delete [] union_array;

delete [] intersection;

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 void function, get_union, to find the union of two sets of data. 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. Be sure to pass each argument by the correct parameter passing mode.

Write a value returning function, get_intersection, to find the intersection of two sets of data. Use the call statement given above to write the function definition. 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 and return the address allocated. Store the intersection of the two arrays in the intersection array and assign a value which is the number of data values stored in the intersection array to the variable size_intersection. Be sure to pass each argument by the correct parameter passing mode.

Instead of writing two separate functions, you can write one function to obtain the union and intersection in the same algorithm/function. This would be a different algorithm than simply using both algorithms from the individual functions described above in this function. To use this function only, comment out the two separate function calls and uncomment the call to this function. You could write all three functions for additional practice.

Example: data set1 contains the following values: 1, 2, 7, 3, 11, 4, 5, 0, 8

data set2 contains the following values: 1, 2, 7, 3, 12, 4, 15, 0, 9, 5, 10

union array would contain the follow values: 1, 2, 7, 3, 11, 4, 5, 0, 8, 12, 15, 9, 10

intersection array would contain the follow values: 1, 2, 7, 3, 4, 5, 0

Test the following data sets: data sets of different sizes; data sets where the values in the two sets are completely different (empty intersection); data sets where the values in the two sets are exactly the same but in different orders (union and intersection are the same); some combination of the previous sets. A data set will contain no duplicate values.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!