Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CSCI 152 Programming Fundamentals II Spring 2018 Program 7 (union, intersection, and symmetric difference) 4/9/18 /* CSCI 152 Spring 2018 - Program 7 program will

CSCI 152 Programming Fundamentals II Spring 2018

Program 7 (union, intersection, and symmetric difference) 4/9/18

/* CSCI 152 Spring 2018 - Program 7

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 */

#include

#include

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

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

{

cout<<"Enter "<

set1 = input_data(size1);

// print the contents of the array

cout<<" there are "<

display_data(set1, size1);

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

cin>>size2;

cout<<"Enter "<

set2 = input_data(size2);

// test pointer to verify memory was allocated

cout<<" there are "<

display_data(set2, size2);

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

cout<<" the union array contains "<

display_data(union_array, size_union);

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

{

cout<<"the intersection array contains "<

display_data(intersection, size_intersection);

}

else

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

get_SymmetricDifference(set1, size1, set2, size2, symmetricdifference, size_SD);

if(size_SD)

{

cout<<"the symmetric difference array contains "<

display_data(symmetricdifference, size_SD);

}

else

cout<<"there are no values in the symmetric difference 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 [] symmetricdifference;

delete [] set1;

delete [] set2;

}

// pause the program to see the results

system("pause");

return 0;

}

Assignment Put in eCollege dropbox: Program 7

Due: 4/16/2018

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

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

symmetric difference array would contain the follow values: 11, 8, 12, 15, 9, 10

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);

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Guide To Client Server Databases

Authors: Joe Salemi

2nd Edition

1562763105, 978-1562763107

More Books

Students also viewed these Databases questions