Presentation is loading. Please wait.

Presentation is loading. Please wait.

计算机科学概述 Introduction to Computer Science 陆嘉恒 中国人民大学 信息学院 www.jiahenglu.net.

Similar presentations


Presentation on theme: "计算机科学概述 Introduction to Computer Science 陆嘉恒 中国人民大学 信息学院 www.jiahenglu.net."— Presentation transcript:

1 计算机科学概述 Introduction to Computer Science 陆嘉恒 中国人民大学 信息学院 www.jiahenglu.net

2 Computer Algorithm (计算机算法简介)

3 An Algorithm is A well-ordered collection of Unambiguous and Effectively computable operations that, when executed Produces a result and Halts in a finite amount of time Textbook definition

4 Representing Algorithms How do we represent algorithms? English Programming Language We will start with pseudocode and later will look at various programming languages.

5 Pseudocode Set of English Constructs designed to resemble statements in programming languages Steps presented in structured manner Simple, Highly readable

6 Constructs We Will Use Sequential operations to carry out –computation, –input, and –output. Conditional operations. Iterative operations.

7 Sequential Operations computation Set the value of X to 3. Assign X a value of A + B. X=2 - C. Set the value of Name to the first person's name. input Get a value for X, Y, and Z. Input values for A1, A2,..., Am. Read X, Y, and Carry. output Output the value of X. Print values for X, Y, and Carry. Print the message, "Error".

8 Example Algorithm (Sequential) Figure 2.3 Algorithm for Computing Average Miles per Gallon

9 Practice Write an algorithm in pseudocode to: Get the radius of a circle as input and output the circumference and area of the circle

10 Conditional Operation If “ a true/false condition ” is true then first set of algorithmic operations Else second set of algorithm operations Notice the indentation

11 Example Algorithm (conditionals) Figure 2.5 Second Version of the Average Miles per Gallon Algorithm

12 Practice Modify your algorithm to print “ large circle ” if the circumference is greater than 100 and “ small circle ” if it is equal to or less than 100.

13 Conditional Operation Picture true-false statement truefalse operation false branch true branch Note: either branch can be missing

14 true-false statement truefalse operation false branch true branch Note: either branch can be missing Conditional Operation Picture

15 true-false statement truefalse operation false branch true branch Note: either branch can be missing Conditional Operation Picture

16 Iterative Operations Set count to 5 While count < 4 Add 1 to count Print count stop Set count to 5 Do Add 1 to count Print count While count < 4 stop Notice when the continuation condition is checked Pretest loopPosttest loop

17 Infinite Loops Set count to 0 While count >= 0 Add 1 to count Print count stop

18 Iteration Operation Picture true-false statement true-false statement false true operation false operation while loop Do/while loop

19 Figure 2.7 Third Version of the Average Miles per Gallon Algorithm Example Algorithm (iteration)

20 Practice Write an algorithm that gets as input a single data value, x, and outputs the three values x 2, sin x, 1/x. This process should repeat until the input value for x is equal to 99.

21 Summary of Operation Types for Algorithms Sequential operations to carry out –computation, –input, and –output. Conditional operations. Iterative operations.

22 There is a theorem in theoretical computer science that proves that these operations are sufficient to represent ANY algorithm! Algorithms are used to do everything you see on a computer! Do word processing. Fly NASA probes to the planets. Run the international telephone switching system. Create CAT scan images. Process your pay checks. Run computer games. Etc. Powerful Algorithms!!

23 EXAMPLE OF AN ALGORITHM PROBLEM: Start with a collection of names N 1, N 2,..., N 10000, and corresponding telephone numbers T 1, T 2,..., T 10000. Given a name, Name, find and print the telephone number for that name if a match on an N i occurs; otherwise, print "Not Found". Given a problem, there are often many ways to provide an algorithm for solving the problem. Note: You must understand the methodology for solving the problem in order to write an algorithm for the solution!!!

24 First Try 1. Get values for N 1, N 2,..., N 10000, T 1, T 2,,,,, T 10000, and Name. 2. If Name is N 1, then print T 1 Stop 3. If Name is N 2, then print T 2. Stop. {a lot of tedious writing here that is being skipped} 10001. If Name is N 10000, then print T 10000. Stop. 10002. Print "Not found" 10003. Stop.

25 Second Try 1. Get values for N 1, N 2,..., N 10000, T 1, T 2,,,,, T 10000, and Name. 2. Set the value of i to 1 and the value of Found to NO. 3. While Found is NO 4.If Name is equal to N i, then 5.Print the telephone number T i 6. Set the value of Found to Yes Else 7.Add 1 to the value of i 8. Stop.

26 Third Try 1. Get values for N 1, N 2,..., N 10000, T 1, T 2,,,,, T 10000, and Name. 2. Set the value of i to 1 and the value of Found to NO. 3. While Found is NO and i <= 10000 4.If Name is equal to N i, then 5.Print the telephone number T i 6. Set the value of Found to Yes Else 7.Add 1 to the value of i 8. If (Found is No) then 9.Print "Not found" 10. Stop.

27 Game Time Who can beat me in the game 21? Objective: Get your opponent to say the number 21. Rules: One player goes first starting counting from 1. On each turn a player may say one or two consecutive numbers. You must begin where your opponent left off. The first person to say the number 21 loses. There is a way to always beat me!


Download ppt "计算机科学概述 Introduction to Computer Science 陆嘉恒 中国人民大学 信息学院 www.jiahenglu.net."

Similar presentations


Ads by Google