Review Problems and Solution1 - Review Problems and...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Review Problems and Solutions (Algorithms and Python Programming) 1. From Assignment 4 Consider the following algorithm printing a part of the Fibonacci sequence: 1. Last <- 0 2. Current <- 1 3. While (Current < 100) do 4. print Current 5. Temp <- Last 6. Last <- Current 7. Current <- Last + Temp 8. End While Where is the initialization step? The modification step? The test step? What list of numbers is produced? Answer: The body of the loop is everything between "While" and "End While". The initialization step is " Current <- 1 ". The modification step is " Current <- Last + Temp ". The test step is " Current < 100 " in the loop header. Program prints the Fibonacci sequence < 100: 1 1 2 3 5 8 13 21 34 55 89 2. Wages Algorithm from Assignment 4 Write an algorithm that computes the net weekly wages for an hourly employee. The algorithm should accept as input hours-worked and hourly-rate, and income tax-rate. The income tax should be deducted from gross wages. If the employee has worked more than 40 hours then the overtime hours (hours greater than 40) will earn 1.5 times the hourly- rate. Answer: Here is the algorithm: 1. Input HRS_WORKED, PAY_RATE, TAX_RATE 2. If (HRS_WORKED <= 40) then 3. GROSS <- HRS_WORKED * PAY_RATE 4. Else 5. OVERTIME_HRS <- HRS_WORKED - 40 6. GROSS <- (40 * PAY_RATE) + (1.5 * PAY_RATE * OVERTIME_HRS) 7. End If 8. INC_TAX <- TAX_RATE * GROSS 9. NET_INCOME <- GROSS - INC_TAX 10. Print NET_INCOME 11. End 3. What letters are interrogated by Binary Search if applied to the list: L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z when searching for U, and when searching for B? Answer: Let's look at the case of searching for U. First the middle element is found. Index of the last element is 15, so the middle position is (1+15)/2 = 8. The 8th element is S. Since U is larger than S, we do a binary search in the upper half (from position 9
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
through position 15). The middle element in this part is (9+15)/2 = 12. The 12th element is W. But, U is smaller than W, so now we apply the binary search to the list between positions 9 through 11). The middle position here is (9+11)/2 = 10. The 10th position is U, so we stop. In the case of searching for "B" (which is not in the list), we again find the middle element (at position 8), which is S. Since B is less than S, we do a binary search in the lower half (from position 1 to position 7). In this case, the new middle is position (1+7)/2 = 4, which is O. B is still less than O, so we again do a binary search, this time from position 1 through position 3. This leads to the interrogation of the element at location (1+3)/2 = 2, which is M. B is still less than M, so we do another binary search from location 1 to location 1, i.e., the exact element L. Given that L is larger than B and we have run out of elements to consider, we end in failure. List of items examined by binary search for U and B is:
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/19/2011 for the course CSCI 101 taught by Professor Dr.ambrose during the Spring '11 term at Community College of Baltimore County.

Page1 / 6

Review Problems and Solution1 - Review Problems and...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online