Key for Homework 1 on Core C++ Syntax Q A Reason ----------------------------------------------------------------------------------- 1 4 6.0 / 8.0 + 5 / 2 == 0.75 + 2 double division + integer division == 2.75 mixed double and int 2 3 9 / 2 * 2 == (9 / 2) * 2 precedence rules == 4 * 2 integer division 3 4 2 * 9 / 2 == (2 * 9) / 2 precedence rules == 18 / 2 integer multiplication == 9 integer division 4 5 14 % 4 == 2 since dividing 14 by 4 leaves a remainder of 2 3 % 4 == 3 since dividing 3 by 4 leaves a remainder of 3 5 1 4 / 5 == 0 (integer division); this is then converted to a double when the assignment is carried out. 6 2 4 / 5.0 == 0.8 (mixed division) 7 1 4 / 5 == 0 (integer division) 8 1 4 / 5.0 == 0.8 (mixed division) again, but now the result is converted to an integer when the assignment is carried out. 9 3 5 / 2.0 == 2.5 (mixed arithmetic so 5 is converted to a double) 10 1 By the precedence rules for algebra, the multiplication is done before the addition, so this is equivalent to (ab) + c. 11 3 By the precedence rules for algebra, the addition is performed before the division operation, so this is equivalent to (a + b) / c. The second answer would be correct if the variables were not integers, but integer division could cause the wrong result; for example 3/5 + 2/5 is 0. 12 2 By the precedence rules for algebra, the addition is performed before the division operation, so this is equivalent to a / (b + c). 13 3 Each of the others manages to group the operations in the wrong way. 14 3 (x + y + z + w) / 4 == (4 + 8 + 5 + 4) / 4 == 21 / 4 == 5 This value is then converted to a double, 5.0, for the assignment. 15 1 Straight out of the course notes.