1 / 14

Introduction to Algorithms

Introduction to Algorithms. Yonglei Tao. What is an Algorithm?. A step-by-step description about a solution to a problem Unambiguous In a logical order Executable A program is the expression of an algorithm in a programming language. Examples of Algorithm. Making a phone call

pavel
Download Presentation

Introduction to Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Algorithms Yonglei Tao

  2. What is an Algorithm? • A step-by-step description about a solution to a problem • 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 • 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. Developing an Algorithm • How to develop an algorithm? • Problem solving • How to describe an algorithm? • In English? • In a programming language?

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

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

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

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

  10. 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”

  11. 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 • avg = ( a + b + c ) / 3 • sum = a + b + c • Print sum, avg

  12. Example • Write pseudocodethat 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

  13. Exercise • Write pseudocodethat inputs the length and width of a carpet in feet and the price in dollar per square yard. The algorithm should print out the total cost of the carpet, including a 6% sales tax.

More Related