计算机科学导论Chap 05

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Posttest loop: repeat (loop body) until(condition)
Pseudo Code: indentation
if (not raining)
then (if (temperature = hot) then (go swimming) else (play golf) ) else (watch television)
22 November 2011 , U. London 7
Program 2 for XOR
Input: binary digits a,b Output: a XOR b 1. If a==b Return 0 2. Else Return 1 3. Halt
22 November 2011
, U. London
23
Getting a Foot in the Door
Try working the problem backwards Solve an easier related problem Relax some of the problem constraints Solve pieces of the problem first (bottom up methodology) Stepwise refinement: Divide the problem into smaller problems (top-down methodology)
22 November 2011 Brookshear Section 5.1 6
Program 1 for XOR
Input: binary digits a,b Output: a XOR b If a==0 and b==0 Return 0 If a==0 and b==1 Return 1 If a==1 and b==0 Return 1 If a==1 and b==1 Return 0 Halt
Very useful informal representation of algorithms. Preferable to using program code when designing an algorithm. Basic structures (assignment, loops, arrays …) are similar across many programming languages.
22 November 2011 , U. London 9
Figure 5.2 Folding a bird from a square piece of paper
10
Figure 5.3 Origami primitives
11
Pseudo Code for Algorithm Representation
if (not raining) then (if (temperature=hot) then (go
swimming) else (play golf)) else (watch television)
16 22 November 2011 16
Procedure
Definition: set of instructions which can be used as a single unit in different contexts. A procedure call contains the name of the procedure and any data supplied to the procedure A result calculated by the procedure can be obtained using a return statement.
22 November 2011 12
Pseudo Code: assignment
General form name <- expression Examples funds <- current + savings x <- 10
13 22 November 2011 13
Pseudo Code: conditional selection
halt.
3
Algorithm Representation
Requires well-defined primitives A collection of primitives constitutes a programming language.
4
Baidu Nhomakorabea
Terminology
Programming language: system for representing algorithms in a human readable form suitable for conversion to machine code. Program: representation of an algorithm in a programming language. Pseudo code: system for the informal representation of algorithms. Hardware: device to carry out the steps or instructions in a program.
Key or reserved words in bold
14 22 November 2011 14
Iterative Structures
Pretest loop: while (condition) do (loop body)
Example
while (tickets remain) do (sell a ticket)
22 November 2011 21
Solution: Exercise
procedure descending(A) j=0; flag=0; while flag==0 && j<Length[A]-1 do if A[j]<A[j+1] then flag=1; print(j); j=j+1; end while if flag==0 then print(-1); end procedure
An Introduction to Computer Science
计算机科学引论
XIANG, Hui 向辉 Shandong University
Chapter 5: Algorithms
5.1 The Concept of an Algorithm 5.2 Algorithm Representation 5.3 Algorithm Discovery 5.4 Iterative Structures 5.5 Recursive Structures 5.6 Efficiency and Correctness
2
Definition of Algorithm
An algorithm is an ordered set of unambiguous, executable steps that defines a terminating process.
The steps are often called instructions. Termination is by a special instruction:
General form if (condition) then (activity) else (activity) Example if (year is leap year) then d <- total divided by 366 else d <- total divided by 365
8
Program 3 for XOR
Input: binary digits a,b Output: a XOR b 1. Convert a to the integer ia 2. Convert b to the integer ib 3. ic=remainder on dividing ia+ib by 2 4. Return ic 5. Halt
18 22 November 2011 18
Pseudo Code: procedures
General type procedure name
Examples
procedure helloWorld //no parameters procedure sort (List) // parameter List
22
Polya’s Problem Solving Steps
1. Understand the problem. 2. Devise a plan for solving the problem. 3. Carry out the plan. 4. Evaluate the solution for accuracy and its potential as a tool for solving other problems.
22 November 2011 Brookshear sections 5.1 and 5.2 5
Representation of an Algorithm
The same algorithm can be represented in many different ways:
F = (9/5)C+32 F <- (9*C)/5+32 To obtain F multiply C by (9/5) and then add 32. Lookup table for Fahrenheit and Centigrade
17 22 November 2011 17
Example of a Procedure
Definition of a procedure procedure temp(c) f=(9/5)*c+32; return f; endProcedure
Procedure call
f1=temp(34)
25 Brookshear, Section 5.3 25
Example: Evaluation of a Boolean expression
Suppose that A, B, C are Boolean variables such that A=0, B=1, C=0. Evaluate the Boolean expression (A AND B AND C) OR (A AND NOT(B))
24
Bottom Up Strategy
Solve many small parts of the problem and gradually accumulate the information needed to solve the entire problem. Examples: evaluation of Boolean expressions, searching an unordered list.
19 22 November 2011 19
Figure 5.4 The procedure Greetings in pseudocode
20
Exercise
Write an algorithm in pseudo code to carry out the following task: input: a 1-dimensional array A of integers output: the integer -1 if A[i]≥ A[i+1] for 0≤i≤Length[A]-2, otherwise the least integer j such that A[j]<A[j+1].
相关文档
最新文档