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;
    }
Paul
  • 448
  • 1
  • 6
  • 14
Stella Jin
  • 15
  • 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. – Cheri Oct 22 '19 at 02:45
  • 1
    Also, in your `for` loop in `isFound`, you're *assigning* `arr[i]` to `m` in your if statement, not comparing them. – jkb Oct 22 '19 at 02: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. – srt1104 Oct 22 '19 at 03:01
  • Please read this https://stackoverflow.com/questions/57842756/why-should-i-always-enable-compiler-warnings – n. m. could be an AI Oct 22 '19 at 03:55

1 Answers1

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

}
boB
  • 173
  • 10