Ternary operator inside a return statement

p0licat

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?

sl0th

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

ternary operator causes an error inside if statement

From Dev

Ternary operator in select statement

From Dev

Ternary operator is not a statement

From Dev

Ternary Operator in jQuery statement

From Dev

Convert if statement to ternary operator

From Dev

Ternary operator inside printf

From Dev

Return throwing ternary operator?

From Dev

Complex return in Ternary Operator?

From Dev

SQL statement equivalent to ternary operator

From Dev

IEnumerable Select statement with ternary operator

From Dev

Max Ternary operator IF statement compare

From Dev

IEnumerable Select statement with ternary operator

From Dev

Javascript: ternary operator inside "if" condition

From Dev

Python - loop inside ternary operator

From Dev

Will a ternary operator work inside .text()?

From Dev

Ternary operator inside calculation PHP

From Dev

Python - loop inside ternary operator

From Dev

Ternary operator with return statements JavaScript

From Dev

js: Multiple return in Ternary Operator

From Dev

ternary operator issue in React return

From Dev

Return ternary operator in JavaScript reduce

From Java

What does a "+=" operator inside a ternary operator means?

From Dev

How to apply += operator inside a conditional ternary operator

From Dev

JavaScript: Is it allowed to use a `ternary` statement inside an `if` statement?

From Dev

convert some lines of php statement into ternary operator

From Dev

How to use AND in if statement using ternary operator

From Dev

Question Regarding Using Ternary Operator in Switch Statement

From Dev

convert some lines of php statement into ternary operator

From Dev

How to use AND in if statement using ternary operator

Related Related

HotTag

Archive