CMPS 10 Winter 2011- Homework Assignment 2 Problems: Chapter 2 (p.75): 1abc, 2, 4, 9, 12, 13, 15, 16abc, 17ab, 18ab 1. Write pseudocode instructions to carry out each of the following computational operations. a.) Determine the area of a triangle given values for the base b and the height h. b.) Compute the interest earned in 1 year given the starting account balance B and the annual interest rate I and assuming simple interest, that is, no compounding. Also determine the final balance at the end of the year. c.) Determine the flying time between two cities given the mileage M between them and the average speed of the airplane. Solutions: (a): Set the value of area to ½ * b * h. (b): Set the value of FinalBalance to (1 + I) * B, assuming I is represented as a decimal fraction. Interest only is equal to I * B. (c): Set the value of FlyingTime to M/AvgSpeed. 2. Using only the sequential operations described in Section 2.2.2, write an algorithm that gets values for the starting account balance B, annual interest rate I, and annual service charge S. Your algorithm should output the amount of interest earned during the year and the final account balance at the end of the year. Assume that the interest is compounded monthly and the service charge is deducted once, at the end of the year. Solution: Here is one possible solution Step Operation 1.) Input values for B, I, and S 2.) Set the value of FinalBalance to B * (1 + I/12)12 3.) Set the value of TotalInterest to FinalBalance – B 4.) Set the value of FinalBalance to FinalBalance – S 5.) Print the message “Total Interest Earned: “, and print the value of TotalInterest 6.) Print the message “Final Balance: “, and print the value of FinalBalance 4. Write an algorithm that gets the price for item A plus the quantity purchased. The algorithm prints out the total cost, including a 6% sales tax. Solution: One possible solution is: Step Operation 1.) Input values for A and N (the quantity purchased). 2.) Set the value of TotalCost to 1.06 * (A * N) 3.) Print out the value of TotalCost. 9. Modify the sales algorithm of Exercise 4 so that after finishing the computation for one item, it starts on the computation for the next. This iterative process is repeated until the total cost exceeds $1000. Solution: Following directly from the previous algorithm of problem 4, we obtain: Step 1.) 2.) 3.) 4.) 5.) Operation Set TotalCost to 0. While TotalCost is less than or equal to $1000, repeat steps 3 and 4. Input values for A and N (the quantity purchased). Set the value of TotalCost to TotalCost + (1.06 * (A * N)). Print out the value of TotalCost. 12. Develop a formal argument that “proves” that the sequential search algorithm shown in figure 2.13 cannot have an infinite loop; that is, prove that it will always stop after a finite number of operations. Solution: Steps 1, 2, 5, 6, 7, 9, and 10 are sequential operations and thus cannot go into an infinite loop. Steps 4 and 8 are conditionals, and also cannot go into an infinite loop. Step 3 is the only one left to consider, and since it is a repeating loop, it could potentially become an infinite loop. The looping conditions is “if Found = no and i 10,000.” If Found ever becomes yes (the algorithm finds the name), then the condition is no longer satisfied and the loop will end. If NAME is never found, then line 7 adds 1 to i per iteration. Since i starts at 1 (from line 2), it will advance to 10,001 after 10,000 iterations of the loop. At this point, the second portion of the condition will no longer be true and the loop will halt. Then lines 8, 9, and 10 will be executed, and the program will stop. 13. Modify the sequential search algorithm of Figure 2.13 so that it works correctly even if the names in the directory are not unique, that is, if the desired name occurs more than once. Your modified algorithm should find every occurrence of NAME in the directory and print out the telephone numbers corresponding to every match. In addition, after all the numbers have been displayed, your algorithm should print out how many occurrences of NAME were located. For example, if NAME occurred three times, the output of the algorithm might look something like this: 528-5638 922-7874 488-2020 A total of three occurrences were located. Solution: Here is one possible solution to the problem. numbers from the directory. Let N denote names and T denote telephone Step Operation 1.) Get values for NAME, N1, … , N10000 and T1, … , T10000 2.) Set the value of i to 1, and set the value of NumberFound to 0. 3.) Repeat steps 4 through 7 while i 10,000. 4.) If Ni = NAME, then 5.) Print Ti 6.) Set the value of NumberFound to NumberFound + 1. 7.) Add 1 to the value of i. 8.) Print “A total of <NumberFound> occurrences were located.” 9.) STOP. 15. With regards to the Find Largest algorithm of Figure 2.14, if the numbers in our list were not unique and therefore the largest number could occur more than once, would the algorithm find the first occurrence? The last occurrence? Every occurrence? Explain precisely how this algorithm would behave when presented with this new condition. Solution: This algorithm will find the first occurrence of the largest element in the collection. This first occurrence will become LargestSoFar, and from then on, Ai will be tested to see if it is greater than LargestSoFar. Some of the other elements are equal to LargestSoFar but since none are greater, the first element will be retained. 16. On the sixth line of the Find the Largest algorithm of Figure 2.14 there is an instruction that reads: While (i n) do Explain exactly what would happen if we changed that instruction to read as follows: a.) While (i n) do b.) While (i < n) do c.) While (i = n) do Solution: a.) If n < 2, then the test would be true, so the loop would be executed. In fact, the algorithm would enter an infinite loop because the condition (i n) would never become false, or the algorithm would generate an error when attempting to refer to an invalid Ai value. On the other hand, if n > 2, then the test condition would be false at the first check, and the loop would be skipped entirely. Thus A1 would be reported as the largest value, probably erroneously. b.) In the case of (i < n), the algorithm would work almost correctly. Instead of finding the largest value of the 1st through nth element, it would find the largest value of the 1st through nth – 1 element because the loop would exit when n = i. c.) If n = 2, then the loop would execute once, comparing the A1 and A2 values to determine which was larger. For any other value of n, the loop would never execute, and the value of A1 would be reported. 17. On the seventh line of the Find the Largest algorithm of Figure 2.14 is an instruction that reads: If Ai > largest so far then… Explain exactly what would happen if we changed that instruction to read as follows: a.) If Ai largest so far then … b.) If Ai < largest so far then … Solution: a.) The algorithm would still correctly find the largest element on the list. However, if the largest element was duplicated, this algorithm would find the last occurrence of the largest element instead of the first. b.) The algorithm would find the smallest element on the list instead of the largest element, completely reversing the goal of the algorithm. 18. a.) Refer to the pattern-matching algorithm in Figure 2.16. What is the output of the algorithm as it currently stands if our text is: Text: We must band together and handle adversity and we search for the pattern “and”? b.) How could we modify the algorithm so that it finds only the complete word and rather than the occurrence of the character sequence a, n, and d that are contained within another word such as band? Solution: a.) The algorithm will find three occurrences of the pattern “and.” The first occurrence is the last three characters of “band.” The second occurrence is the word “and.” The third occurrence is the “and” portion of “handle.” Remembering that the counter starts at 1 for “W” and spaces count as part of the text, the output of the program will be: There is a match at position 10 There is a match as position 23 There is a match as position 28 b.) In order to find only complete words, the algorithm must also examine the characters immediately before and after the word. Searching for spaces on either side would catch all normal stand-alone instances of the word. In other words, search for “ and ”. However, this does not account for possibilities when the word is inside quotation marks or has other punctuation surrounding it. For example, the string “and?” would not match with the above method. Therefore, in addition to searching for spaces, also search for punctuation surrounding the word. Lastly, even searching for punctuation may not be enough. If you wanted to search for the word “an” and were scanning a text with the word “Qur’an” would produce a positive match because it would find the string “’an “. All in all, one needs to be very careful in construction rules of this nature.