1 / 102

Experience with Algorithms

Experience with Algorithms.

kara
Download Presentation

Experience with 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. Experience with Algorithms • Before we proceed to programming computers, we need to prepare ourselves by learning how to think about programming.  At the heart of programming is the algorithm, basically a very carefully expressed problem-solving strategy, usually in the form of a structured set of instructions.  We say we use or apply an algorithm to solve the problem it was designed for.

  2. Experience with Algorithms • Programs are algorithms and all of the applications you use daily are programs—simple set of rules in software • You use algorithms all the time • Google search, ATM, online purchases, dialing the phone, GPS directions, recipes

  3. Is it an Algorithm? • The process of doing something step-by-step may not be not an algorithm • An algorithm is a “systematic method for producing a specified result” • Searching is purposeful, and provides direction…with no guarantee of a result! • Algorithms ALWAYS work! • Heuristic might describe the search! Heuristics are helpful procedures for finding a result

  4. Algorithm Properties • An algorithm must have five properties: • Input specified • Output specified • Definiteness • Effectiveness • Finiteness

  5. 1. Input Specified • The input is the data to be transformed during the computation to produce the output. • What data do you need to begin to get the result you want? • Input precision requires that you know what kind of data, how much and what form the data should be

  6. 2. Output Specified • The output is the data resulting from the computation (your intended result) • Frequently the name of the algorithm contains the output: • “Algorithm to compute batting average” • Output precision also requires that you know what kind of data, how much and what form the output should be (or even if there will be any output at all!)

  7. 3. Definiteness • Algorithms must specify every step and the order the steps must be taken in the process • Definiteness means specifying the sequence of operations for turning input into output • Details of each step must be also be spelled out (including how to handle errors)

  8. 4. Effectiveness • For an algorithm to be effective, it means that all those steps that are required to get to output MUST BE DOABLE!

  9. 5. Finiteness • The algorithm must stop, eventually! • Stopping may mean that you get the expected output OR you get a response that no solution is possible • Finiteness is not usually an issue for noncomputer algorithms • Computer algorithms often repeat instructions with different data and finiteness may be a problem

  10. Algorithm Facts • Algorithms can be specified at different levels of detail • Algorithms use functions to simplify the algorithmic description • These functions (such as scan) may have their own algorithms associated with them

  11. Algorithm Facts • Algorithms always build on functionality previously defined and known to the user • Assume the use familiar functions and algorithms • For example, how might scan be defined? Would it be consistent for everyone? Could it mean alphabetize? Look for similar formats? Are these the same?

  12. Algorithm Facts • Different algorithms can solve the same problem differently, and the different solutions can take different amounts of time (or space)

  13. How Do We Know it Works? • Algorithm solution is clear and simple and efficient • Then, how do we know it works? • If there is no loop, the program runs, gets to an end, and we can check the result • What if there is a loop? • Programs with loops cannot be absolutely verified that it works…there are too many possible cases

  14. Everyday Algorithm • Algorithm for getting dressed Let's call our algorithm  get_dressed()

  15. Scenario 1 get_dressed() { put_on( red_shirt ); put_on( blue_jeans ); put_on( belt ); put_on( white_socks ); put_on( sneakers ); }

  16. Scenario 2 get_dressed() { put_on( tshirt ); if( is_dirty(blue_jeans) ) put_on( black_jeans ); else put_on( blue_jeans ); if( is_missing(sandals) ) put_on( sneakers ); else put_on( sandals ); }

  17. Scenario 3 get_dressed( int temperature ) { if( temperature > 75 ) /* it's hot out */ putOn( tshirt ); putOn( shorts ); putOn( sandals ); else if( temperature > 60 ) /* it's warm out */ putOn( blue_shirt ); putOn( blue_jeans ); putOn( white_socks ); putOn( sneakers ); else /* it's kind of cold */ putOn( sweatshirt ); putOn( blue_jeans ); putOn( wool_socks ); putOn( shoes ); putOn( jacket ); }

  18. Programming Concepts • Programming is the act of formulating an algorithm or program • A systematic means of solving a problem is designed • Someone (and a computer) can follow the instructions and produce the intended result for every input, every time

  19. Programming Concepts • The program must be able to perform or be executed without the programmer • All steps must be spelled out precisely and effectively • All contingencies must be planned for

  20. Programming Concepts • Programming requires thinking • Basic programming concepts provide tools needed to formulate any computation • Trying to program an algorithm precisely using English is hopeless • Natural languages are too ambiguous for directing anything as clueless as a computer

  21. Programming Concepts • Programming languages have been developed to help programmers in two ways: • Precision • Specialized for using the earlier mentioned concepts • Using a programming language is actually easier than writing in English

  22. Programming Concepts • This chapter introduces the following programming concepts: • Names, values, and variables • Declarations • Data types, numbers, string literals, and Booleans • Assignment • Expressions • Conditionals

  23. Names, Values, and Variables • Names Have Changing Values • Names and values are separable in programming • Names have changing values

  24. Variables Are Names in a Program • In programming terminology, the names are called variables • Variables mean that values vary • The most commonly used programming language operation is the command to change the value of a variable: • Called assignment

  25. Identifiers and Their Rules • The letter sequence that makes up a variable’s name is called the identifier • Identifiers have a particular form • Identifiers must begin with a letter, followed by any sequence of letters, numerals, or the underscoresymbol • Identifiers are not allowed to contain spaces

  26. Identifiers and Their Rules • Note two features of identifiers: • The underscore symbol can be used as a word separator • It makes identifiers more readable • There is a “no spaces” rule • Identifiers are case sensitive • uppercase and lowercase letters are different

  27. Variable Declaration Statement • Programs are usually written “starting from scratch” • The first thing to do when writing any program is to state or declare what variables will be used • Declaring variables is done using a command called a declaration • In JavaScript, the declaration command is the word var, followed by a list of the identifiers for the variables to be declared, separated by commas

  28. Variable Declaration Statement var area, radius; • This command declares that two identifiers (area, radius) will be used as variables

  29. The Statement Terminator • A program is simply a list of statements; • Each statement must be terminated by some punctuation symbol; • The statement terminator in JavaScript is the semicolon; • The computer needs the semicolon to know when a statement is complete; • Terminate every statement with a semicolon;

  30. Rules for Declaring Variables • Every variable used must be declared • JavaScript allows declaration statements anywhere in the list of statements • Variable declarations announce what variables will be used in the program • Declare variables first

  31. Undefined Values • The declaration states that the identifier is the name of a variable • The name has no value at first, it is not defined • It is a name that doesn’t name anything • The name is declared but there is no value assigned yet • The value is undefined

  32. Initializing a Declaration • Sometimes there is an initial value for identifiers • JavaScript allows setting the initial value as part of the declaration • This is called initializing the variable • Declaring variables with initial values is written as: • var taxRate = .088; • var balanceDue = 0;

  33. Initializing a Declaration • Variables can be declared and initialized by separating them with commas: • var taxRate = .088, balanceDue = 0; • Usually several variables are declared in a single declaration statement when the variables are logically related • If the variables are not related, they are usually specified in separate statements

  34. JavaScript

  35. Introduction to JavaScript JavaScript HTML CSS Style Sheet Language PRESENTATION Markup Language CONTENT Programming Language BEHAVIOR

  36. What is Scripting Language? WEB BROWSER WEB PAGE JavaScript

  37. Creating your first JavaScript Start with BASIC HTML

  38. Creating your first JavaScript

  39. Understanding the structure of JavaScript code

  40. Understanding the structure of JavaScript code

  41. Keep in mind… • Don’t forget semicolon at the end of each statement! • Whitespace Insensitive • Use external page in js folder <script src= “js/myscript.js”></script> // this is a comment

  42. Creating variables underscore

  43. Creating Statements

  44. Three Basic Data Types of JavaScript • There are three types of data in JavaScript programs that will be used in this book: • numbers, • strings, and • Booleans

  45. Rules for Writing Numbers • There are rules for writing numbers • One “unusual” aspect of numbers in programming is that there are no “units” • Numbers must be written in decimal form (0.33, not 33%; 10.89, not $10.89) • Standard computer numbers: • Have about 10 significant digits • Range from as small as 10−324 to as large as 10308

  46. Rules for Writing Numbers • Numbers and computer arithmetic are unexpectedly subtle • As a general rule, the “safe zone” for numbers is the range from 2 billionths to 2 billion plus or minus

  47. Strings • Strings are a common kind of data • Stringsare “sequences of keyboard characters” • Notice that a string is always surrounded by single (') or double (") quotes • Strings can initialize a declaration • Strings are needed when manipulating text

More Related