Question

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]

Homework Answers

Answer #1

#include <iostream>

using namespace std;

// helper function that returns true if given element found in given array

bool found(int *array, int size, int find)

{

    // search find in array

    for (int i = 0; i < size; i++)

        if (*(array + i) == find)

            return true;

    return false;

}

int main()

{

    // 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),

    int size1, size2;

    // get size of first array

    cout << "Enter size of first array: ";

    cin >> size1;

    int array1[size1];

    // input first array

    cout << "Enter array: ";

    for (int i = 0; i < size1; i++)

        cin >> *(array1 + i);

    cout << "Enter size of second array: ";

    cin >> size2;

    int array2[size2];

    // input second array

    cout << "Enter array: ";

    for (int i = 0; i < size2; i++)

        cin >> *(array2 + i);

    int union_size = 0, intersection_size = 0;

    // computes the intersection

    int intersection_array[size1 + size2];

    // loop over first array

    for (int i = 0; i < size1; i++)

    {

        // if found in second array too

        if (found(array2, size2, *(array1 + i)))

        {

            // insert to union

            *(intersection_array + intersection_size) = *(array1 + i);

            intersection_size++;

        }

    }

    // computes the union

    int union_array[size1 + size2];

    // loop over first array

    for (int i = 0; i < size1; i++)

    {

        // insert to union

        *(union_array + union_size) = *(array1 + i);

        union_size++;

    }

    // loop over second array

    for (int i = 0; i < size2; i++)

    {

        // if not found in first array

        if (!found(array1, size1, *(array2 + i)))

        {

            // insert to union

            *(union_array + union_size) = *(array2 + i);

            union_size++;

        }

    }

    // print intersection

    cout << "\nIntersection: ";

    for (int i = 0; i < intersection_size; i++)

        cout << intersection_array[i] << " ";

    // print union

    cout << "\nUnion: ";

    for (int i = 0; i < union_size; i++)

        cout << union_array[i] << " ";

}

.

Output:

.

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
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++
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
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++ 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...
*****C++ program***** Please implement the following, comments throughout code to explain, and provide screenshots of output...
*****C++ program***** Please implement the following, comments throughout code to explain, and provide screenshots of output for proof. Write a program for sorting a list of integers in ascending order using the bubble sort algorithm. Implement the following functions: Implement a function called readData int readData( int *arr) 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 arr....
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...
Its a c++ task. Write a program that reads 10 integers from the user into an...
Its a c++ task. Write a program that reads 10 integers from the user into an array and uses a function arrayMinimum that accepts an integer array a along with its size arraySize as parameters and returns the smallest array element. The program then outputs the result (the smallest array element).
Write a C program to combine two arrays of the same size arranged in order descendant....
Write a C program to combine two arrays of the same size arranged in order descendant. Test data : Enter the number of elements to be stored in the first array: 3 Input 3 elements in the arrangement: element [0]: 1 element [1]: 2 element [2]: 3 Enter the number of elements to be stored in the second array: 3 Input 3 elements in the arrangement: element [0]: 1 element [1]: 2 element [2]: 3 Expected output: The combined array...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF A RUNNING COMPILER QUESTION: 1) Fibonacci sequence is a sequence in which every number after the first two is the sum of the two preceding ones. Write a C++ program that takes a number n from user and populate an array with first n Fibonacci numbers. For example: For n=10 Fibonacci Numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 2): Write...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT