Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 101 Overview of Algorithms. Example: Make Pancakes Prepare batter Beat 2 eggs Add 1 tablespoon of brown sugar Add 1 cup of milk Add 2.

Similar presentations


Presentation on theme: "Computer Science 101 Overview of Algorithms. Example: Make Pancakes Prepare batter Beat 2 eggs Add 1 tablespoon of brown sugar Add 1 cup of milk Add 2."— Presentation transcript:

1 Computer Science 101 Overview of Algorithms

2 Example: Make Pancakes Prepare batter Beat 2 eggs Add 1 tablespoon of brown sugar Add 1 cup of milk Add 2 tablespoons of melted butter Add 2 cups of flour and 1 tablespoon of baking powder Mix well Cook batter Pour three spoons full of batter into greased pan Heat at medium until bubbles appear Turn pancakes over Repeat last 3 steps until batter is gone

3 Programming a VCR Step 1. If the clock and calendar are not correctly set, then go to page 9 of the owner's manual and follow instructions there. Step 2. Place a blank tape in the VCR tape slot. Step 3. Repeat steps 4-7 for each program, up to 10 times Step 4. Enter the channel number and press CHAN Step 5. Enter time to start and press TIME-START Step 6. Enter time to stop and press TIME-FINISH Step 7. If no more programs, press END-PROG Step 8. Press TIMER

4 Properties of an Algorithm A finite sequence of steps Describes a process that halts with a solution to a problem Each step must be effectively computable

5 Pseudocode Informal language used to present algorithms Pretty close to English but precise enough for a computing agent to carry out

6 Pseudocode Algorithms Algorithms consist of a set of steps Steps are also called instructions, statements, or actions

7 Instructions and Data Instructions describe processes Data are things or entities that can be processed Example: (the + names the addition instruction and the 2 is an integer datum)

8 Variables and Constants Variables are names for data values The value of a variable can change during the course of a process Constants also are data values but cannot change

9 Example: Compute the Area of a Circle input radius set area to 3.14 * radius * radius output area Uses mathematical knowledge, that is area =  r 2 Pseudocode algorithm:

10 Arithmetic Expressions input radius set area to 3.14 * radius * radius output area Use the standard operators for addition and multiplication Use variables and constants for operands

11 Variable Assignment input radius set area to 3.14 * radius * radius output area Make a variable take on a new value set to

12 Input input radius set area to 3.14 * radius * radius output area Send data from the external world to the computing agent input get or

13 Output input radius set area to 3.14 * radius * radius output area Send data from the computing agent to the external world output print or

14 Flow of Control: Sequencing input radius set area to 3.14 * radius * radius output area Run a sequence of statements one after the other … A sequence

15 Flow Diagram for Sequencing statement Sequencing statement

16 Compute the Radius Uses input area set radius to square root of (area / 3.14) output radius  /arearadius 

17 Functions Uses input area set radius to sqrt(area / 3.14) output radius  /arearadius  A function names an action that transforms one or more data values (its arguments) into another data value sqrt(4) is 2, sqrt(25) is 5, etc.

18 Restricting the Inputs Well-defined for input values that are greater than 0 We should output an error message if the input is less than or equal to 0 input area set radius to sqrt(area / 3.14) output radius

19 Check the Input First input area if area <= 0 output "Error: area must be positive" else set radius to sqrt(area / 3.14) output radius Well-defined for input values that are greater than 0 We should output an error message if the input is less than or equal to 0

20 Flow of Control: Conditional input area if area <= 0 output "Error: area must be positive" else set radius to sqrt(area / 3.14) output radius Take one action if a true/false test is true Take an alternative action if that test is false

21 Flow of Control: Conditional if else Take one action if a true/false test is true Take an alternative action if that test is false Each statement can be a sequence of statements

22 Boolean Expressions if else A true/false test is also called a Boolean expression Most Boolean expressions compare two values using standard operators ( =,, etc)

23 Compute the Absolute Value input number if number < 0 set number to -number output number Uses arithmetic negation (-)

24 One-Way Conditional input number if number < 0 set number to -number output number There is one course of action if the test is true but no alternative course of action if the test is false

25 Flow Diagrams for Conditionals ? statement ? false statement One-way conditional Two-way conditional

26 Syntax and Semantics Syntax: the form of a statement Semantics: the meaning of a statement or what the statement does

27 One-Way Conditional ? statement false Semantics Syntax if Example if number < 0 set number to -number

28 Example: Summation Write an algorithm that computes the sum of the numbers between 1 and 10. set counter to 1 set sum to 0 while counter <= 10 set sum to sum + counter increment counter output sum

29 Flow of Control: Iteration Iteration allows a sequence of statements to be repeated zero or more times A loop keeps going while a continuation condition is true set counter to 1 set sum to 0 while counter <= 10 set sum to sum + counter increment counter output sum

30 Syntax and Semantics of while Statements while while. ? statement true false

31 Count-controlled Loops // General form while

32 Today Print Homework Exercise 1 and get started


Download ppt "Computer Science 101 Overview of Algorithms. Example: Make Pancakes Prepare batter Beat 2 eggs Add 1 tablespoon of brown sugar Add 1 cup of milk Add 2."

Similar presentations


Ads by Google