i have a problem :

Write the definition of a function, isReverse , whose first two parameters are arrays of integers of equal size, and whose third parameter is an integer indicating the size of each array. The function returns true if and only if one array is the reverse of the other. ("Reverse" here means same elements but in reverse order.)

this is the code i wrote:

bool isReverse(int x[],int y[],int z){
int i,b;
for(i=0;i<z;i++)
if(x[i]==y[z-i-1])
b=1;
else b=0;
return b;
}

i thought i did it right but its not and i'm not sure whats wrong. one of the error messages i was given is:

It seems that your function fails to check the ends of the arrays. Please revise your code.

i'm not sure what that mean. can somebody help me. thanks in advance.

Dani AI

Generated

The posted function's behaviour comes from a simple logic bug: the result flag is overwritten on every loop pass, so the function returns whatever the last comparison produced instead of whether all elements matched. That also creates an uninitialized-variable edge if the loop never runs. As suggested, stop at the first mismatch (breaking out); using an immediate return is clearer and safer. was right that the remark shown isn't a compiler error, and 's question about an online judge is relevant — judges often report only which hidden test failed.

A concise, correct C++ implementation:

bool isReverse(const int a[], const int b[], int n) {
    if (n < 0) return false;       // defensive: invalid size
    for (int i = 0; i < n; ++i) {
        if (a[i] != b[n - 1 - i]) return false; // stop at first mismatch
    }
    return true; // all positions matched (empty arrays are treated as reverse)
}

Why this fixes it: no flag variable is repeatedly reassigned; the function exits immediately on a mismatch so later matches cannot hide earlier failures. If n == 0 the loop is skipped and true is returned (empty arrays are reverses of each other). For robustness, validate that n is non-negative or use size_t/std::vector for sizes.

Extra tip: for idiomatic C++, prefer container-based checks (and the standard algorithms) when allowed. For example, compare sizes and then use std::equal with reverse iterators to express the same check in a single line.

Recommended Answers

All 7 Replies

>It seems that your function fails to check the ends of the arrays.
>Please revise your code.

That doesn't look like a compiler error. Did your teacher tell you this?

yea thats not a compiler error message. you could say that its a remark, i guess the code i wrote doesnt do what it wants me to. but i dont understand what it wants. what i wrote looks right to me, but it says its wrong so its wrong.

yea thats not a compiler error message. you could say that its a remark, i guess the code i wrote doesnt do what it wants me to. but i dont understand what it wants. what i wrote looks right to me, but it says its wrong so its wrong.

Is "it" a online judge? If so then post the question.

no, you just write the code for what it wants then submit it, if its right then you pass and if its wrong then you do it again thing.

what is "it"?

Suppose you have two arrays:

tab1[]={1,1,1};
tab2[]={1,0,1};

First iteration: tab1[0]==tab2[2] =>b=1; second iteration: tab1[1]!=tab2[1] =>b=0; (so far, so good) third iteration: tab1[2]==tab1[0] =>b=1 (again!) The loop is over and you return b, your function says that arrays are equal which is not true.
Solution: end the loop after second iteration (at the first time, when find difference between arrays). Use break

Suppose you have two arrays:

tab1[]={1,1,1};
tab2[]={1,0,1};

First iteration: tab1[0]==tab2[2] =>b=1; second iteration: tab1[1]!=tab2[1] =>b=0; (so far, so good) third iteration: tab1[2]==tab1[0] =>b=1 (again!) The loop is over and you return b, your function says that arrays are equal which is not true.
Solution: end the loop after second iteration (at the first time, when find difference between arrays). Use break

thanks alot it worked. gonna take a bit to understand why but thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.