solving for the value of arithmetic sequences

These questions are on my practice exam, which we just have to evaluate. If someone could break down the modulo % and how that works that would be great! The given answers are listed below, just need an explanation to how the order of operation works for this

15 / ( 5 - 3 % 7 ) * 2.0 ans: 14.0
2.0 * 15 / ( 5 - 3 % 7 ) ans: 15.0

suggest you lookup operator precedence in C++, the expressions would be fairly simple to evaluate once you know the order of applying the operators
This particular question illustrates precedence, associativity and 'usual arithmetic conversions' in C++.

In particular, if we have an arithmetic operation between two integers, integer arithmetic is performed.
If we have an arithmetic operation between an integer and a floating point value, the integer is first converted to floating point and then floating point arithmetic is performed.

The percentage in 3%7 is the integer modulo operator; it yields the remainder after division of one number by another. https://en.wikipedia.org/wiki/Modulo_operation
3%7 == 3 (integer)

To evaluate the sub-expression ( 5 - 3 % 7 ), we need to take precedence into account: in this case % has a higher precedence than - (multiplicative operators have higher precedence than additive operators).
So it is parsed as ( (5) - (3%7) ) resulting in ( 5 - 3 ) == 2

To evaluate 15 / ( 5 - 3 % 7 ) * 2.0 ie. 15 / 2 * 2.0, we need to take associativity into account; multiplicative operators associate from left to right; so this is ( 15 / 2 ) * 2.0
In ( 15 / 2 ), both operands are integers, we have integer division which yields 7.
15 / ( 5 - 3 % 7 ) * 2.0 == ( 15 / 2 ) * 2.0 == 7 * 2.0, after 'usual arithmetic conversion' == 7.0 * 2.0 == 14.0

2.0 * 15 / ( 5 - 3 % 7 ) == 2.0 * 15 / 2 == ( 2.0 * 15 ) / 2 (associates from left to right)
== ( 2.0 * 15.0 ) / 2 ('usual arithmetic conversion')
== 30.0 / 2.0 ('usual arithmetic conversion')
== 15.0
Topic archived. No new replies allowed.