Lecture 3 Representation of Algorithms 1/21/04

advertisement
Lecture 3
1/21/04
Representation of Algorithms
• Natural languages (English, Spanish,
Chinese, …)
• Formal programming languages (Java,
Pascal, C++, …)
• Pseudocode
Lecture 3
Pseudocode
(S&G, §2.2)
1/21/04
– “a special set of English language constructs
modeled to look like the statements available in
most programming languages”
– compromise between natural & formal
languages
CS 100 - Lecture 3
1
1/21/04
input
interface
computation
2
Input Operation
Sequential Operations
computing
agent
CS 100 - Lecture 3
• Example:
Get a value for r, the radius of the circle
output
25↵
17
25
Get a value for r
r
• General form:
Get values for “variable”, “variable”, …
• “variable” means “any variable” (e.g., r)
external world
1/21/04
CS 100 - Lecture 3
3
1/21/04
Output Operation
Print the value of Area
• Example:
Print the message ‘Sorry, no answer’
26↵
Print the message
‘Sorry, no answer’
Area
• General form:
Print the values of “variable”, “variable”, …
1/21/04
CS 100
CS 100 - Lecture 3
4
Output Operation (2)
• Example:
Print the value of Area
26
CS 100 - Lecture 3
Sorry, no
answer↵
• General form:
Print the message ‘message’
5
1/21/04
CS 100 - Lecture 3
6
1
Lecture 3
1/21/04
Computation is
Physically Instantiated
Computational Operation
• Example:
Set the value of Area to π r2
2 cm
π = 3.14…
ππ ×× (2
(2 cm/2)
cm/2)22
25
= 1963
1963
177
Hg
Area
r
3 cm
compute
3.14… × (252)
2
×× 33 cm
cm
×× 13.55
13.55 g/cm
g/cm33
== 127.7
127.7 g
g
127.7 g
• General form:
Set the value of “variable” to “arithmetic expression”
1/21/04
CS 100 - Lecture 3
7
1/21/04
Computational Processes
in General
• An input process translates an external
physical situation into the computer’s
internal representational medium
• Example physical situations:
– in which patterns can be represented
– by means of which they can be manipulated
• Example computational media:
–
–
–
–
– electrical signals & charges
– light intensity, fluid pressure, molecular
structure, quantum state, gear position (old),
abacus beads, paper, blackboard, etc.
CS 100 - Lecture 3
9
CS 100
if true
characters on display screen or printer
graphics images
position of robot arm
etc.
CS 100 - Lecture 3
CS 100 - Lecture 3
10
Conditional Operation
• An output process translates the computer’s
internal medium of representation into an
external physical effect
• Example physical effects:
1/21/04
position of mouse or joystick
position of key (depressed or not)
signal from light sensor
etc.
1/21/04
Output Processes in General
–
–
–
–
8
Input Processes in General
• Computation takes place in a medium
1/21/04
CS 100 - Lecture 3
true/false
condition
do one set of
operations
if false
do another set
of operations
continue
11
1/21/04
CS 100 - Lecture 3
12
2
Lecture 3
1/21/04
Conditional Statement
General:
If “true/false condition”
is true then
One-Branch Conditional
Example:
If (ci ≥ 10) then
if true
Set the value of ci to (ci – 10)
Set the value of carry to 1
“first set of algorithmic
operations”
do one set of
operations
Else
Else (or Otherwise)
Set the value of carry to 0
“second set of
algorithmic
operations”
if false
If “true/false
condition” is true
then
“first set of
algorithmic
operations”
do nothing
(rest of algorithm)
continue
(rest of algorithm)
1/21/04
true/false
condition
CS 100 - Lecture 3
13
Computer-Inspired Conditionals
in Legal Drafting
1/21/04
CS 100 - Lecture 3
14
Basic Loop (Trailing Decision)
Subsection (a). IF AND ONLY IF
(1) (A) A person has threatened or attempted suicide or to inflict serious
bodily harm on himself, OR
(B) The person has threatened or attempted homicide or other violent
behavior, OR
(C) The person has placed others in reasonable fear of violent behavior and
serious physical harm to them, OR
(D) The person is unable to avoid severe impairment or injury from specific
risks, AND
do loop body
if not, repeat
(2) There is a substantial likelihood that such harm will occur,
THEN
(3) The person poses a “substantial likelihood of serious harm” for
purposes of subsection (b).
test if done
if done, continue
— Tenn. Code Ann. sect. 33-6-104(a) (1991)
1/21/04
CS 100 - Lecture 3
15
1/21/04
Iteration (Trailing Decision)
1. Set the value of count to 1
2. Repeat steps 3 to 5 until (count > 100)
3.
Set square to (count × count)
4.
Print the values of count and square
5.
Add 1 to count
termination
condition
loop
body
(rest of algorithm)
1/21/04
CS 100
CS 100 - Lecture 3
16
Example Loop
Repeat steps i to j until “a true/false
condition” becomes true
step i:
operation
step i + 1: operation
…
step j:
operation
CS 100 - Lecture 3
17
1/21/04
CS 100 - Lecture 3
18
3
Lecture 3
1/21/04
Leading Decision Loop
Alternate Notation
Repeat until “a true/false condition” becomes
true
“a set of algorithmic operations”
End of loop
if not
test if more
if more
and repeat
do loop body
While “a true/false
condition”
remains true do
“a set of
algorithmic
operations”
End of loop
continue with rest of algorithm
1/21/04
CS 100
CS 100 - Lecture 3
19
1/21/04
CS 100 - Lecture 3
20
4
Download