Chapter 1 An Introduction to Computer Science INVITATION TO Computer Science 1
Q8. Under what conditions would the well-known quadratic formula not be effectively computable? (Assume that you are working with real numbers.) a quadratic equation is a univariate polynomial equation of the second degree. Invitation to Computer Science, 6th Edition 2
Answer It is not effectively computable if b 2 4ac < 0 (since we cannot take the square root of a negative number) or if a = 0 (since we cannot divide by 0). Invitation to Computer Science, 6th Edition 3
Q10. The following is Euclid s 2,300-year-old algorithm for finding the greatest common divisor of two positive integers I and J. Step 1 Operation Get two positive integers as input; call the larger value I and the smaller value J 2 Divide I by J, and call the remainder R 3 If R is not 0, then reset I to the value of J, reset J to the value of R, and go back to Step 2 4 Print out the answer, which is the value of J 5 Stop Invitation to Computer Science, 6th Edition 4
1. Go through this algorithm using the input values 20 and 32. After each step of the algorithm is completed, give the values of I, J, and R. Determine the final output of the algorithm. 2. Does the algorithm work correctly when the two inputs are 0 and 32? Describe exactly what happens, and modify the algorithm so that it gives an appropriate error message. Step 1 Operation Get two positive integers as input; call the larger value I and the smaller value J 2 Divide I by J, and call the remainder R 3 If R is not 0, then reset I to the value of J, reset J to the value of R, and go back to Step 2 4 Print out the answer, which is the value of J 5 Stop Invitation to Computer Science, 6th Edition 5
Answer (a) Trace: Step 1: I = 32, J = 20, and R =?? Step 2: I = 32, J = 20, and R = 12 Step 3: I = 20, J = 12, and R = 12 Step 2: I = 20, J = 12, and R = 8 Step 3: I = 12, J = 8, and R = 8 Step 2: I = 12, J = 8, and R = 4 Step 3: I = 8, J = 4, and R = 4 Step 2: I = 8, J = 4, and R = 0 Step 4: Print J = 4 Invitation to Computer Science, 6th Edition 6
Answer (b) At Step 2 we are asked to divide I = 32 by J = 0, which cannot be done. We can fix the problem by adding a step between Step 1 and Step 2 that says: If J = 0, then print ERROR: division by 0 and Stop. Invitation to Computer Science, 6th Edition 7
Q 13. A student was asked to develop an algorithm to find and output the largest of three numerical values x, y, and z that are provided as input. Here is what was produced: Invitation to Computer Science, 6th Edition 8
Answer The algorithm will work correctly only if all three numbers are unique. If two or more numbers are identical, none of the Boolean expressions will be true and nothing will be output. To make this a correct solution you either have to specify in the problem statement that the three numbers provided must all be distinct or (better) change all of the comparison operations to in place of >. Invitation to Computer Science, 6th Edition 9
Quiz Q12 One way to do multiplication is by repeated addition. For example, 47 25 can be evaluated as 47 + 47 + 47 + + 47 (25 times). Sketch out an algorithm for multiplying two positive numbers a and b using this technique. Invitation to Computer Science, 6th Edition 10
Invitation to Computer Science, 6th Edition 11
Answer 12 A Multiplication Algorithm. Given: Two positive numbers a and b Wanted: A number c which contains the result of multiplying a and b Step 1: Set the value of c equal to 0 Step 2: Set the value of i equal to b Step 3: Repeat steps 4 and 5 until the value of i is 0 Step 4: Set the value of c to be c + a Step 5: Subtract 1 from i Step 6: Print out the final answer c Step 7: Stop This algorithm assumes that we know how to add two multiple-digit numbers together. We may assume this because we have the algorithm from the book which does exactly that. Invitation to Computer Science, 6th Edition 12
Challenge Question #1 Assume we have a computing agent that knows how to do onedigit subtraction where the first digit is at least as large as the second (i.e., we do not end up with a negative number). Thus, our computing agent can do such operations as 7 3 = 4, 9 1 = 8, and 5 5 = 0. It can also subtract a one-digit value from a two-digit value in the range 10 18 as long as the final result has only a single digit. This capability enables it to do such operations as 13 7 = 6, 10 2 = 8, and 18 9 = 9. Using these primitive capabilities, design an algorithm to do decimal subtraction on two m-digit numbers, where m 1. You will be given two unsigned whole numbers a mm1 a mm2 a 0 and b mm1 b mm2 b 0. Your algorithm must compute the value c mm1 c mm2 c 0, the difference of these two values. Invitation to Computer Science, 6th Edition 13
You may assume that the top number (a m1 a mm2 a 0 ) is greater than or equal to the bottom number (b mm1 b mm2 b 0 ) so that the result is not a negative value. However, do not assume that each individual digit a i is greater than or equal to b i. If the digit on the bottom is larger than the digit on the top, then you must implement a borrowing scheme to allow the subtraction to continue. (Caution: It may have been easy to learn subtraction as a first grader, but it is devilishly difficult to tell a computer how to do it!) Invitation to Computer Science, 6th Edition 14
Hint: We may perform subtraction, like addition, by subtracting one column at a time, starting with the rightmost column and working to the left. Since we know that the first number is larger than the second one, we know that we can always borrow from columns to the left of the current one. Therefore, if the upper number in the column (a i ) is smaller than the lower, we automatically borrow from the previous column. We can do this by subtracting one from the a i+1 value of the column to the left. If the a i+1 value were already zero, then it would become - 1. This automatically causes a borrow to occur on the next step. Invitation to Computer Science, 6th Edition 15