# 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 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:

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 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

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

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

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

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

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

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

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:

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

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

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

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

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

Flow Diagram for Sequencing statement Sequencing statement

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.

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

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

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

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

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

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

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

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

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

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

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

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

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

Count-controlled Loops // General form while

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