1 / 18

Introduction to Computers -3rd exam-

Introduction to Computers -3rd exam-. 授課教師 : 李錫智 教授. 1.[ 10] Suppose we have the following algorithm: abc ← -1000 pqr ← 1 while ( pqr ≦ 10) { current ← next integer If (current > abc ) abc ← current pqr ← pqr + 1 } return abc

carol
Télécharger la présentation

Introduction to Computers -3rd exam-

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Computers-3rd exam- 授課教師: 李錫智 教授

  2. 1.[10] Suppose we have the following algorithm: abc← -1000 pqr← 1 while (pqr≦ 10) { current ← next integer If (current > abc) abc← current pqr←pqr + 1 } return abc What is the value returned after each integer in the following list is processed: 28, 22, 18, 30, 20, 42, 15, 60, 35, 45

  3. Ans: pqr= 1 Current = 28, abc = -1000 28 > -1000 abc= 28 pqr= 1 + 1 = 2 pqr = 2 Current = 22, abc = 28 22 < 28 abc= 28 pqr = 2 + 1 = 3 pqr = 3 Current = 18, abc = 28 18 < 28 abc=28 pqr= 3 + 1 = 4 . . . pqr= 9 Current = 35, abc = 60 35 < 60 abc= 60 pqr = 9 + 1 = 10 pqr = 10 Current = 45, abc = 60 45 < 60 abc= 60 pqr= 10 + 1 = 11 break;

  4. 2.[10] Suppose we have the following algorithm: abc← 0 pqr← 1 while (pqr≦ 10) { current ← next integer abc←abc + current pqr←pqr + 1 } return abc What is the value returned after each integer in the following list is processed: 28, 22, 18, 30, 20, 42, 15, 60, 35, 45

  5. Ans:pqr= 1 current = 28, abc = 0 abc = abc + current = 28 + 0 = 28 pqr = 1 + 1 = 2 pqr = 2 current = 22, abc = 28 abc = abc + current = 22 + 28 = 50 pqr = 2 + 1 = 3 pqr = 3 current = 18, abc = 50 abc = abc + current = 50 + 18 = 68 . . . pqr= 9 current = 35, abc = 235 abc = abc + current = 235 + 35 = 270 pqr = 10 current = 45, abc = 270 abc = abc + current = 270 + 45 = 315 break;

  6. 3. [10] Suppose we have the following algorithm: abc← 1 pqr← 1 while (pqr≦ 10) { current ← next integer abc←abc× current pqr←pqr + 1 } return abc What is the value returned after each integer in the following list is processed: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

  7. Ans:pqr= 1 current = 1, abc = 1 abc = abc× current = 1 × 1 = 1 pqr = 2 current = 2, abc = 1 abc = abc× current = 1 × 2 = 2 pqr = 3 current = 3, abc = 2 abc = abc× current = 2 × 3 = 6 . . . pqr= 10 current = 10, abc = 362880 abc = abc× current = 362880 × 10 = 3628800 10 × 9 × 8 × 7 × 6 × 5 × 4 × 3 × 2 × 1 = 3628800

  8. 4. [10] Suppose we have the following algorithm: abc← 1 pqr← 1 while (pqr≦ 10) { abc←abc×pqr pqr←pqr + 1 } return abc What is the value returned after the algorithm is executed.

  9. Ans:pqr= 1 current = 1, abc = 1 abc = abc×pqr = 1 × 1 = 1 pqr = 2 current = 2, abc = 1 abc = abc×pqr = 1 × 2 = 2 pqr = 3 current = 3, abc = 2 abc = abc×pqr = 2 × 3 = 6 . . . pqr = 10 current = 10, abc = 362880 abc = abc×pqr = 362880 × 10 = 3628800 10 × 9 × 8 × 7 × 6 × 5 × 4 × 3 × 2 × 1 = 3628800

  10. 5. [10] Suppose we have the following algorithm: Place the wall at the leftmost part of the list while (the wall is not at the rightmost part of the list) { Find the smallest element in the right-hand side of the wall Swap the smallest element with the first element in the right-hand side of the wall Move the wall one element to the right } return the list What is the returned list after the algorithm runs on the following list: 28, 22, 18, 30, 20, 42, 15, 60, 35, 45

  11. Ans:|28 22 18 30 20 42 15 60 35 45 |28 22 18 30 20 42 15 60 35 45 |15 22 18 30 20 42 28 60 35 45 15 | 22 18 30 20 42 28 60 35 45 15 | 2218 30 20 42 28 60 35 45 15 | 1822 30 20 42 28 60 35 45 15 18 | 22 30 20 42 28 60 35 45 . . . 15 18 20 22 28 30 35 42 45 60

  12. 6.[10] Suppose you write a program to find the larger of two input integers between 100 and 199 (inclusive). What is the number of exhaustive tests to test all combinations of the two input integers? Ans: Total = 199 – 100 + 1 = 100

  13. 7. [10] Follow Problem 6. Suppose you are given a random generator to test the program. The random generator can generate a random number in the range of 0 and 1 (including 0 but excluding 1). How do you generate a pair of input integers for testing? Ans:

  14. 8. [10] Suppose we have the following algorithm: i← 0 j ← 9 while (i≦ j) { t ← A[j] A[j] ← A[i] A[i] ← t i←i + 1 j ← j - 1 } return A What is the returned A after the algorithm runs on the array A containing the following ten integers: 28, 22, 18, 30, 20, 42, 15, 60, 35, 45

  15. Ans: A[0] = 28, A[9] = 45 t  A[9], t = 45 A[9]  A[0] A[9] = 28 A[0]  t A[0] = 45

  16. 9. [10] Suppose we have a stack S. The operation push(S, x) stores x in S, and the operation pop(S,y) retrieves and deletes an element from S. Note that a stack is a last-come-first-out data type. Consider the following program: push(S, 25) push(S, 85) push(S, 55) push(S, 75) pop(S, p) pop(S, q) push(S, 95) pop(S,r) pop(S,s) What are q and s during the execution of the above program?

  17. 10.[10] Suppose you have two sequential files A and B sorted on the key values. You want to merge these two files into another sequential file C which is also sorted on the key values. Please describe an algorithm to do this merge. Note that if a record exists in both A and B, then both copies of this record must be identical and you just put one copy in C. Ans: 1.combine A with B into sequential C 2.sort C 3.delete repetitive recordand remain the one.

More Related