0

I have to use part (a) a function named isFound that takes three arguments, namely an array of integers, its size and an integer value m; and then returns true if m is found in the array and returns false otherwise.

    bool isFound (int arr[], int size, int m)
   {
    bool result = false;
    for (int i = 0; i < size; i++){
        if (m = arr[i]){
            return true;
        }
    }
    return result;
    }

    bool isContained(int A1[],int A2[],int size1, int size2)
   {
    for ( int i = 0; i < size1 ; i ++){
        bool result = false;
        for ( int j = 0; j < size2 && !isFound; j++){
            if ( A1[i] == A2[j]){
                result = true;
            }
        }
    if (!isFound){
        return false;
    }
    }
    return true;
    }
4
  • 1
    To start, you need to call isFound with arguments. If you just put isFound, it most certainly will not do what you want. Commented Oct 22, 2019 at 2:45
  • 1
    Also, in your for loop in isFound, you're assigning arr[i] to m in your if statement, not comparing them. Commented Oct 22, 2019 at 2:47
  • The code you have provided won't compile but if I'm not mistaken with what you're trying to accomplish with it, if A1 = {1, 2, 2} and A2 = {1, 2} then it will return true when it shouldn't. Commented Oct 22, 2019 at 3:01
  • Please read this stackoverflow.com/questions/57842756/… Commented Oct 22, 2019 at 3:55

1 Answer 1

1

As was already pointed out, you're doing an assignment where you want a comparison. As far as I can see, your logic in isContained is way off base -- there's no reason you should be checking individual elements of the two arrays to each other if all you want to know is does one contain the other's values. Since you have isFound(), use it :-).

Here's my take on the solution:

bool isFound(int arr[], int size, int m) {
    for (int i=0; i<size; i++) 
        if (m == arr[i]) return true;
    return false;
}

bool isContained(int A1[], int A2[], int size1, int size2) {
    for (int i=0; i<size2; i++) {
        if (!isFound(A1, size1, A2[i])) return false;  // Any one value not included, the array is not included
    }
    return true;
}

And the code I used to confirm it:

int main() {
    int A1[] = { 1,2,3,4,5,6 };
    int A2[] = { 1,2 };
    std::cout << (isContained(A1, A2, 6, 2) ? "" : "NOT ") << "contained" << "\r\n";  // Output: contained


    int A3[] = { 1,2,3,4,5,6 };
    int A4[] = { 1,2,8 };
    std::cout << (isContained(A3, A4, 6, 3) ? "" : "NOT ") << "contained" << "\r\n";  // Output: NOT contained

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.