Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)"— Presentation transcript:

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

2

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

4 Developing an Algorithm for Area 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.

5 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

6 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

7 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

8 Additional Primitive Needed for 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 }

9 Pseudocode with a Condition print “Enter the value for radius” get radius print “Type A for area or C for circumference.” get response if (response = A) { set area to (314 * radius * radius) print area } else { set circumference to (2 * 3.14 * radius) print circumference }

10 Practice with Conditional Algorithms In class: write algorithms in pseudocode. Get two numbers and print the larger one.

11 Conditional Algorithms with More Than Two Choices Nested If statement: if first condition //do first thing else if second condition //do second thing else //do something else end if

12 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

13 Additional Primitive Needed for an Iterative Operation while (some condition is true) { some action(s) }

14 Pseudocode with an Iteration Method 1: Ask if you should repeat set answer to C while (answer = C) {print “Enter a value for radius” get radius print “Type A for area or C for circumference.” get response if (response = A) // etc. – same code as before print “Type C to do another circle or Q to quit.” get answer }

15 Pseudocode with an Iteration Method 2: Ask how many times print “How many circles do you want? “ get number while (number > 0) {print “Enter a value for radius” get radius print “Type A for area or C for circumference.” get response if (response = A) // etc. – same code as before set number to (number – 1) }

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

17 Pseudocode with an Iteration Method 3: Check a value get x, y while (x > 0) { set x to (x – y) print x }

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

19 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,

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

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

22

23


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

Similar presentations


Ads by Google