You need to put the comparison in a loop, so it's something like this.
1 2 3 4 5
int stop = x/2;
for (int i=0; i<stop; ++i) {
if (br[i] != br[x-i]) returnfalse;
}
returntrue;
Note that I have intentionally left a bug in here to help you walk through the process of creating the algorithm. The basic idea is plain, but to get it right you need to check against a bunch of possible "off-by-one" errors. Ask yourself these questions:
- What is the right value for "stop"? Consider an odd and an even value of x to determine this (like x=4 and x=5).
- What are the right values to compare? When i=0 it should compare against the last value in the array.
- Does it work in degenerate cases like when x=0, x=1 and x=2?
Note that I store the ending value of a loop in a variable rather than putting the expression in the loop itself. This is because C++ evaluates the expression each time and why make the computer do the division when the answer won't change? If optimization is turned on then the compiler will figure out that it can do this, but personally I like to do it myself.