Algorithms III Problem Solving and Pseudocode CMSC 104
Algorithms III Problem Solving and Pseudocode CMSC 104 1
Methods of Problem Solving l Decode this sentence: Pdeo eo pda yknnayp wjosan. CMSC 104 2
Problem Solving Now that we know what algorithms are, we are going to try some problem solving and write algorithms for the problems. l We’ll start with step-by-step instructions that solve a particular problem and then write a generic algorithm that will solve any problem of that type. l CMSC 104 3
Someone stole a cookie from the cookie jar l CMSC 104 Momma had just filled the cookie jar when the three children went to bed. That night one child woke up, ate half the cookies and went back to bed. Later the second child woke up, ate half the remaining cookies, and went back to bed. Still later the third child woke up, ate half the remaining cookies, leaving 3 cookies in the jar. How many cookies were in the jar to begin with? 4
Solve the Problem 3 cookies left X 2 = 6 cookies left after 2 nd child l 6 X 2 = 12 cookies left after 1 st child l 12 X 2 = 24 = original number of cookies l CMSC 104 5
A Generic Algorithm l What’s a generic algorithm for this problem? An algorithm that will work with any number of remaining cookies AND that will work with any number of children CMSC 104 6
Generic Algorithm for Cookie Problem Get number of children as input from the user. l Get number of remaining cookies as input from the user. l While there are still children that have not raided the cookie jar, multiply the number of cookies by 2 and reduce the number of children by 1. l Print the original number of cookies. l CMSC 104 7
Pseudocode When we broke down the previous problem into steps, we expressed each step as an English phrase. l We can think of this as writing pseudocode for the problem. l Typically, pseudocode is a combination of English phrases and formulas. l If we know the programming language that we’ll be using, it can also include CMSC 104 code fragments. l 8
Brian’s Shopping Trip l CMSC 104 Brian bought a belt for $9 and a shirt that cost 4 times as much as the belt. He then had $10. How much money did Brian have before he bought the belt and shirt? 9
Brian’s Shopping Trip l First we solve the problem to help us identify the steps. 9 + 4 X 9 = START$ -10 9 + 36 = START$ - 10 45 = START$ -10 55 = START$ CMSC 104 10
Generic Algorithm Now, we’ll make a generic algorithm to solve any problem of this type. l Instead of using actual amounts or a description of items, we’ll use variable names. l CMSC 104 11
Brian’s Clothing Purchases Brian’s belt cost $9. We’ll call this item 1. l Brian’s shirt cost 4 times item 1. So, we’ll call 4 the multiplier. l Brian’s shirt will be called item 2. It can be calculated by item 2 = multiplier X item 1. l Since Brian had $10 left over, we’ll call that amount. Left. l CMSC 104 12
Brian’s Clothing Purchases Cont. Brian started with start dollars. l item 1 + item 2 = start - amount. Left l start = item 1 + item 2 + amount. Left l CMSC 104 13
Pseudocode for Brian’s Clothing Problem Algorithm Get price of item 1 from user l Get multiplier from user l Get amount. Left from user l Calculate item 2 (item 2 = multiplier X item 1) l Calculate start (start = item 1 + item 2 + amount. Left) l Print “The amount remaining =” amount. Left l CMSC 104 14
Uses of Pseudocode • Used in designing algorithms. • Used in communicating to users. • Used in implementing algorithms as programs. • Used in debugging logic errors in programs. CMSC 104 15
Uses of Pseudocode Cont. • Must have a limited vocabulary. • Must be easy to learn. • Must produce simple, English-like narrative notation. CMSC 104 16
Control Structures • Sequence • Selection • Repetition CMSC 104 17
Sequence • Series of steps or statements that are executed in the order they are written. • Example: Get num 1 from user Get num 2 from user sum = num 1 + num 2 Print “sum = “ sum CMSC 104 18
Selection • Defines one or two courses of action depending on the evaluation of a condition. • A condition is an expression that is either true or false. • Example: CMSC 104 if condition (is true) do this else do that end_if 19
Repetition • Many times there will be a group of statements that should be repeated. • These statements will make up what is known as the body of a loop. • Example: while condition (is true) loop-body end_while CMSC 104 20
Pseudocode for Cookie Problem Get number of children as input from the user, num. Child. l Get number of remaining cookies as input from the user, cookies. Left. l while (num. Child > 0 ) cookies. Left = cookies. Left X 2 num. Child = num. Child - 1 l Print “Original number of cookies =” cookies. Left l CMSC 104 21
- Slides: 21