Invitation to computer science 7th edition schneider test bank

Page 1

Chapter 02: Algorithm Discovery and Design

Invitation to Computer Science 7th Edition Schneider Test Bank Full clear download (no formatting errors) at: http://testbanklive.com/download/invitation-to-computer-science-7th-edition-schneider-testbank/ Invitation to Computer Science 7th Edition Schneider Solutions Manual Full clear download (no formatting errors) at: http://testbanklive.com/download/invitation-to-computer-science-7th-edition-schneidersolutions-manual/ 1. An unstructured, ―free-flowing‖ writing style should be used for writing algorithms. a. True b. False ANSWER: False POINTS: 1 REFERENCES: 44 2. With a natural language, different readers can interpret the same sentence in totally different ways. a. True b. False ANSWER: True POINTS: 1 REFERENCES: 45 3. Java and C++ are examples of pseudocode languages. a. True b. False ANSWER: False POINTS: 1 REFERENCES: 46 4. The three basic sequential operations are called addition, multiplication, and exponentiation. a. True b. False ANSWER: False POINTS: 1 REFERENCES: 47 5. Input and output enable the computing agent to communicate with the outside world. a. True b. False ANSWER: True POINTS: 1


Chapter 02: Algorithm Discovery and Design REFERENCES: 49 6. The if/then/else operation allows you to select exactly one of three alternatives. a. True b. False ANSWER: False POINTS: 1 REFERENCES: 51 7. One of the most powerful features of a computer is its ability to handle loops. a. True


Chapter 02: Algorithm Discovery and Design b. False ANSWER: True POINTS: 1 REFERENCES: 53 8. Having an infinite loop in an algorithm is an error. a. True b. False ANSWER: True POINTS: 1 REFERENCES: 54 9. Once an algorithm has been developed, it may itself be used in the construction of other algorithms. a. True b. False ANSWER: True POINTS: 1 REFERENCES: 70 10. Pattern matching can only be applied to graphics and pictures. a. True b. False ANSWER: False POINTS: 1 REFERENCES: 77 11. Natural language is a set of English language constructs designed to resemble the statements in a programming language but that do not actually run on a computer. ANSWER: False - Pseudocode POINTS: 1 REFERENCES: 47 12. Pseudocode is a formal language with rigidly standardized syntactic rules and regulations. ANSWER: False - is not, isn‘t POINTS: 1 REFERENCES: 47 13. A(n) control algorithm executes its instructions in a straight line from top to bottom and then stops. ANSWER: False - sequential, straight-line POINTS: 1 REFERENCES: 51 14. The use of high-level instructions during the design process is an example of abstraction.


Chapter 02: Algorithm Discovery and Design ANSWER: True POINTS: 1 REFERENCES: 80 15. The process of searching for a special pattern of symbols within a larger collection of information is called object matching. ANSWER: False - pattern POINTS: 1 REFERENCES: 77 16. During the initial phases of design, we should be thinking and writing at a highly ANSWER: abstract POINTS: 1 REFERENCES: 46

level.

17. is sometimes called a programming language without any details. ANSWER: Pseudocode POINTS: 1 REFERENCES: 47 18. ANSWER: Control POINTS: 1 REFERENCES: 51 19. In a(n) ANSWER: pretest POINTS: 1 REFERENCES: 57

operations allow us to alter the normal sequential flow of control in an algorithm.

loop, it is possible for the loop body to never be executed.

20. The process of finding a solution to a given problem is called ANSWER: algorithm POINTS: 1 REFERENCES: 65

discovery.

21.

is an example of a natural language. a. C b. Java c. English d. Perl ANSWER: c POINTS: 1 REFERENCES: 44 22. In the line of code, ―Set the value of Area to length*width‖, ―Area‖ is a a. value b. variable

.


Chapter 02: Algorithm Discovery and Design c. constant d. primitive ANSWER: b POINTS: 1 REFERENCES: 48 23. A(n) is a named storage location that can hold a data value. a. expression b. variable c. computation d. constant ANSWER: b POINTS: 1 REFERENCES: 48 24. operations provide the computing agent with data values from the outside world that it may then use in later instructions. a. Ingoing b. Outgoing c. Input d. Output ANSWER: c POINTS: 1 REFERENCES: 49 25.

operations send results from the computing agent to the outside world. a. Input b. Put c. Send d. Output ANSWER: d POINTS: 1 REFERENCES: 49 26. A purely algorithm is sometimes termed a straight-line algorithm. a. sequential b. conditional c. iterative d. control ANSWER: a POINTS: 1 REFERENCES: 51 27. Together, conditional and iterative operations are called a. sequential b. control c. hierarchical d. dynamic ANSWER: b POINTS: 1 REFERENCES: 51 28.

operations.

statements are the ―question-asking‖ operations of an algorithm. a. Primitive b. Iterative c. Sequential d. Conditional


Name:

Class:

Date:

Chapter 02: Algorithm Discovery and Design ANSWER: d POINTS: 1 REFERENCES: 51 29. A is the repetition of a block of instructions. a. cycle b. nucleus c. matrix d. loop ANSWER: d POINTS: 1 REFERENCES: 53 30. An algorithm can fall into an infinite loop when . a. the input operations are missing b. the algorithm uses more than one loop c. the output operations are missing d. the continuation condition of the loop never becomes false ANSWER: d POINTS: 1 REFERENCES: 54 31. In a pretest loop, the continuation condition is tested at the through the loop. a. beginning of each pass b. beginning of only the first pass c. end of each pass d. end of only the last pass ANSWER: a POINTS: 1 REFERENCES: 57 32. The loop is an example of a posttest loop. a. do/while b. do c. while d. if/then/else ANSWER: a POINTS: 1 REFERENCES: 57 33. To create a loop that executes exactly b times, we create a a. control object b. counting method c. counter d. variable ANSWER: c POINTS: 1 REFERENCES: 61 34. ―Print the value of product‖ is an example of a(n) a. sequential b. conditional c. input d. output

.

operation.


Name:

Class:

Date:

Chapter 02: Algorithm Discovery and Design ANSWER: d POINTS: 1 REFERENCES: 63 35. The technique of looking at all the items in a list, starting at the beginning of the list, one at a time, until we either find what we are looking for or come to the end of the list is called search. a. sequential b. control c. iterative d. random ANSWER: a POINTS: 1 REFERENCES: 66 36. The selection of an algorithm to solve a problem is greatly influenced by the way the input organized. a. words b. data c. solutions d. pseudocode ANSWER: b POINTS: 1 REFERENCES: 69

for that problem are

37. A(n) is a collection of useful, prewritten algorithms. a. primitive b. binary c. set d. library ANSWER: d POINTS: 1 REFERENCES: 71 38. In order to implement a ―find‖ functionality in a word processor, one would have to design a a. pattern matching b. natural language c. sequential d. do-while ANSWER: a POINTS: 1 REFERENCES: 77

algorithm.

39. Which statement exemplifies abstraction? a. The president of General Motors views the company in terms of every worker, every supplier, and every car. b. The president of General Motors views the company in terms of its corporate divisions and high-level policy issues. c. A good approach to algorithm design and software development is to focus on how we might actually implement a particular operation. d. A convenient way to view the hardware component called ―memory‖ is to focus on the billions of electronic devices that go into constructing a memory unit. ANSWER: b POINTS: 1


Name:

Class:

Date:

Chapter 02: Algorithm Discovery and Design REFERENCES: 81 40. Viewing an operation at a high level of abstraction and fleshing out the details of its implementation at a later time is known as design. a. bottom-up b. top-down c. increasing size d. increasing depth ANSWER: b POINTS: 1 REFERENCES: 81 41. Briefly describe what pseudocode is and is not. ANSWER: Pseudocode is not a precise set of notational rules to be memorized and rigidly followed. It is a flexible notation that can be adjusted to fit your own view about how best to express ideas and algorithms. POINTS: 1 REFERENCES: 48 TOPICS: Critical Thinking 42. Under what circumstances would the body of a pretest loop never be executed? ANSWER: With a pretest loop, the continuation condition is tested at the beginning of each pass through the loop, and therefore it is possible for the loop body never to be executed. This would happen if the continuation condition were initially false. POINTS: 1 REFERENCES: 57 TOPICS: Critical Thinking 43. Briefly define the concept of iteration ANSWER: The powerful algorithmic concept of iteration means that instead of writing instruction 10,000 separate times, it is far better to write it only once and indicate that it is to be repetitively executed 10,000 times, or however many times it takes to obtain the answer. POINTS: 1 REFERENCES: 67 TOPICS: Critical Thinking 44. What is the definition of a library in terms of algorithms? ANSWER: In the world of algorithms, a library is a collection of useful, prewritten algorithms, which are an important tool in the design and development of algorithms. POINTS: 1 REFERENCES: 71 TOPICS: Critical Thinking 45. What is pattern matching? ANSWER: Pattern matching is the process of searching for a special pattern of symbols within a larger collection of information. POINTS: 1 REFERENCES: 77


Name:

Class:

Date:

Chapter 02: Algorithm Discovery and Design TOPICS:

Critical Thinking

46. What is the problem with using natural language to represent algorithms? ANSWER: Natural language can be extremely verbose, causing the resulting algorithms to be rambling, unstructured, and hard to follow. An unstructured, ―free-flowing‖ writing style might be wonderful for novels and essays, but it is horrible for algorithms. The lack of structure makes it difficult for the reader to locate specific sections of the algorithm because they are buried inside the text. For example, without any clues to guide us, such as indentation, line numbering, or highlighting, locating the beginning of a loop can be a daunting and time-consuming task. A second problem is that natural language is too ―rich‖ in interpretation and meaning. Natural language frequently relies on either context or a reader‘s experiences to give precise meaning to a word or phrase. This permits different readers to interpret the same sentence in totally different ways. This may be acceptable, even desirable, when writing poetry or fiction, but it is disastrous when creating algorithms that must always execute in the same way and produce identical results. POINTS: 1 REFERENCES: 44-46 TOPICS: Critical Thinking 47. What is the problem with using high-level programming languages to represent algorithms? ANSWER: As an algorithmic design language, this notation is also seriously flawed. During the initial phases of design, we should be thinking and writing at a highly abstract level. Using a programming language to express our design forces us to deal immediately with detailed language issues, such as punctuation, grammar, and syntax. These technical details clutter our thoughts and at this point in the solution process are totally out of place. When creating algorithms, a programmer should no more worry about semicolons and capitalization than a novelist should worry about typography and cover design when writing the first draft. POINTS: 1 REFERENCES: 46-47 TOPICS: Critical Thinking 48. What is pseudocode and why is it well-suited for representing algorithms? ANSWER: Most computer scientists use a notation called pseudocode to design and represent algorithms. This is a set of English language constructs designed to resemble statements in a programming language but that do not actually run on a computer. Pseudocode represents a compromise between the two extremes of natural and formal languages. It is simple, highly readable, and has virtually no grammatical rules. (In fact, pseudocode is sometimes called a programming language without the details.) However, because it contains only statements that have a well-defined structure, it is easier to visualize the organization of a pseudocode algorithm than one represented as long, rambling natural-language paragraphs. In addition, because pseudocode closely resembles many popular programming languages, the subsequent translation of the algorithm into a computer program is relatively simple. Pseudocode is not a formal language with rigidly standardized syntactic and semantic rules and regulations. On the contrary, it is an informal design notation used solely to express algorithms. One of the nice features of pseudocode is that you can adapt it to your own personal way of thinking and problem solving. POINTS: 1 REFERENCES: 47 TOPICS: Critical Thinking 49. Explain the importance of the concept of building blocks in the use of algorithms.


Name:

Class:

Date:

Chapter 02: Algorithm Discovery and Design The use of a ―building-block‖ component is a very important concept in computer science. You might think that every algorithm you write must be built from only the most elementary and basic of primitives. However, once an algorithm has been developed, it may itself be used in the construction of other, more complex algorithms. This is similar to what a builder does when constructing a home from prefabricated units rather than bricks and boards. Our problem-solving task need not always begin at the beginning but can instead build on ideas and results that have come before. Every algorithm that we create becomes, in a sense, a primitive operation of our computing agent and can be used as part of the solution to other problems. That is why a collection of useful, prewritten algorithms, called a library, is such an important tool in the design and development of algorithms. POINTS: 1 REFERENCES: 71 TOPICS: Critical Thinking ANSWER:

50. Discuss in detail the application of pattern matching to the mapping of the human genome. ANSWER: One of the most interesting and exciting applications of pattern matching is assisting microbiologists and geneticists studying and mapping the human genome, the basis for all human life. The human genome is composed of a sequence of approximately 3.5 billion nucleotides, each of which can be one of only four different chemical compounds. These compounds (adenine, cytosine, thymine, guanine) are usually referred to by the first letter of their chemical names: A, C, T, and G. Thus, the basis for our existence can be rendered in a very large ―text file‖ written in a four-letter alphabet (e.g., T C G G A C T A A C A T C G G G A T C G A G A T G …) Sequences of these nucleotides are called genes. There are about 25,000 genes in the human genome, and they determine virtually all of our physical characteristics— sex, race, eye color, hair color, and height, to name just a few. Genes are also an important factor in the occurrence of certain diseases. A missing or flawed nucleotide can result in one of a number of serious genetic disorders, such as Down syndrome or Tay-Sachs disease. To help find a cure for these diseases, researchers are attempting to locate individual genes that, when exhibiting a certain defect, cause a specific malady. A gene is typically composed of thousands of nucleotides, and researchers generally do not know the entire sequence. However, they may know what a small portion of the gene—say, a few hundred nucleotides— looks like. Therefore, to search for one particular gene, they must match the sequence of nucleotides that they do know, called a probe, against the entire 3.5 billion-element genome to locate every occurrence of that probe. From this matching information, researchers hope to isolate specific genes. When a match is found, researchers examine the nucleotides located before and after the probe to see whether they have located the desired gene and, if so, to see whether the gene is defective. Physicians hope someday to be able to ―clip out‖ a bad sequence and insert in its place a correct sequence. POINTS: 1 REFERENCES: 77-78 TOPICS: Critical Thinking

Invitation to Computer Science 7th Edition Schneider Solutions Manual Full clear download (no formatting errors) at:


Name:

Class:

Date:

Chapter 02: Algorithm Discovery and Design

http://testbanklive.com/download/invitation-to-computer-science-7th-editionschneider-solutions-manual/ Invitation to Computer Science 7th Edition Schneider Test Bank Full clear download (no formatting errors) at: http://testbanklive.com/download/invitation-to-computer-science-7th-editionschneider-test-bank/

Solutions to End-of-Chapter Exercises Chapter 1: An Introduction to Computer Science 1. There is no one correct answer. Common examples are the instructions for using a voice mail system, the instructions for opening a mail box lock, and the instructions for doing laundry. 2. A heuristic is a method for finding a reasonably close, ―good enough‖ solution to a problem. It can be viewed as a rule-of-thumb, a method of approximation, an informal technique, or even a way to make an ―educated guess.‖ It differs from the concept of an algorithm in that it does not guarantee to produce an optimal solution, just to make a good faith attempt to locate a reasonable one. Heuristics are often used when executing an algorithm might be too timeconsuming, and we only need an approximation to the correct answer. An example of a heuristic for adding two 3-digit numbers, such as 234 + 567, might be: 1. Set the one and tens digit of both operands to 0 2. Increase the hundreds digit of the second operand by 1. These two steps result in changing the problem to the simpler one 200 + 600. 3. Add the hundreds digits, resulting in a final ―answer‖ of 800. Now, of course, this is not the correct answer, which is 801. But the result we get may be close enough for our needs, and it is certainly a lot easier to add a single column of numbers rather than three columns of numbers. 3. One may argue that the instruction is not well-ordered, since it is unclear whether one should enter the channel first or press CHAN first. Also, it may not be effectively computable if you desire to enter a channel that is out of the DVR‘s range. 4. (a) (b) (c) (d)

Sequential Conditional Sequential Iterative

5.

Step 1: carry = 0, c3 = ??, c2 = ??, c1 = ??, and c0 = ?? Step 2: i = 0, all others unchanged Step 4: c0 = 18, all others unchanged Step 5: c0 = 8 and carry = 1, all others unchanged


Name:

Class:

Chapter 02: Algorithm Discovery and Design Step 6: i = 1, carry = 1, c3 = ??, c2 = ??, c1 = ??, and c0 = 8 Step 4: c1 = 7, all others unchanged Step 5: carry = 0, all others unchanged Step 6: i = 2, carry = 0, c3 = ??, c2 = ??, c1 = 7, and c0 = 8

Date:


Name:

Class:

Date:

Chapter 02: Algorithm Discovery and Design Step 4: c1 = 1, all others unchanged Step 5: carry = 0, all others unchanged Step 6: i = 3, carry = 0, c3 = ??, c2 = 1, c1 = 7, and c0 = 8 Step 7: c3 = 0, c2 = 1, c1 = 7, and c0 = 8 Step 8: Print out 0178. 6. Replace Step 8 with the following steps: Step 8: Set the value of i to m Step 9: Repeat step 10 until either ci is not equal to 0 or i < 0 Step 10:

Subtract 1 from i, moving one digit to the right

Step 11: If i > 0 then print cici-1 . . . c0 7. Assume that a has n digits an-1, ‌ , a0, and b has m digits, bm-1, ‌ , b0, with n not necessarily equal to m. Add an operation at the beginning of the algorithm that resets the two numbers to the same number of digits by adding non-significant leading zeros to the shorter one. We can then reuse the algorithm of Figure 1.2. If (m > n) then Set i to 0 While (n+i < m) Add a leading zero to the number at position an+i Increment i by 1 End of the loop Else If (n > m) Set i to 0 While (m+i < n) Add a leading zero to the number at position bm+i Increment i by 1 End of the loop We have now made the two numbers equal in length. All we need do now is set the variable m to the larger of the two values: Set m to the larger of m and n. The addition algorithm in Figure 1.2 will now work correctly. Note that if m and n are equal in value, neither of the Boolean expressions will be true, and neither of the conditional statements will be executed.


Name:

Class:

Date:

Chapter 02: Algorithm Discovery and Design

8. It is not effectively computable if b2 – 4ac < 0 (since we cannot take the square root of a negative number if we are limited to real numbers) or if a = 0 (since we cannot divide by 0). 9. The first algorithm (Figure 1.3(a)) is a better general purpose algorithm. If you want to shampoo your hair any number n times you can change the 2 to n. You could even ask the shampooer to input the desired number n of washings. For the second algorithm you would have to rewrite the algorithm to repeat steps 4 and 5 998 more times. 10. (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 (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. 11. There are 25! possible paths to be considered. That is approximately 1.5 x 1025 different paths. The computer can analyze 10,000,000, or 107, paths per second. The number of seconds required to check all possible paths is about 1.5 x 1025/107, or about 1.5 x 1018 seconds. That‘s roughly 1012 years: about a trillion years. This would not be a feasible algorithm. 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


Name:

Class:

Date:

Chapter 02: Algorithm Discovery and Design 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. 13. 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 >. 14. This is an essay question. Students may find excellent resources on the Internet. 15. If this problem is assigned, be sure to coordinate with your computing staff ahead of time for students to get the required information. 16. This is an essay question. Because this is a ―hot‖ topic, a great deal of hype and hyperbole is available, as well as useful information. It might be a good opportunity to teach students about finding reliable sources on the Internet, and evaluating online and print source materials. 17. Like question 16 this is an essay question. Students may be familiar with Apple iCloud services for iPhone and iPad devices, so it might be a good opportunity to relate their answers to the services provided by Apple. 18. About 130 feet ((((700,000,000 chars/5 chars per word)/300 words per page)/300 pages per inch)/12 inch per foot) Discussion of Challenge Work 1. 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 (ai) is smaller than the lower, we automatically borrow from the next column. We can do this by subtracting one from the ai+1 value of the column to the left. If the ai+1 value were already zero, then it would become -1. This automatically causes a borrow to occur on the next step. Here is the algorithm: Step 1: Set the value of i equal to the value of 0 Step 2: Repeat steps 3 to 6 until the value of i is m


Name:

Class:

Date:

Chapter 02: Algorithm Discovery and Design Step 3: Step 4:

If bi < ai then Set ci equal to ai - bi Otherwise (bi > ai)

Step 5:

Set ci equal to (ai + 10) – bi and replace ai+1 with ai+1 – 1 (This amounts to a borrow of 1 from ai+1 which adds 10 to ai)

Step 6:

Add 1 to i (moving us one column to the left)

Step 7: Print out the final answer cm-1cm-2 . . . c0 Step 8: Stop. 2. Students may need assistance finding or understanding other definitions from other sources.


Name:

Class:

Chapter 02: Algorithm Discovery and Design

Chapter 2: Algorithm Discovery and Design 1. (a) Set the value of area to ½(b h) (b) Set the value of interest to I B Set the value of FinalBalance to (1 + I) B (c) Set the value of FlyingTime to M/AvgSpeed 2. Algorithm: Step 1: Get values for B, I, and S Step 2: Set the value of FinalBalance to (1 + I/12)12B Step 3: Set the value of Interest to FinalBalance – B Step 4: Set the value of FinalBalance to FinalBalance – S Step 5: Print the message 'Interest Earned: ' Step 6: Print the value of Interest Step 7: Print the message 'Final Balance: ' Step 8: Print the value of FinalBalance 3. Algorithm: Step 1: Get values for E1, E2, E3 and F Step 2: Set the value of Ave to (E1 + E2 + E3 + 2F)/5 Step 3: Print the value of Ave 4. Algorithm: Step 1: Get values for P and Q Step 2: Set the value of Subtotal to P Q Step 3: Set the value of TotalCost to (1.06) Subtotal Step 4: Print the value of TotalCost

Date:


Name:

Class:

Chapter 02: Algorithm Discovery and Design 5. (a) If y 0 then Print the value of (x/y) Else Print the message 'Unable to perform the division.' (b) If r > 1.0, then Set the value of Area to r2 Set the value of Circum to 2 r Else Set the value of Circum to 2 r 6. Algorithm: Step 1: Get a value for B, I, and S Step 2: Set the value of FinalBalance to (1 + I/12)12B Step 3: Set the value of Interest to FinalBalance – B Step 4: If B < 1000 then Set the value of FinalBalance to FinalBalance – S Step 5: Print the message 'Interest Earned: ' Step 6: Print the value of Interest Step 7: Print the message 'Final Balance: ' Step 8: Print the value of FinalBalance 7. Algorithm: Step 1: Set the value of i to 1 Step 2: Set the values of Won, Lost, and Tied all to 0 Step 3: While i < 10 do Step 4:

Get the value of CSUi and OPPi

Step 5:

If CSUi > OPPi then

Step 6: Step 7: Step 8:

Set the value of Won to Won + 1 Else if CSUi < OPPi then Set the value of Lost to Lost + 1

Date:


Name:

Class:

Date:

Chapter 02: Algorithm Discovery and Design Step 9:

Else

Step 10:

Set the value of Tied to Tied + 1

Step 11:

Set the value of i to i + 1 End of the While loop

Step 12: Print the values of Won, Lost, and Tied Step 13: If Won = 10, then Step 14:

Print the message, 'Congratulations on your undefeated season.'

8. Algorithm: Step 1: Set the value of i to 1 Step 2: Set the value of Total to 0 Step 3: While i < 14 do Step 4:

Get the value of Ei

Step 5:

Set the value of Total to Total + Ei

Step 6:

Set the value of i to i + 1 End of While loop

Step 7: Get the value of F Step 8: Set the value of Total to Total + 2F Step 9: Set the value of Ave to Total / 16 Step 10: Print the value of Ave 9. Algorithm: Step 1: Set the value of TotalCost to 0 Step 2: Do Step 3:

Get values for P and Q

Step 4:

Set the value of Subtotal to P Q

Step 5:

Set the value of TotalCost to TotalCost + (1.06) Subtotal


Name:

Class:

Date:

Chapter 02: Algorithm Discovery and Design While (TotalCost < 1000) Step 6: Print the value of TotalCost 10. The tricky part is in steps 6 through 9. If you use no more than 1000 kilowatt hours in the month then you get charged $.06 for each. If you use more than 1000, then you get charged $.06 for the first 1000 hours and $.08 for each of the remaining hours. There are Mi – 1000 remaining hours, since Mi is the number of hours in the ith month. Also, remember that KWBegini and KWEndi are meter readings, so we can determine the total kilowatt-hours used for the whole year by subtracting the first meter reading (KWBegin1) from the last (KWEnd12). Step 1: Set the value of i to 1 Step 2: Set the value of AnnualCharge to 0 Step 3: While i < 12 do Step 4:

Get the value of KWBegini and KWEndi

Step 5:

Set the value of Mi to KWEndi – KWBegini

Step 6:

If Mi < 1000 then

Step 7:

Set AnnualCharge to AnnualCharge + (.06Mi)

Step 8:

Else

Step 9:

Set AnnualCharge to AnnualCharge + (.06)1000 + (.08)(Mi – 1000)

Step 10:

Set the value of i to i + 1 End of While loop

Step 11: Print the value of AnnualCharge Step 12: If (KWEnd12 – KWBegin1) < 500, then Step 13:

Print the message 'Thank you for conserving electricity.'

11. Algorithm: Step 1: Do Step 2:

Get the values of HoursWorked and PayRate

Step 3:

If HoursWorked > 54 then


Name:

Class:

Date:

Chapter 02: Algorithm Discovery and Design Step 4:

DT = HoursWorked – 54

Step 5:

TH = 14

Step 6:

Reg = 40

Step 7:

Else if HoursWorked > 40 then

Step 8:

DT = 0

Step 9:

TH = HoursWorked – 40

Step 10:

Reg = 40

Step 11:

Else (HoursWorked < 40)

Step 12:

DT = 0

Step 13:

TH = 0

Step 14:

Reg = HoursWorked

Step 15:

GrossPay = PayRate Reg + 1.5 PayRate TH + 2 PayRate DT

Step 16:

Print the value of GrossPay

Step 17:

Print the message 'Do you wish to do another computation?'

Step 18:

Get the value of Again While (Again = yes)

12. Steps 1, 2, 5, 6, 7, and 9 are sequential operations and steps 4 and 8 are conditional operations. After their completion, the algorithm moves on to the step below it, so none of these could cause an infinite loop. Step 3, however, is a while loop, and it could possibly cause an infinite loop. The true/false looping condition is ―Found = NO and i 10,000.‖ If NUMBER is ever found in the loop then Found gets set to YES, the loop stops, and the algorithm ends after executing steps 8 and 9. If NUMBER is never found, then 1 is added to i at each iteration of the loop. Since step 2 initializes i to 1, i will become 10,001 after the 10,000th iteration of the loop. At this point the loop will halt, steps 8 and 9 will be executed, and the algorithm will end. 13. Algorithm: Step 1: Get values for NUMBER, T1, . . . . T10000, and N1, . . . ., N10000 Step 2: Set the value of i to 1 and set the value of NumberFound to 0


Name:

Class:

Date:

Chapter 02: Algorithm Discovery and Design Step 3: While (i 10,000) do steps 4 through 7 Step 4:

If NUMBER equals Ti then

Step 5:

Print the name of the corresponding person, Ni

Step 6:

Set the value of NumberFound to NumberFound + 1

Step 7:

Add 1 to the value of i

Step 8: Print the message NUMBER ' was found ' NumberFound 'times' Step 9: Stop 14. Let‘s assume that FindLargest is now a primitive to us, and use it to repeatedly remove the largest element from the list until we reach the median. Step 1: Get the values L1, L2, . . ., LN of the numbers in the list Step 2: If N is even then Let M = N / 2 Else Let M = (N + 1) / 2 Step 3: While (N M) do steps 4 through 9 Step 4:

Use FindLargest to find the location of the largest number in the list L1, L2, . . ., LN

Step 5:

Exchange Llocation and LN as follows

Step 6:

Temp = LN

Step 7:

LN = Llocation

Step 8:

Llocation = Temp

Step 9:

Set N to N – 1 and effectively shorten the list

Step 10: Print the message 'The median is: ' Step 11: Print the value of LM Step 12: Stop


Name:

Class:

Date:

Chapter 02: Algorithm Discovery and Design 15. This algorithm will find the first occurrence of the largest element in the collection. This element 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 none are greater than it. 16. (a) If n < 2, then the test would be true, so the loop would be executed. In fact, the test would never become false. Thus the algorithm would either loop forever, or generate an error when referring to an invalid Ai value. If n > 2, then the test would be false the first time through, so the loop would be skipped and A1 would be reported as the largest value. (b) The algorithm would find the largest of the first n – 1 elements and would not look at the last element, as the loop would exit when i = n. (c) For n = 2 the loop would execute once, comparing the A1 and A2 values. Then the loop would quit on the next pass, returning the larger of the first two values. For any other value of n, the loop would be skipped, reporting A1 as the largest value. 17. (a) The algorithm would still find the largest element in the list, but if the largest were not unique then the algorithm would find the last occurrence of the largest element in the list. (b) The algorithm would find the smallest element in the list. The relational operations are very important, and care must be taken to choose the correct one, for mixing them up can drastically change the output of the algorithm. 18. (a) The algorithm will find the three occurrences of "and". First in the word band, second in the word and, and third in the word handle. (b) We could search for ― and ‖. That is, the word itself surrounded by spaces. Note that the word "and" is special in that it is almost always surrounded by spaces in a sentence. Other words may start or end sentences and be followed by punctuation. 19. It would go into an infinite loop, because k will stay at 1, and we will never leave the outside while loop. We will keep checking the 1 position over and over again. 20.

Step 1: Get the value for N Step 2: Set the value of i to 2 Step 3: Set the value of R to 1; Step 4: While (i < N and R 0) do Steps 5-6 Step 5:

Set R to the remainder upon computing N/i

Step 6:

Set the value of i to i + 1

Step 7: If R = 0 then


Name:

Class:

Date:

Chapter 02: Algorithm Discovery and Design Print the message 'not prime' Else Print the message 'prime' (This algorithm could be improved upon because it is enough to look for divisors of N less than or equal to N .) 21. Here we assume that we can perform "arithmetic" on characters, so that m + 3 = p, for example. Step 4 is the difficult part that must handle the "wraparound" from the end of the alphabet back to the beginning. Step 1: Get the values for nextChar and k Step 2: While (nextChar $) do steps 3 through 5 Step 3:

Set the value of outChar to nextChar + k

Step 4:

If outChar > z then Set the value of outChar to (outChar -26)

Step 5: 22.

Print outChar

Step 1: Get the values for k and N1, N2, …, Nk Step 2: Set the value of front to 1 Step 3: Set the value of back to k Step 4: While (front back) do steps 5 through 9

23.

Step 5:

Set the value of Temp to Nback

Step 6:

Set the value of Nback to Nfront

Step 7:

Set the value of Nfront to Temp

Step 8:

Set front = front + 1

Step 9

Set back = back – 1

Step 1: Get the values for N1, N2, …, Nk, and SUM Step 2: Set the value of i to 1 Step 3: Set the value of j to 2


Name:

Class:

Chapter 02: Algorithm Discovery and Design Step 4: While (i < k) do steps 5 through 11 Step 5:

While (j k) do steps 6 through 9

Step 6:

If Ni + Nj = SUM then

Step 7:

Print (Ni, Nj)

Step 8:

Stop Else

Step 9:

Set the value of j to j + 1

Step 10:

Set the value of i to i + 1

Step 11:

Set the value of j to i + 1

Step 12: Print the message 'Sorry, there is no such pair of values.' 24.

Set count to 0 Set sum to 0 Get a value for V While V ≠ -1 Set sum to sum + V Set count to count + 1 Get the next value for V End of the loop Let’s make sure that we had at least one value so we don’t divide by 0 If (count > 0) Set average to sum / count Print the value of average Else Print the message ‗I was given no input data‘ Stop

Date:


Name:

Class:

Date:

Chapter 02: Algorithm Discovery and Design 25.

Set adjacent to NO Get values for V1 and V2

We can do this since we know there are at least 2 values

While (V2 ≠-1) AND (adjacent = NO) If V1 = V2 Set adjacent to YES Else Set V1 = V2 Get a new value for V2 End of loop Print the value of adjacent Stop 26. We only need to make one simple change. Instead of writing Print the value of adjacent we change that to read: If (adjacent = YES) Print the message 'Yes, the numbers ' V1 ' and ' V1 ' are adjacent.' Else Print just the value of adjacent

Discussion of Challenge Work 1. The general algorithm is fairly clear, in English, in the text. Step 1: Read values for start, step, and accuracy Step 2: While |step| > accuracy do steps 3 through 9 Step 3:

If f(start) > 0 then set FirstSign to +

Step 4:

Else set FirstSign to –


Name:

Class:

Date:

Chapter 02: Algorithm Discovery and Design Step 5:

Do steps 6 through 8

Step 6:

Set the value of start to start + step

Step 7:

If f(start) > 0 then set the value of Sign to +

Step 8:

Else set the value of Sign to – while (Sign = FirstSign)

Step 9:

If |step| > accuracy then set the value of step to (-0.1)step

Step 10: Set the value of root to start – step/2 Step 11: Print the value of root. 2. Many excellent simulations of sorting algorithms are available on the Web, suggest students examine them if they have questions about this algorithm. The Find Largest algorithm given in the book always searches the whole list. First, we should create a variation that takes, in addition to the list of values, two indices which bound the range of the list that should be searched. Also, it is easiest to return the location of the largest value, for use in the sort algorithm. Below is a sketch of how it should change: FindLargest(A, start, end) Step 1: Set the value of loc to start Step 2: Set the value of i to start + 1 Step 3: While (i < end) do Step 4: Step 5: Step 6:

If Ai > Aloc then Set loc to i Add 1 to the value of i

Step 7: End of the loop Step 8: Return the value loc The Selection Sort algorithm is quite simple, once we have a suitable form for the Find Largest portion of it. Step 1: SelectionSort(A, n) Step 2: Set lastpos to n


Name:

Class:

Date:

Chapter 02: Algorithm Discovery and Design Step 3: While (lastpos > 1) do Step 4:

Set biggestpos to FindLargest(A, 1, lastpos)

Step 5:

Swap Alastpos and Abiggestpos

Step 6:

Subtract 1 from lastpos

Step 7: End of loop 3. Students should be provided with concrete leads to reference materials about non-European mathematicians, including references to online resources.


Name:

Class:

Date:

Chapter 02: Algorithm Discovery and Design

Invitation to Computer Science 7th Edition Schneider Test Bank Full clear download (no formatting errors) at: http://testbanklive.com/download/invitation-to-computer-science-7th-edition-schneider-testbank/ Invitation to Computer Science 7th Edition Schneider Solutions Manual Full clear download (no formatting errors) at: http://testbanklive.com/download/invitation-to-computer-science-7th-edition-schneidersolutions-manual/ People also search: invitation to computer science 7th edition pdf free download invitation to computer science 7th edition ebook invitation to computer science pdf invitation to computer science 6th edition invitation to computer science 7th edition answers invitation to computer science schneider invitation to computer science 5th edition pdf introduction to computer science 7th edition


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.