CMSC 100 Algorithm Design and Pseudocode Professor Marie desJardins Thursday, September 20, 2012

advertisement
CMSC 100
Algorithm Design and Pseudocode
Professor Marie desJardins
Thursday, September 20, 2012
Some material borrowed from instructor slides for Schneider & Gerstung
CMSC 100 -- Algorithms and Pseudocode
1
Thu 9/20/12
Main Ideas



2
Languages for representing algorithms:

Natural language (e.g., English)

Pseudocode

Programming language (Scratch, C, Java, assembly...)
Three main types of algorithm statements:

Sequential

Conditional

Iterative
Algorithm design strategies:

Abstraction

Top-down design
CMSC 100 -- Algorithms and Pseudocode
Thu 9/20/12
Representing Algorithms

Pseudocode is used to design algorithms

Natural language is:



expressive, easy to use

verbose, unstructured, and ambiguous
Programming languages are:

structured, designed for computers

grammatically fussy, cryptic to beginners
Pseudocode lies somewhere between these two
3
CMSC 100 -- Algorithms and Pseudocode
Thu 9/20/12
Sequential Algorithms

It is absolutely essential to understand that every algorithm proceeds
sequentially



Each step is fully completed before the processor considers the next step
Sequential operations perform a single task. Typical tasks include:

Computation: a single numeric calculation

Input: gets data values from outside the algorithm

Output: sends data values to the outside world
A variable is a named location to hold a value
4
CMSC 100 -- Algorithms and Pseudocode
Thu 9/20/12
5
CMSC 100 -- Algorithms and Pseudocode
Thu 9/20/12
Control Statements

Control operation: changes the normal flow of control

Conditional statement (if... then... else...): asks a question and
selects among alternative options

1.
Evaluate the true/false condition
2.
If the condition is true, then do the first set of operations and skip
the second set
3.
If the condition is false, skip the first set of operations and do the
second set
Example: check for good or bad gas mileage
6
CMSC 100 -- Algorithms and Pseudocode
Thu 9/20/12
7
CMSC 100 -- Algorithms and Pseudocode
Thu 9/20/12
Indentations
show which
parts of the
statement are
inside the “if”
and “else”
8
CMSC 100 -- Algorithms and Pseudocode
Thu 9/20/12
Iteration/Repetition

Iteration: an operation that causes looping, repeating a block of
instructions (“body”)

While statement repeats the loop body as long a condition remains
true


continuation condition: a test to see if while loop should continue

loop body: instructions to perform repeatedly
Example: repeated mileage calculations
9
CMSC 100 -- Algorithms and Pseudocode
Thu 9/20/12
10
CMSC 100 -- Algorithms and Pseudocode
Thu 9/20/12
12
13
End while
Stop
Explicit “End while” shows where loop body
ends, eliminates need to specify steps at the
top, and improves readability
11
CMSC 100 -- Algorithms and Pseudocode
Thu 9/20/12
Another Pseudocode
Variation

C/Java-like syntax:
if (condition) {
if-statements
} else {
then-statements
}
12
CMSC 100 -- Algorithms and Pseudocode
Thu 9/20/12
Do... while loop: loop
variant that places the
continuation condition
at the end (continues if
true; loop always
executes at least once)
13
CMSC 100 -- Algorithms and Pseudocode
Thu 9/20/12
14
CMSC 100 -- Algorithms and Pseudocode
Thu 9/20/12
Examples of Algorithmic Problem Solving
Example 1: Go Forth and Multiply
“Given two nonnegative integer values, a ≥ 0, b ≥ 0, compute and
output the product (a × b) using the technique of repeated addition.
That is, determine the value of the sum a + a + a + . . . + a (b times).”
15
CMSC 100 -- Algorithms and Pseudocode
Thu 9/20/12
Examples of Algorithmic Problem Solving
Example 1: Go Forth and Multiply (continued)

Get input values


Compute the answer


Get values for a and b
Loop b times, adding each time*
Output the result

Print the final value*
* steps need elaboration
16
CMSC 100 -- Algorithms and Pseudocode
Thu 9/20/12
Examples of Algorithmic Problem Solving
Example 1: Go Forth and Multiply (continued)

Loop b times, adding each time

Set the value of count to 0

While (count < b) do


… the rest of the loop*

Set the value of count to count + 1
End of loop
* steps need elaboration
17
CMSC 100 -- Algorithms and Pseudocode
Thu 9/20/12
Examples

Print the numbers from 1 to n

Print the even numbers from 2 to n

You try it: Print the first n multiples of 5
18
CMSC 100 -- Algorithms and Pseudocode
Thu 9/20/12
Data Types

Basic data types (information that can be stored in a variable):





Integers
Real numbers
Characters (single letters or numbers)
Strings (sequence of letters or numbers)
The simplest abstract data type is a list (also sometimes called an array):




A list L can hold zero or more objects of any of the basic data types
We will write a list using braces {}:
 Set Days to {“Sunday”, “Monday”, ..., “Friday”, “Saturday”}
We will denote the ith object in a list L by L[i]
 Print Days[2] would print out Monday
We will denote the length of list L by Length(L) or length of L
19
CMSC 100 -- Algorithms and Pseudocode
Thu 9/20/12
More Examples

Print the sum of a list of numbers

You try it: Print a list in reverse

You try it: Given a day of the week, print the next day

Tricky bit: Handling Saturday!

Error checking: What if the day of the week doesn’t exist? (Happyday)
20
CMSC 100 -- Algorithms and Pseudocode
Thu 9/20/12
Summary

Pseudocode is used for algorithm design: structured like
code, but allows English and math phrasing and notation

Pseudocode is made up of: sequential, conditional, and
iterative operations

Algorithmic problem solving involves:

Step-by-step development of algorithm pieces

Use of abstraction and top-down design
21
CMSC 100 -- Algorithms and Pseudocode
Thu 9/20/12
Download