[Solved] array function C++


bool equivalent(int a[], int b[], int n) {
    int i=0, j=0, count=0;
    // This condition fails immediately because the values are equal
    while (a[i] != b[j]) {
        // For this reason neither count nor j are incremented
        count++;
        j++;
    }

    for (int i=0; i<=n; i++)
        // This condition succeeds immediately because these values are equal
        if (a[i] % n == b[i+count] % n)
            // And true is returned.
            return true;
        else
            return false;
}

Please note there are also several other problems in the code. The first loop doesn’t have a safe end condition, the second loop has off-by-one end condition, …

2

solved array function C++