- The total number of all possible pairs you guessed wrong.
- The algorithm for odd/even products is wrong too but that is closely related on your guesstimation on total possible pairs.
- Resetting variables in that function for re-use is inappropriate since that is not the responsibility of that function. Nor was that part of the task.
However, setting that variable to "0" for reasons of correctness would have been correct. I should have done that to make sure the precondition is right. In your (and mine) case you should have set "ep" to 0 as well for this very reason.
You forgot that both "i" and "j" INCLUDE index "0". So you need to start with Numbers[0]*Numbers[0]. If you did that right then your algorithm would have been correct.
Also this:
Code:
std::size_t i;
for (i = 0; i < size; ++i)
Keep "i" in the scope of the for-loop only. Now it's scope has been extended to the function for no valid reason.
All this the correct code:
Code:
void odd_even_pairs_fast(const int* vec, std::size_t size, unsigned& ep, unsigned& op)
{
ep =0; op =0; //Guarantee correct precondition.
for(std::size_t i = 0; i < size; ++i)
{
if( !even(vec[i]))
{
++op;
}
}
op *= op;
ep = (unsigned)(size * size) - op;
}
I think you did well.