Question

In: Computer Science

     program will enter data into two single dimension arrays (do not store duplicate values in arrays)...

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

Solutions

Expert Solution

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

}

}


Related Solutions

Write an arm assembly program that will multiply two arrays (index by index) and store the...
Write an arm assembly program that will multiply two arrays (index by index) and store the result in a third array. Declare an array: .data Arr1: .quad 10    #index 0             .quad 4      #index 1          ….. Arr2: .quad 2,5,6…. Arr3: .quad 0,0,0…. To load array pointer address:      Movq $Arr1, %rdx   #stores address of Arr1 index 0 in rdx To move to the next index of an array:     Addq $8,%rdx To retrieve data: Movq (%rdx), %rcx         # will...
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an...
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an array which can store up to 50 student names where a name is up to 25 characters long an array which can store marks for 5 courses for up to 50 students The program should first obtain student names and their corresponding marks for a requested number of students from the user. Please note that the program should reject any number of students that...
Build a simple list implementation that uses arrays to store the values. Create a class SimpleArrayList...
Build a simple list implementation that uses arrays to store the values. Create a class SimpleArrayList with a public constructor that initializes the list using a passed array of Object references. Assert that the passed array is not null. Next, implement: 1)Object get(int), which takes an int index and returns the Object at that index 2)void set(int, Object), which takes an int index and an object reference and sets that value at the index to the passed reference Both your...
Write a C program that asks the user to enter double values (the values can be...
Write a C program that asks the user to enter double values (the values can be positive, zero, or negative). After entering the first double value, the program asks "Would you like to enter another value?", and if the user enters Y or y, the program continues to get additional values and stores these values into a double array (for up to 100 values — use a symbolic #define constant to specify the 100 size limit). The program will need...
The following data are available for the Midwest Division of Dimension Products, Inc. and the single...
The following data are available for the Midwest Division of Dimension Products, Inc. and the single product it makes. (Note that this is the same data as that provided for the question above.) Unit selling price $40 Variable cost per unit       $24 Annual fixed costs           $560,000 Average operating assets   $3,000,000 How many units must the Midwest Division sell each year to have an ROI of 16%? A)         52,000.             B)         65,000.               C)         240,000.             D)         1,300,000 2. Which of the following variances is caused by a difference between the...
C++ DO not use arrays to write this program. Write a program that repeatedly generates three...
C++ DO not use arrays to write this program. Write a program that repeatedly generates three random integers in the range [1, 100] and continues as follows: If the right-most digit of all the three integers is equal, the program displays them in ascending order on the screen and continues. If the generated integers have different right-most digits, they are not displayed and the program continues. The program terminates once the right-most digits of all the three random numbers are...
Develop an assembly language program for 89c51 to do following {Marks 10} Store any 8 values...
Develop an assembly language program for 89c51 to do following {Marks 10} Store any 8 values of one byte each anywhere in Scratch Pad area of RAM. You are bound to use loop to store values. Find Mean of these 8 values (use shift operator for division) and send it to port P2 (formula for to find mean is given below) Find the lowest number among the numbers saved in part (1), take its 2’s complement and send it to...
Write a Java program that prompts the user to enter a list of integer values and...
Write a Java program that prompts the user to enter a list of integer values and displays whether the list is sorted in increasing order or not. Here is a sample run. Note that the first number in the input indicates the number of the elements in the list. <Output> Enter list: 8 101516619111 The list is not sorted <End Output <Output> Enter list: 10 11344579 11 21 The list is already sorted <End Output Create a complete class for...
Write a JAVA program that prompts the user to enter a single name. Use a for...
Write a JAVA program that prompts the user to enter a single name. Use a for loop to determine if the name entered by the user contains at least 1 uppercase and 3 lowercase letters. If the name meets this policy, output that the name has been accepted. Otherwise, output that the name is invalid.
Write a Java program that directs the user to enter a single word (of at least...
Write a Java program that directs the user to enter a single word (of at least four characters in length) as input and then prints the word out in reverse in the pattern shown below (please note that the spaces added between each letter are important and should be part of the program you write): <Sample Output Enter a word (at least four characters in length): cat Word must be at least four characters in length, please try again. Enter...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT