Lecturer: Prof. B. Gärtner Informatik für Mathematiker und Physiker HS16 Solutions 3 Solution 1 Summarized: (i) (ii) (iii) (iv) (v) type int bool bool bool bool value 2 false true true true Now about how to obtain these: here we do it in more detail than you’re usually asked for, if they appear in an exam. We first show you the fully-parenthesized expressions indicating in which order subexpressions are evaluated. The parentheses are applied according to the rules of precedence and associativity. a) The parenthesized expressions are: (i) (n / f) * d (ii) u == (4 * (++d)) (iii) ((0 < d) < 2) || (2 > f) (iv) ((0 < 1) && (1 != 1)) || ((++x) > 3) (v) ((3 * d) > d) || (((1 / z) != 0) && ((3 + 4) >= 7)) b) Possible step-by-step evaluations: (i) (n / f) (n / f) (9 / f) (9 / 5) 1 * 2→ 2 * * * * d 2 2 2 → → → → Type: int 1 (ii) (Note: u == u == u == 4 == false this exercise was slightly changed compared to the one in the exam.) (4 * (++d)) → (4 * 3) → 12 → 12 → Type: bool (iii) ((0 < d) < 2) || (2 > f) → ((0 < 2) < 2) || (2 > f) → (true < 2) || (2 > f) → (1 < 2) || (2 > f) → true || (2 > f) → true Type: bool Note: Due to short circuit evaluation, the right part of the or statement will not be executed (ever!). If you tried to do so, your solution is wrong (independent of the result you might have found). (iv) ((0 < 1) && (1 != 1)) || ((++x) > 3) → (true && (1 != 1)) || ((++x) > 3) → (true && false) || ((++x) > 3) → false || ((++x) > 3) → false || (4 > 3) → false || true → true Type: bool (v) ((3 * d) > d) || (((1 / x) != ((3 * 2) > d) || (((1 / x) != (6 > d) || (((1 / x) != 0) && (6 > 2) || (((1 / x) != 0) && true || (((1 / x) != 0) && ((3 true 0) && ((3 + 4) >= 7)) → 0) && ((3 + 4) >= 7)) → ((3 + 4) >= 7)) → ((3 + 4) >= 7)) → + 4) >= 7)) → Type: bool Note: Due to short circuit evaluation, the right part of the or statement will not be executed (ever!). If you tried to do so, your solution is wrong (independent of the result you might have found). Solution 2 1 2 // I n f o r m a t i k − S e r i e 3 − Aufgabe 2 // Programm : c r o s s s u m . cpp 2 3 4 5 6 // A u t o r : . . . ( Gruppe . . . ) // Compute t h e c r o s s sum o f a n a t u r a l number . //#i n c l u d e ” t e s t s . h” // remove s l a s h e s a t b e g i n n i n g o f l i n e t o t e s t ←or submit 7 #include <i o s t r e a m > 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 int main ( ) { // i n p u t unsigned int n ; std : : cin >> n ; // c o m p u t a t i o n unsigned int cross = 0 ; for ( unsigned int m = n ; m > 0 ; m /= 1 0 ) cross += m % 1 0 ; // o u t p u t std : : cout << "Cross sum of " << n << " is: " << cross << "\n" ; return 0 ; } Solution 3 1 2 3 4 5 6 // // // // I n f o r m a t i k − S e r i e 3 − Aufgabe 3 Programm : d e c 2 b i n . cpp A u t o r : . . . ( Gruppe . . . ) Output r e v e r s e b i n a r y r e p r e s e n t a t i o n o f a number n . //#i n c l u d e ” t e s t s . h” // remove s l a s h e s a t b e g i n n i n g o f l i n e t o t e s t ←or submit 7 #include <i o s t r e a m > 8 9 10 11 12 13 14 15 16 17 int main ( ) { // i n p u t unsigned int n ; std : : cin >> n ; // c o m p u t a t i o n and o u t p u t std : : cout << "Reverse binary representation is: " ; if ( n == 0 ) // s p e c i a l c a s e ! 3 18 19 20 21 22 23 24 25 std : : cout << n ; for ( ; n > 0 ; n /= 2 ) std : : cout << n % 2 ; std : : cout << "\n" ; // no o u t p u t f o r n==0 return 0 ; } 4
© Copyright 2024 ExpyDoc