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?
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
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments