270 likes | 620 Views
Algorithm: definition. An algorithm is an ordered set of unambiguous , executable steps that defines a terminating process. Algorithms: levels of abstraction. Problem = motivation for algorithm Algorithm = procedure to solve the problem Often one of many possibilities
E N D
Algorithm: definition • An algorithm is an ordered set of unambiguous, executable steps that defines a terminating process.
Algorithms: levels of abstraction • Problem = motivation for algorithm • Algorithm = procedure to solve the problem • Often one of many possibilities • Representation = description of algorithm sufficient to communicate it to the desired audience • Always one of many possibilities
Pseudocode primitives • Assignment name expression • Conditional selection ifconditionthenaction • Repeated execution whileconditiondoactivity repeat activity until condition • Procedure procedurename (generic names)
Iterative Structures Pretest loop: while (condition) do (loop body) Posttest loop: repeat (loop body) until(condition)
Problem solving steps • 1. Understand the problem. • 2. Get an idea how an algorithmic procedure might solve the problem. • 3. Formulate the algorithm and represent it as a program. • 4. Evaluate the program for accuracy and its potential as a tool for solving other problems.
Techniques for “getting a foot in the door” • Work the problem backwards • Solve an easier related problem • Relax some of the problem constraints • Solve pieces of the problem first = bottom up methodology • Stepwise refinement = top-down methodology • Popular technique because it produces modular programs
Sample problem • Person A is charged with the task of determining the ages of B’s three children. • B tells A that the product of the children’s ages is 36. • A replies that another clue is required. • B tells A the sum of the children’s ages. • A replies that another clue is needed. • B tells A that the oldest child plays the piano. • A tells B the ages of the three children. • How old are the three children?
Connect each of the nine dots using only four straight lines and without lifting your pen from the paper.
Arrange the numbers 1 through 9 on a tic tac toe board such that the numbers in each row, column, and diagonal add up to 15.
Mental Gymnastics What's the largest number of U.S. coins you can have without having even change for a dollar? A mountain goat attempts to scale a cliff sixty feet high. Every minute, the goat bounds upward three feet but slips back two. How long does it take for the goat to reach the top?
Mental Gymnastics Three switches can be turned on or off. One is the light switch for the overhead light in the next room, which is initially off, but you don't know which. The other two switches do nothing. From the room with the switches in it, you can't see whether the light in the next room is turned on or off. You may flip the switches as often and as many times as you like, but once you enter the next room to check on the light, you must be able to say which switch controls the light without flipping the switches any further. (And you can't open the door without entering, either!) How can you determine which switch controls the light?
Software verification • Proof of correctness • Assertions • Preconditions • Loop invariants • Testing
Example problem: Chain separating • A traveler has a gold chain of seven links. • He must stay at an isolated hotel for seven nights. • The rent each night consists of one link from the chain. • What is the fewest number of links that must be cut so that the traveler can pay the hotel one link of the chain each morning without paying for lodging in advance?
Is This Program Segment Correct? Precondition: X is a positive integer and Y is a positive even integer Z X/Y if (Remainder is positive) then (Print X is odd) else (Print X is even)