Survey

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Survey

Document related concepts

Transcript

Assessment test Unit 5 Algorithms Assessment Test Answers 1. (a) What is an algorithm? [2] A set of instructions for how to solve a problem or perform a task (b) The following program simulates throwing two dice. What is the output from the following program if: (i) Die1 = 3, Die2 = 4 7 (ii) Die1 = 5, Die2 =5 20 [2] Start Die1 = Random(1,6) Die2 = Random(1,6) Score = Die1 + Die 2 No Die1 = Die2? Yes Score = Score*2 Output score End 1 Assessment test Unit 5 Algorithms (c) Rewrite the flowchart as a pseudocode algorithm, making the following alterations: If the user throws a “double”, they get another turn, and the score accumulates into a total score. Their turn ends as soon as Die1 and Die2 have different values after a “throw”. Print out the total score at the end of a turn. [4] TotalScore = 0 Repeat Die1 = Random (1,6) Die2 = Random (1,6) Score = Die1 + Die2 If Die1 == Die2 then Score = Score*2 End If TotalScore = TotalScore+Score Until Die1<>Die2 Output Score (d) What is output if the user “throws” 3 times, getting 4 and 4, 3 and 3, 6 and 1 on the three throws? 16 + 12 + 7 = 35 [1] [9 marks] 2. The following list of numbers is to be searched: 103, 118, 122, 134, 138, 149, 156, 159, 163, 171, 180, 193, 199 (a) List the numbers that will be examined if a linear search is carried out to find the number 138. [2] 103, 118, 122, 134, 138 (b) List the numbers that will be examined if a binary search is carried out to find the number 138. [2] 156, 122,138 (c) Using a binary search, how many items will need to be examined to find the number 199? [1] 4 (d) Give one advantage of the linear search method and one advantage of the binary search method. [2] Linear search can be carried out on an unsorted list Binary search is much faster for a large number of items [7 marks] 2 Assessment test Unit 5 Algorithms 3. A sort is carried out on the following list of numbers. 15 6 43 21 8 17 11 3 (a) How many passes through the list will be required to sort the numbers into ascending sequence using the bubble sort algorithm? 7 [1] (b) Show how the numbers change after each comparison is made during the first pass, and the position of the numbers after the first pass is complete. The first row is completed for you. [6] 6 15 43 21 8 17 11 3 6 15 43 21 8 17 11 3 6 15 21 43 8 17 11 3 6 15 21 8 43 17 11 3 6 15 21 8 17 43 11 3 6 15 21 8 17 11 43 3 6 15 21 8 17 11 3 43 (c) Using an insertion sort, what sequence are the numbers in after 1, 2 and 3 items have been moved? [3] 15 6 43 21 8 17 11 3 6 15 43 21 8 17 11 3 6 15 43 21 8 17 11 3 6 15 21 43 8 17 11 3 [10 marks] 3 Assessment test Unit 5 Algorithms 4. Explain the principle of the merge sort, using the numbers below to illustrate the procedure. Show each stage of the process. 46 27 [8 marks] 33 21 18 28 43 13 The merge sort has two phases. In the first phase the list is divided into sublists until there are 8 lists containing 1 item each Sublists: 46, 27, 33, 21 18, 28, 43, 13 Then: 46, 27 18, 28 Then: 33, 21 46 27 33 21 18 (1 mark) 43, 13 (1 mark) 28 43 13 (1 mark) In the second stage the sublists are merged in pairs, into ascending sequence: (1 mark) 27 46 21 27 33 21 33 46 18 28 13 43 13 18 28 (1 mark) 43 (1 mark) 43 (1 mark) The final stage is to merge the two sublists giving 5. 21 27 33 46 13 18 28 13 18 21 27 28 33 43 46 (1 mark) Write a pseudocode algorithm which does the following: Asks the user to input an integer between 0 and 23, representing the hours in a 24hour clock, and accepts the user’s input, which you can assume will be valid. Outputs the equivalent time in 12 hour clock format. For example: If the user inputs 0, the output will be 12am If the user inputs 2, the output will be 2am If the user inputs 12, the output will be 12pm If the user inputs 23, the output will be 11pm [7 marks] hour = input (“Input hour between 0 and 23:”) if hour = 0 then print (12, “am”) else if hour < 12 then print (hour, “am”) else if hour = 12 print (12, “pm”) else hour = hour – 12 print (hour, “pm”) endif endif endif 4 Assessment test Unit 5 Algorithms Alternatively, use a switch/case statement: hour = input (“Input hour between 0 and 23:”) switch hour: case 0: print (12, “am”) case <12: print (hour, “am”) case 12: print (12, “pm”) default: print (hour, “pm”) endswitch 6. Below is an incomplete algorithm which is intended to check the username and password entered by a user. 1 passwordOK = false 2 count = 1 3 password = input (Please enter password”) 4 ………………………………………………………………………………………… 5 6 7 ………………………………………………………………………………………… passwordOK = True else 8 print (“password incorrect”) 9 …………………………………………………………………………………… 10 count = count + 1 11 endif 12 endwhile 13 …………………………………………………………………………………… 14 print (“Welcome back”) 15 else 16 print (“Contact administrator”) Lines 4, 5, 9 and 13 are missing from the algorithm above. Using four of the lines of code below, complete this algorithm. [4 marks] password = input (“Please re-enter password: ”)Line 9 if password == “AXcd35” Line 5 if password != “AXcd35” while count < 3 AND passwordOK == False Line 4 while count < 3 OR passwordOK == False if passwordOK == True Line 13 if passwordOK == False 5 Assessment test Unit 5 Algorithms 7. Complete the trace table below to determine the output from the following algorithm: total = 0 n = 1 while n < 6 if n mod 2 == 0 total = total + n endif n = n + 1 endwhile print(total) [5 marks] total n n mod 2 n<6 0 1 1 Yes 0 2 0 Yes 2 3 1 Yes 2 4 0 Yes 6 5 1 Yes 6 6 No OUTPUT 6 [Total 50 Marks] 6