Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three...
-
Upload
victoria-griffin -
Category
Documents
-
view
240 -
download
0
description
Transcript of Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three...
Algorithm Discovery and DesignObjectives:• Interpret pseudocode• Write pseudocode, using the three types of
operations:* sequential (steps in order written)* conditional (if)* iterative (while)
How to Represent an Algorithm
An algorithm could be represented with• natural language• formal programming language• pseudocode
Developing an Algorithm forArea and Circumference
We need to:• get the radius from the user• compute and print area• compute and print circumference
A sequential algorithm will do the job.
How to Write the Algorithm in Pseudocode
• Decide on names for the items in the problem and use them consistently. e.g.:– area– circumference
• Use the following primitive operations:– get a value (e.g. get radius)– print a value or message (e.g. print area)– set the value of an item (e.g. set radius to the
value input)– arithmetic operations (+ - / *), square root
Pseudocode for the Area and Circumference Algorithm
• print “Enter the value for radius”• get radius // get the radius from the user
• set area to (3.14 * radius * radius)• print area• set circumference to (2 * 3.14 * radius)• print circumference
@ Note: You also need to prompt the user to type in the value for the radius
An Algorithm with a Conditional Operation
Give the user a choice of seeing the area or the circumference.
• Get the radius• Ask if the user wants area or circumference• Compute and print the requested measure
Additional Primitive Neededfor a Conditional Operation
Use the same primitives as before plus the following:if (some condition is true){
some action(s) should be done}else{ some other action(s) should be done}
Pseudocode with a Conditionprint “Enter the value for radius”get radiusprint “Type A for area or C for circumference.”get responseif (response = A){
set area to (314 * radius * radius)print area
}else{
set circumference to (2 * 3.14 * radius)print circumference
}
Practice with Conditional Algorithms
In class: write algorithms in pseudocode.• Get two numbers and print the larger one.
Conditional Algorithms with More Than Two Choices
Nested If statement:if first condition
//do first thingelse if second condition
//do second thingelse
//do something elseend if
An Algorithm with an Iterative Operation
Compute area or circumference for as many circles as the user wants
This can be done two ways:1. Ask the user each time whether or not to
repeat the computation for another circle.2. Ask the user how many times to do it, then
repeat the computation that many times
Additional Primitive Neededfor an Iterative Operation
while (some condition is true){
some action(s)}
Pseudocode with an IterationMethod 1: Ask if you should repeat
set answer to Cwhile (answer = C){ print “Enter a value for radius”
get radiusprint “Type A for area or C for circumference.”get responseif (response = A) // etc. – same code as beforeprint “Type C to do another circle or Q to quit.”get answer
}
Pseudocode with an IterationMethod 2: Ask how many times
print “How many circles do you want? “get numberwhile (number > 0){ print “Enter a value for radius”
get radiusprint “Type A for area or C for circumference.”get responseif (response = A) // etc. – same code as beforeset number to (number – 1)
}
Iteration Method 3: Check a value
Check some changing value to decide whether or not to repeat
Example: Read two numbers x and y. Repeatedly subtract y from x and print x as long as x is positive.
Pseudocode with an IterationMethod 3: Check a value
get x, ywhile (x > 0){
set x to (x – y)print x
}
Things to Note - Summary
• The steps in the algorithms are normally carried out one after the other in the given sequence. However, the conditional and Iterative operations (primitives) alter this normal flow.
Formulating algorithms• Look at the givens (the input information – about
the objects and their properties; give suitable names to them; Circle – object; radius - property)
• Examine what is/are to be produced (the output information – properties of objects; give suitable names for them as well)
• Remember the available primitives (actions) are:– accepting an input, – assigning the input value to something,
Formulating algorithms• Remember the available primitives
(continued): – Carrying out arithmetic operations (+ - * /; some
functions like square-root etc.),– Checking a ‘condition’ and doing something if
it is true and something else if it is not true, – Repeat a set of instructions as specified,
(iterative)– Delivering the result (one or more output
information - properties of items; give suitable names to them).
Formulating algorithms• Produce (express) each output in terms of the
givens (inputs) using a solution procedure you came up with based on your knowledge of how the output is related to the input. (Get to the unknowns using the givens.)
• Use the three constructs in the above process for producing output info in terms of input info.
• In addition to the input and output, use intermediary variables, where necessary, to help implement the solution procedure.