Reputation: 55
I was writing a recursive function, and encountered a problem at the return statement.
int SumOfEvenNumbers(int v[], int i)
{
if ( i > v[0] )
return 0;
return SumOfEvenNumbers(v, i+1) + (v[i]%2==0)?v[i]:0;
}
The function is called like this:
SumOfEvenNumbers(vector_indexed_from_1, 1);
//v[0] is equal to the number of elements, excluding itself
What I expected the ternary if to return was either v[i] or 0 ( in case it is even ) but apparently after printing the result of every ternary if to the screen, the only values that result from the expression are 1 and 0.
int SumOfEvenNumbers(int v[], int i)
{
if ( i > v[0] )
return 0;
cout << (v[i]%2==0)?(v[i]):(0); // either a 1 or a 0
return SumOfEvenNumbers(v, i+1) + (v[i]%2==0)?v[i]:0;
}
The way I fixed this is by initializing a variable with the result of the expression and then adding that to the return value.
int SumOfEvenNumbers(int v[], int i)
{
if ( i > v[0] )
return 0;
int rv = (v[i]%2==0)?(v[i]):(0);
return SumOfEvenNumbers(v, i+1) + rv;
}
Could anyone explain what is happening? Is it possible to avoid declaring a variable in order to obtain the correct result?
Upvotes: 2
Views: 1232
Reputation: 4236
Could anyone explain what is happening?
Take a look at operator precedence. Here ternary conditional happens to have very low priority. In SumOfEvenNumbers(v, i+1) + (v[i]%2==0)?v[i]:0
expression it is calculated after +
given you unexpected result.
Is it possible to avoid declaring a variable in order to obtain the correct result?
I cases when you are not sure about order of operators it is safe to use brackets ()
to indicate the order explicitly. So SumOfEvenNumbers(v, i+1) + ((v[i]%2==0)?v[i]:0)
will be calculated as expected.
Upvotes: 0
Reputation: 113
The ternary conditional operator has lower precedence than operator+. Your code was actually parsed as
(SumOfEvenNumbers(v, i+1) + (v[i]%2==0)) ? v[i] : 0;
To get what you want, use parentheses
SumOfEvenNumbers(v, i+1) + ((v[i]%2==0)) ? v[i] : 0);
See: http://en.cppreference.com/w/c/language/operator_precedence
Upvotes: 7