algorithm discovery and design objectives: interpret pseudocode write pseudocode, using the three...

23
Algorithm Discovery and Design Objectives: Interpret pseudocode • Write pseudocode, using the three types of operations: * sequential (steps in order written) * conditional (if) * iterative (while)

Upload: victoria-griffin

Post on 06-Jan-2018

241 views

Category:

Documents


0 download

DESCRIPTION

How to Represent an Algorithm An algorithm could be represented with natural language formal programming language pseudocode

TRANSCRIPT

Page 1: Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)

Algorithm Discovery and DesignObjectives:• Interpret pseudocode• Write pseudocode, using the three types of

operations:* sequential (steps in order written)* conditional (if)* iterative (while)

Page 2: Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)
Page 3: Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)

How to Represent an Algorithm

An algorithm could be represented with• natural language• formal programming language• pseudocode

Page 4: Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)

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.

Page 5: Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)

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

Page 6: Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)

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

Page 7: Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)

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

Page 8: Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)

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}

Page 9: Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)

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

}

Page 10: Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)

Practice with Conditional Algorithms

In class: write algorithms in pseudocode.• Get two numbers and print the larger one.

Page 11: Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)

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

Page 12: Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)

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

Page 13: Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)

Additional Primitive Neededfor an Iterative Operation

while (some condition is true){

some action(s)}

Page 14: Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)

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

}

Page 15: Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)

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)

}

Page 16: Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)

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.

Page 17: Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)

Pseudocode with an IterationMethod 3: Check a value

get x, ywhile (x > 0){

set x to (x – y)print x

}

Page 18: Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)

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.

Page 19: Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)

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,

Page 20: Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)

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).

Page 21: Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)

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.

Page 22: Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)
Page 23: Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)