CS 177 Recitation - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

CS 177 Recitation

Description:

Use Mozilla Firefox for the development and testing of project 2 ... In FireFox, the Error Console is a way to debug JavaScript. To access the Error Console go to: ... – PowerPoint PPT presentation

Number of Views:105
Avg rating:3.0/5.0
Slides: 23
Provided by: just154
Category:

less

Transcript and Presenter's Notes

Title: CS 177 Recitation


1
CS 177 Recitation
  • Oct 5, 2007 Week 7

2
Announcements
  • Project 2, due 900 pm on October 17, 2007.
  • Use Mozilla Firefox for the development and
    testing of project 2
  • There will be consultation hours on Tuesday Oct
    9, 2007

3
Debugging
  • What?
  • From Wikipedia
  • Debugging is a methodical process of finding and
    reducing the number of bugs, or defects, in a
    computer program or a piece of electronic
    hardware thus making it behave as expected.

4
  • Why?
  • Debugging is very useful to make it easier to
    identify errors in programs
  • How?
  • In FireFox, the Error Console is a way to debug
    JavaScript
  • To access the Error Console go toTools - Error
    Console
  • You can click on an error and go directly to the
    line that caused that error

5
ExampleWhat is the problem?
var
rMath.rando() if (r0.5) document.write(" href'http//www.A.com'Visit A!") else
document.write("Visit
B!")
6
Error Console
7
Debugging
  • The bigger the code is, the harder it is to catch
    errors
  • Debugging can save you a lot of time!
  • One trick as well is toTRACE variables using
    document.write(). This is a way to catch logical
    errors
  • document.write(before_foo()variablename)foo
    ()document.write(after_foo()variablename)

8
Final word to say here!
  • Carefully writing your code and rigorously
    testing it is a good way to make sure your code
    is error-free!
  • Test, test and test your pages!

9
Algorithms
  • The central concept underlying all computation
  • An algorithm
  • 1) Must be clear -- unambiguous, understandable,
    capable of being "executed
  • 2) Must be correct -- must yield the correct
    result(s)
  • 3) Must be complete -- no missing cases,
    information on what to do in every situation
  • 4) Must end -- the algorithm must come to some
    conclusion

10
Example Algorithm
  • Finding the oldest person
  • Line up all the people along one wall
  • As long as there is more than one person in line,
    repeatedly
  • have the people pair up (1st with 2nd, 3rd with
    4th, etc) if there is an odd number of people,
    the last person will be without a partner
  • ask each pair of people to compare their
    birthdays
  • request that the younger of the two leave the
    line
  • When there is only one person left in line, that
    person is the oldest

11
Example Algorithm (cont.)
  • Is this algorithm complete?
  • What if number of people is odd?
  • The last person, which does not have a partner,
    stays in the line for the current round
  • What if two people have the same birthday?
  • Flip a coin to see who leaves and who stays
  • Person on the left leaves
  • Now algorithm complete and could be programmed

12
Big-Oh Notation
  • To represent an algorithms performance in
    relation to the size of the problem
  • Computer Scientists use Big-Oh notation
  • Executing an O(N) algorithm requires time
    proportional to the size of the problem
  • Given an O(N) algorithm, doubling the problem
    size doubles the work
  • Executing an O(log N) algorithm requires time
    proportional to the logarithm of the problem size
  • Given an O(log N) algorithm, doubling the problem
    size adds how much more work?
  • ANSWER adds a constant amount of work

13
Big-Oh Notation (cont.)
  • What is the Big-Oh Notation of the following
    algorithm?
  • Loop(1N) Loop(1..N) print Hi
  • Hint How many times is the command print Hi
    called?
  • ANSWER Big-Oh(N2)

14
Big-Oh Notation (cont.)
  • Consider our earlier algorithm (to calculate the
    oldest person)
  • What is the Big-Oh of that algorithm?
  • O(log N)
  • Why?
  • Hint How many steps are involved? Consider a
    step to be every time that the younger people
    in the pair leaves the line.
  • Consider how many steps are involved if there
    are 16 people? How about 32?

15
Search Problem
  • Problem Finding a certain item in a list
  • Recall the following solutions discussed in
    class
  • Sequential Search sequential search is an
    algorithm that involves examining each list item
    in sequential order until the desired item is
    found
  • Binary Search binary search involves continually
    cutting the desired search list in half until the
    item is found

16
Search Analysis
  • Sequential Search
  • What is the Big-Oh? Why?
  • Worst case the item you are looking for is in
    the last spot in the list (or not in the list at
    all)
  • have to inspect and compare every entry in the
    list
  • the amount of work required is proportional to
    the list size ? sequential search is an O(N)
    algorithm

17
Search Analysis (cont.)
  • Binary Search
  • What is the Big-Oh? Why?
  • in the worst case, you will have to keep halving
    the list until it gets down to a single entry
  • each time you inspect/compare an entry, you rule
    out roughly half the remaining entries
  • the amount of work required is proportional to
    the logarithm of the list size ? binary search is
    an O(log N) algorithm

18
Constants in running time
  • Note that calculating the Big-Oh of a function
    ignores constants in the running time.
  • Example
  • IffuntionA time to execute NfunctionB time
    to execute 2N? Both functions are O(N)

19
Interpreters
  • The interpreter reads one high-level statement
    at a time, immediately translating and executing
    the statement before processing the next one
  • JavaScript is an interpreted language

20
Compilers
Translate the entire high-level language program
into its equivalent machine-language
instructions Examples C, C, Java
21
Next week
  • Chapter 11 Conditional Execution
  • Project 2 is due!

22
Questions?
Write a Comment
User Comments (0)
About PowerShow.com