Home > Net >  Write a program that reads two sets of integer elements (10 elements for each set) and finds the int
Write a program that reads two sets of integer elements (10 elements for each set) and finds the int

Time:10-08

How can I finish my code below? Where should I add isEmpty? And how to just print "The Intersected Elements of Set A and B are:" once only?

#include <iostream>
using namespace std;

int main() {

    int A[10], B[10];

    cout << "Enter 10 Elements of Set A:" << endl;
    for (int i = 0; i < 10; i  ) {
        cin >> A[i];
    }
    cout << "Enter 10 Elements of Set B:" << endl;
    for (int i = 0; i < 10; i  ) {
        cin >> B[i];
    }

    bool isFirstIntersectedElem = true;
    bool isEmpty = true;

    for (int i = 0; i < 10; i  ) {
        for (int j = 0; j < 10; j  ) {
            if (A[i] == B[j]) {
                for (int i = 0; i < 10 && isFirstIntersectedElem; i  ) {
                    if (A[i] != B[i]) {
                        isFirstIntersectedElem = false;
                    }
                }

                if (isFirstIntersectedElem) {
                    cout << "The Intersected Elements of Set A and B are:" << endl;
                    cout << A[i] << " ";
                }
                else {
                    cout << "The IntersectedElement of Set A and B are not Found.";
                }
            }
        }
    }
    return 0;
}

Here is the expected output: (https://i.stack.imgur.com/7cPvX.png)

CodePudding user response:

It looks like you're mostly there. You can read in the numbers, and then you're using nested loops to check for each element in A if it's in B.

The best approach may be to create an array C of ten ints and a new counter variable initially set to zero. If you find an element in A that is in B you can store it in C and increment your counter. You'll have the intersection in C and the count of how long it is.

CodePudding user response:

Don’t write using namespace std;.

You can, however, in a CPP file (not H file) or inside a function put individual using std::string; etc. (See SF.7.)


Separate your "real code", the logic of finding the intersection in this case, from the input and output. Write a function that takes inputs as parameters and returns a result.

So, don't print-as-you-go. Rather, collect a new set for the result, which is eventually returned.

Then, the main code can see whether the result is an empty set and print something suitable, easily.


Where should I add isEmpty?

What's it for? It doesn't seem to be used at all. Just delete it.


Rather than limiting your input to exactly ten values each, use a proper collection that holds any number of elements. In fact, you should use a std::set which will automatically squish duplicates and give you a sorted view regardless of how it was input.

Use a ranged for loop, rather than iterating via subscripts.

Don't duplicate the identical input code for the two input values. Make a common function to read a set, and call it twice.

Something like:

using setval = std::set<int>;

setval read_input() { ... }  // prompt user and read

setval intersection (const setval& left, const setval& right)
{
/* your main problem is solved here */
/* just calling std::set_intersection
  (see https://en.cppreference.com/w/cpp/algorithm/set_intersection) is cheating!
  The exercise is to understand and write the logic for doing this.
  However, it would be smart to automate testing by comparing your 
  results against the library function! */
}

int main()
{
    setval A = read_input();
    setval B = read_input();
    auto I = intersection (A,B);
    if (I.empty()) cout << "No intersection.\n";
    else {
         // print the results
    }
}

  • Related