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
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
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 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...
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...
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...
Phone number lookup Design a program that has two parallel arrays: a string array named people...
Phone number lookup Design a program that has two parallel arrays: a string array named people that is initialized with the names of seven of your friends, and a string array named phoneNumbers that is initialized with your friends phone numbers. The program should allow the user to enter a persons name (or part of a persons name). it should then search for that person in the people array. If the person is found, it should get that persons phjone...
Q: Design a program that lets the user enter the total rainfall for each of 12...
Q: Design a program that lets the user enter the total rainfall for each of 12 months into an array. The program should calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest amounts. Create parallel arrays for the month names and rainfall amounts for each month. Use month names (i.e. January, February, March, etc.) when printing out the months with the highest and lowest amounts. Include a modular...
Assignment #4 – Student Ranking : In this assignment you are going to write a program...
Assignment #4 – Student Ranking : In this assignment you are going to write a program that ask user number of students in a class and their names. Number of students are limited to 100 maximum. Then, it will ask for 3 test scores of each student. The program will calculate the average of test scores for each student and display with their names. Then, it will sort the averages in descending order and display the sorted list with students’...
(C++) 5.15 LAB: Two smallest numbers with arrays Write a program that reads a list of...
(C++) 5.15 LAB: Two smallest numbers with arrays Write a program that reads a list of integers, and outputs the two smallest integers in the list, in ascending order. The input begins with an integer indicating the number of integers that follow. Ex: If the input is: 5 10 5 3 21 2 the output is: 2 3 You can assume that the list of integers will have at least 2 values. To achieve the above, first read the integers...