WGU C960 Discrete Math 2 Questions and Answers Graded A+
WGU C960 Discrete Math 2 Questions and Answers Graded A+ 1.4.3: If-else-statement 1) What is the value of abs after the following lines of code are run? x := 2 If ( x 0 ) abs := x Else abs := -x End-if 2 The condition ( x 0 ) evaluates to true because the value of x is 2. The line abs := x is executed and the line abs := -x is skipped. Therefore the final value of abs is 2. 1.4.3: If-else-statement 2) What is the value of abs after the following lines of code are run? x := -2 If ( x 0 ) abs := x Else abs := -x End-if 2 The condition ( x 0 ) evaluates to false because the value of x is -2. The line abs := x is skipped and the line abs := -x is executed. Therefore the final value of abs is -x, which is -(-2) = 2. 1.5.2: For-loops Consider the following pseudocode fragment: sum := 0 For i = 2 to 5 sum := sum + i End-for 1) What is the value of sum after the second iteration? 2) How many iterations will the for-loop execute? 3) What is the final value for sum after executing the for-loop? 1) 5 In the first iteration, i = 2. In the second iteration, i = 3. 2 + 3 = 5 2) 4 i = 2, 3, 4, 5. Four iterations 3) 14 i = 2, 3, 4, 5. sum = 2 + 3 + 4 + 5 1.6.2: While-loops product := 1 count := 5 While ( count 0 ) product := product⋅count count := count - 2 End-while 1) What is the value of product after the second iteration? 2) How many iterations will the while-loop execute? 3) What is the final value for product? 1) 15 In the first iteration, count = 5. In the second iteration, count
Document information
- Uploaded on
- September 17, 2023
- Number of pages
- 158
- Written in
- 2023/2024
- Type
- Exam (elaborations)
- Contains
- Questions & answers