algorithms iii n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Algorithms III PowerPoint Presentation
Download Presentation
Algorithms III

Loading in 2 Seconds...

play fullscreen
1 / 21

Algorithms III - PowerPoint PPT Presentation


  • 381 Views
  • Uploaded on

Algorithms III. Problem Solving and Pseudocode. Methods of Problem Solving. Decode this sentence: Pdeo eo pda yknnayp wjosan. Problem Solving. Now that we know what algorithms are, we are going to try some problem solving and write algorithms for the problems.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Algorithms III' - harlow


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
algorithms iii
Algorithms III

Problem Solving

and

Pseudocode

methods of problem solving
Methods of Problem Solving
  • Decode this sentence:

Pdeo eo pda yknnayp wjosan.

problem solving
Problem Solving
  • Now that we know what algorithms are, we are going to try some problem solving and write algorithms for the problems.
  • 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.
someone stole a cookie from the cookie jar
Someone stole a cookie from the cookie jar
  • 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?
solve the problem
Solve the Problem
  • 3 cookies left X 2 = 6 cookies left after 2nd child
  • 6 X 2 = 12 cookies left after 1st child
  • 12 X 2 = 24 = original number of cookies
a generic algorithm
A Generic Algorithm
  • 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

generic algorithm for cookie problem
Generic Algorithm for Cookie Problem
  • Get number of children as input from the user.
  • Get number of remaining cookies as input from the user.
  • 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.
  • Print the original number of cookies.
pseudocode
Pseudocode
  • When we broke down the previous problem into steps, we expressed each step as an English phrase.
  • We can think of this as writing pseudocode for the problem.
  • Typically, pseudocode is a combination of English phrases and formulas.
  • If we know the programming language that we’ll be using, it can also include code fragments.
brian s shopping trip
Brian’s Shopping Trip
  • 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?
brian s shopping trip1
Brian’s Shopping Trip
  • 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$

generic algorithm
Generic Algorithm
  • Now, we’ll make a generic algorithm to solve any problem of this type.
  • Instead of using actual amounts or a description of items, we’ll use variable names.
brian s clothing purchases
Brian’s Clothing Purchases
  • Brian’s belt cost $9. We’ll call this item1.
  • Brian’s shirt cost 4 times item1. So, we’ll call 4 the multiplier .
  • Brian’s shirt will be called item2. It can be calculated by item2 = multiplier X item1.
  • Since Brian had $10 left over, we’ll call that amountLeft.
brian s clothing purchases cont
Brian’s Clothing Purchases Cont.
  • Brian started with start dollars.
  • item1 + item2 = start - amountLeft
  • start = item1 + item 2 + amountLeft
pseudocode for brian s clothing problem algorithm
Pseudocode for Brian’s Clothing Problem Algorithm
  • Get price of item1 from user
  • Get multiplier from user
  • Get amountLeft from user
  • Calculate item2 (item2 = multiplier X item1)
  • Calculate start (start = item1 + item2 + amountLeft)
  • Print “The starting amount = ” start
uses of pseudocode
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.
uses of pseudocode cont
Uses of Pseudocode Cont.
  • Must have a limited vocabulary.
  • Must be easy to learn.
  • Must produce simple, English-like narrative notation.
control structures
Control Structures
  • Sequence
  • Selection
  • Repetition
sequence
Sequence
  • Series of steps or statements that are executed in the order they are written.
  • Example:Get num1 from user

Get num2 from user

sum = num1 + num2

Print “sum = “ sum

selection
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:if condition (is true) do this

else do that end_if

repetition
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

pseudocode for cookie problem
Pseudocode for Cookie Problem
  • Get number of children as input from the user, numChild.
  • Get number of remaining cookies as input from the user, cookiesLeft.
  • while (numChild > 0 )

cookiesLeft = cookiesLeft X 2

numChild = numChild - 1

  • Print “Original number of cookies =” cookiesLeft