Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Algorithms

Similar presentations


Presentation on theme: "Introduction to Algorithms"— Presentation transcript:

1 Introduction to Algorithms
Yonglei Tao

2 What is an Algorithm? A step-by-step description about how to perform a task Unambiguous In a logical order Executable A program is the expression of an algorithm in a programming language

3 Examples of Algorithm Making a phone call Other examples
Pick up the receiver Hear the dial-tone Dial a phone number Wait for the reply Talk Replace the receiver Other examples Programming a VCR A recipe for a cake

4 Building Instructions for LEGO

5

6 Developing an Algorithm
How to develop an algorithm? Problem solving How to describe an algorithm? In English? In a programming language?

7 Pseudocode English statements + basic control structures in programming languages Good compromise Simple, readable, no strict rules, don’t worry about punctuation Let you think at an abstract level about the problem Allow you to focus on what needs to be done without having to deal with the syntax and grammar of a programming language

8 Pseudocode vs. Code Pseudocode VB code Get the rate from user
dblRate = Convert.ToDouble(txtRate.Text)

9 Building Blocks for Pseudocode
Basic statements Get the input (from user) Print the output (on the screen) Perform basic calculations Conditional and iterative statements Variables

10 Variables How to do (2 + 3) * (5 – 1) using a calculator?
A variable is a memory location Can store a value Addressable by its name Examples count 1 sum 5

11 Basic Statements Assign values to variables using basic arithmetic operations Set the value of x to 3 or x = 3 Set the value of y to x/10 or y = x/10 Set the value of z to x +25 or z = x + 25 Get input from user Get the value of x Print results to the screen Print the value of y, z Print “CIS160 Visual BASIC”

12 Example Write pseudocode that asks the user for three numbers, computes their sum and average and outputs the results. Get the values of a, b, c from user sum = a + b + c avg = sum / 3 Print sum, avg Input Process Output

13 Example Write pseudocode that reads the value of a circle radius from the user, and prints the area of a circle with that radius Get the value of radius from user area = pi * radius * radius Print “The area of your circle is “ & area

14 Exercise Write pseudocode that inputs the length and width of a carpet in feet and the price in dollar per square yard. It also prints out the total cost of the carpet, including a 6% sales tax. 1 square yard = 9 square feet


Download ppt "Introduction to Algorithms"

Similar presentations


Ads by Google