Question

Write a program that takes two integer arrays of different sizes from the user (this means...

Write a program that takes two integer arrays of different sizes from the user (this means that the user can tell the program the size of each array), and then computes the intersection and union of those arrays. Note: the arrays can be of different lengths and the intersection/union should have unique elements in sorted order. Use pointer notation of arrays for this question. Eg: *(arr+1) [10]


C++

Homework Answers

Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks

#include<iostream>

using namespace std;

//method to add element to an array in sorted order, and update size reference variable
//element will be added only if it is not present already
void add_sorted(int* arr, int element, int& size){
        int index=size;
        //finding proper place to add element
        for(int j=0;j<size;j++){
                if(element<*(arr+j)){
                        //found the place to add
                        index=j;
                        break;
                }else if(element==*(arr+j)){
                        //duplicate, exiting
                        return;
                }
        }
        //now shifting all elements starting from index position to one place right
        for(int j=size;j>index;j--){
                *(arr+j)=*(arr+j-1);
        }
        //adding element at index pos, and incrementing size
        *(arr+index)=element;
        size++;
}

//method to find the union of arr1 and arr2, and store in result
//returns the number of elements in result.
int find_union(int* arr1, int size1, int* arr2, int size2, int* result){
        int count=0;
        //looping through each elements in arr1
        for(int i=0;i<size1;i++){
                int value=*(arr1+i);
                //adding to result array in sorted order, if not present already
                add_sorted(result,value,count);
        }
        //repeating same with arr2 elements
        for(int i=0;i<size2;i++){
                int value=*(arr2+i);
                add_sorted(result,value,count);
        }
        return count;
}

//method to find the intersection of arr1 and arr2, and store in result
//returns the number of elements in result.
int find_intersection(int* arr1, int size1, int* arr2, int size2, int* result){
        int count=0;
        //looping through all elements in arr1
        for(int i=0;i<size1;i++){
                int value=*(arr1+i);
                //checking if element is present in other array
                for(int j=0;j<size2;j++){
                        int value2=*(arr2+j);
                        if(value==value2){
                                //yes. adding to result array in sorted order
                                add_sorted(result,value,count);
                                break;
                        }
                }
                
        }
        return count;
}

int main(){
        int *arr1, *arr2, *un, *in;
        int size1, size2, unSize, inSize;
        
        //reading size for arr1
        cout<<"Enter size of array 1: ";
        cin>>size1;       
        //initializing arr1 using dynamic memory
        arr1=new int[size1];
        //asking and reading values to arr1
        cout<<"Enter "<<size1<<" integers: ";
        for(int i=0;i<size1;i++){
                cin>>*(arr1+i);
        }
        
        //repeating the same for arr2
        cout<<"Enter size of array 2: ";
        cin>>size2;       
        arr2=new int[size2];
        cout<<"Enter "<<size2<<" integers: ";
        for(int i=0;i<size2;i++){
                cin>>*(arr2+i);
        }
        
        //initializing array to store union, with max capacity: size1+size2
        un=new int[size1+size2];
        //initializing array to store intersection. here, size1 or size2 can be used
        in=new int[size1];
        
        //finding union, storing in un, storing number of elements in unSize
        unSize=find_union(arr1,size1,arr2,size2,un);
        //finding intersection
        inSize=find_intersection(arr1,size1,arr2,size2,in);
        
        //displaying union and intersection
        cout<<"Union: ";
        for(int i=0;i<unSize;i++){
                cout<<*(un+i)<<" ";
        }
        cout<<endl;
        
        cout<<"Intersection: ";
        for(int i=0;i<inSize;i++){
                cout<<*(in+i)<<" ";
        }
        cout<<endl;
        
        //deleting all arrays before exiting
        delete[] arr1;
        delete[] arr2;
        delete[] un;
        delete[] in;
        return 0;
}

/*OUTPUT*/

Enter size of array 1: 10
Enter 10 integers: 10 9 8 8 11 23 2 10 3 3
Enter size of array 2: 5
Enter 5 integers: 1 2 3 4 5
Union: 1 2 3 4 5 8 9 10 11 23
Intersection: 2 3
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
C++ Write a program that takes two integer arrays of different sizes from the user (this...
C++ Write a program that takes two integer arrays of different sizes from the user (this means that the user can tell the program the size of each array), and then computes the intersection and union of those arrays. Note: the arrays can be of different lengths and the intersection/union should have unique elements in sorted order. Use pointer notation of arrays for this question. Eg: *(arr+1) [10]
Write a Java program that asks the user to enter an array of integers in the...
Write a Java program that asks the user to enter an array of integers in the main method. The program should prompt the user for the number of elements in the array and then the elements of the array. The program should then call a method named isSorted that accepts an array of and returns true if the list is in sorted (increasing) order and false otherwise. For example, if arrays named arr1 and arr2 store [10, 20, 30, 41,...
Write a function that takes in 3 arguments: a sorted array, size of the array, and...
Write a function that takes in 3 arguments: a sorted array, size of the array, and an integer number. It should return the position where the integer value is found. In case the number does not exist in that array it should return the index where it should have been if it were present in this sorted array. Use pointer notation of arrays for this question.
C++ Write a function that takes in 3 arguments: a sorted array, size of the array,...
C++ Write a function that takes in 3 arguments: a sorted array, size of the array, and an integer number. It should return the position where the integer value is found. In case the number does not exist in that array it should return the index where it should have been if it were present in this sorted array. Use pointer notation of arrays for this question. c++ code
***C++ CODING*** Write a program for sorting a list of integers in ascending order using the...
***C++ CODING*** Write a program for sorting a list of integers in ascending order using the bubble sort algorithm. Requirements Implement the following functions: Implement a function called readData int *readData( )   The function returns a pointer that points to the locations with integers reading from the file data.txt. arr is a pointer for storing the integers. The function returns the number of integers. The function readData reads the list of integers from a file call data.txt into the array...
Instructions Write a Java code Your goal is to take N integer inputs from the user...
Instructions Write a Java code Your goal is to take N integer inputs from the user -- N's value will be given by the user as well. You can assume the user provides a valid value for N, i.e., >0. Store the input integers in an array of size N in the order they are provided. These tasks should be done in the main() method. Create a new method called checkArray() that will take the previously created array as input...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an array of ints, an int value named element, and an int value named end. Return a bool based on whether the element appears in the array starting from index 0 and up to but not including the end index. Generate Random Array Write a function that takes as parameters an array of integers and another integer for the size of the array. Create a...
Topics Arrays Accessing Arrays Description Write a C++ program that will display a number of statistics...
Topics Arrays Accessing Arrays Description Write a C++ program that will display a number of statistics relating to data supplied by the user. The program will ask the user to enter the number of items making up the data. It will then ask the user to enter data items one by one. It will store the data items in a double array. Then it will perform a number of statistical operations on the data. Finally, it will display a report...
Write a program that takes n integer numbers from the user, and then counts the number...
Write a program that takes n integer numbers from the user, and then counts the number of even numbers and odd numbers and print them to the screen. Sample Output: Enter how many numbers you have: 10 Enter the 10 numbers: 1 3 19 50 4 10 75 20 68 100 The number of even numbers is: 6 The number of odd numbers is: 4 (( in java ))
Using C++, Python, or Java, write a program that: In this programming exercise you will perform...
Using C++, Python, or Java, write a program that: In this programming exercise you will perform an empirical analysis of the QuickSort algorithm to study the actual average case behavior and compare it to the mathematically predicted behavior. That is, you will write a program that counts the number of comparisons performed by QuickSort on an array of a given size. You will run the program on a large number of arrays of a certain size and determine the average...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT