Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Local versus Global Variables - Thinking Like Computers - Lecture Slides, Slides of Artificial Intelligence

During the course work of Thinking Like Computers, we study the key concept of artificial intelligence. The main points in these lecture slides are:Local Versus Global Variables, Errors to Avoid, Javascript Functions, Designing

Typology: Slides

2012/2013

Uploaded on 04/24/2013

banani
banani 🇮🇳

4.3

(3)

91 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CSCI 100
Think Like Computers
Lecture 17
Fall 2008
Last Time …
Abstractions: functions
User defined JavaScript functions
Define function in <head> </head>
Or, include .js file
Use function in <body> </body>
Local versus global variables
Use var inside function definitions
Otherwise, global variables are assumed
Errors to Avoid
When beginning programmers attempt to load a JavaScript code
library, errors of two types commonly occur:
if the SCRIPT tags are malformed or the name/address of the
library is incorrect, the library will fail to load
this will not cause an error in itself, but any subseq uent attempt to call
a function from the library will produce
“Error: Object Expected” (using Internet Explorer)
or
“Error: XXX is not a function” (using Navigator), where XXX
represents the typed function name
Errors to Avoid
when you use the SRC attribute in a pair of SCRIPT tags to load a
code library, you cannot place additional JavaScript code
between the tags
think of the SRC attribute as causing the contents of the libr ary to be
inserted between the tags, overwriting any other code th at was
erroneously placed there
<script type="text/javascript" src="FILENAME">
ANYTHING PLACED IN HERE WILL BE IGNORED
</script>
if you want additional JavaScript code or another librar y, you must
use another pair of SCRIPT tags
Algorithms
the central concept underlying all computation is
that of the algorithm
an algorithm is a step-by-step sequence of instructions
for carrying out some task
programming can be viewed as the process of
designing and implementing algorithms that a
computer can carry out
a programmer’s job is to:
create an algorithm for accomplishing a given objective, then
translate the individual steps of the algorithm into a
programming language that the computer can understand
Algorithms
example: programming in JavaScript
we have written programs that instruct the browser to
carry out a particular task
given the proper instructions, the browser is able to
understand and produce the desired results
Docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Local versus Global Variables - Thinking Like Computers - Lecture Slides and more Slides Artificial Intelligence in PDF only on Docsity!

CSCI 100

Think Like Computers

Lecture 17

Fall 2008

Last Time …

• Abstractions: functions

• User defined JavaScript functions

Š Define function in

Š Or, include .js file

Š Use function in

• Local versus global variables

Š Use var inside function definitions

Š Otherwise, global variables are assumed

Errors to Avoid

  • When beginning programmers attempt to load a JavaScript code library, errors of two types commonly occur:

‡ if the SCRIPT tags are malformed or the name/address of the library is incorrect, the library will fail to load ‡ this will not cause an error in itself, but any subsequent attempt to call a function from the library will produce “Error: Object Expected” (using Internet Explorer) or “Error: XXX is not a function” (using Navigator), where XXX represents the typed function name

Errors to Avoid

‡ when you use the SRC attribute in a pair of SCRIPT tags to load a code library, you cannot place additional JavaScript code between the tags ‡ think of the SRC attribute as causing the contents of the library to be inserted between the tags, overwriting any other code that was erroneously placed there

‡ if you want additional JavaScript code or another library, you must use another pair of SCRIPT tags

Algorithms

• the central concept underlying all computation is

that of the algorithm

Š an algorithm is a step-by-step sequence of instructions

for carrying out some task

• programming can be viewed as the process of

designing and implementing algorithms that a

computer can carry out

Š a programmer’s job is to:

ƒ create an algorithm for accomplishing a given objective, then ƒ translate the individual steps of the algorithm into a programming language that the computer can understand

Algorithms

• example: programming in JavaScript

Š we have written programs that instruct the browser to

carry out a particular task

Š given the proper instructions, the browser is able to

understand and produce the desired results

Algorithms in the Real World

  • the use of algorithms is not limited to

the domain of computing

Š e.g., recipes for baking cookies Š e.g., directions to your house

  • there are many unfamiliar tasks in life

that we could not complete without the

aid of instructions

Š in order for an algorithm to be effective, it must be stated in a manner that its intended executor can understand ƒ a recipe written for a master chef will look different than a recipe written for a college student Š as you have already experienced, computers are more demanding with regard to algorithm specifics than any human could be

Designing & Analyzing

Algorithms

• 4 steps to solving problems (George Polya)

1. understand the problem

2. devise a plan

3. carry out your plan

4. examine the solution

Designing & Analyzing

Algorithms

• EXAMPLE: finding the oldest person in a room full

of people

1. understanding the problem

ƒ initial condition – room full of people ƒ goal – identify the oldest person ƒ assumptions 9 a person will give their real birthday 9 if two people are born on the same day, they are the same age 9 if there is more than one oldest person, finding any one of them is okay

2. we will consider 2 different designs for solving this

problem

Algorithm 1

  • Finding the oldest person (algorithm 1)
    1. line up all the people along one wall
    2. ask the first person to state his or her name and birthday, then write this information down on a piece of paper
    3. for each successive person in line: i. ask the person for his or her name and birthday ii. if the stated birthday is earlier than the birthday on the paper, cross out old information and write down the name and birthday of this person when you reach the end of the line, the name and birthday of the oldest person will be written on the paper

Algorithm 2

  • Finding the oldest person (algorithm 2)
    1. line up all the people along one wall
    2. as long as there is more than one person in the line, repeatedly i. have the people pair up (1 st^ with 2 nd, 3 rd^ with 4 th, etc) – if there is an odd number of people, the last person will be without a partner ii. ask each pair of people to compare their birthdays iii. request that the younger of the two leave the line

when there is only one person left in line, that person is the oldest

Algorithm Analysis

  • determining which algorithm is "better" is not always clear

cut

Š it depends upon what features are most important to you ƒ if you want to be sure it works, choose the clearer algorithm ƒ if you care about the time or effort required, need to analyze performance

  • algorithm 1 involves asking each person’s birthday and

then comparing it to the birthday written on the page

Š the amount of time to find the oldest person is proportional to the number of people Š if you double the amount of people, the time needed to find the oldest person will also double

Binary Search Example

suppose you have a sorted list of state names, and want to find Illinois

  1. start by examining the middle entry ( Missouri ) since Missouri comes after Illinois alphabetically, can eliminate it and all entries that appear to the right
  2. next, examine the middle of the remaining entries ( Florida ) since Florida comes before Illinois alphabetically, can eliminate it and all entries that appear to the left
  3. next, examine the middle of the remaining entries ( Illinois ) the desired entry is found

Search Analysis

• sequential search

Š in the worst case, the item you are looking for is in the

last spot in the list (or not in the list at all)

ƒ as a result, you will 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

• binary search

Š 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

Search Analysis

• imagine searching a phone book of the United

States (280 million people)

Š sequential search requires at most 280 million

inspections/comparisons

Š binary search requires at most ⎡log 2 (280,000,000)⎤ = 29

inspections/comparisons

Another Algorithm Example

  • Newton’s Algorithm for finding the square root of N

1. start with an initial approximation of 1

2. as long as the approximation isn’t close enough,

repeatedly

i. refine the approximation using the formula: newApproximation = (oldApproximation + N/oldApproximation)/

example: finding the square root of 1024

Another Algorithm Example

• algorithm analysis:

Š Newton's Algorithm does converge on the square root

because each successive approximation is closer

than the previous one

ƒ however, since the square root might be a non-terminating fraction it is difficult to define the exact number of steps for convergence

Š in general, the difference between the given

approximation and the actual square root is roughly

cut in half by each successive refinement

Æ demonstrates O(log N) behavior

Algorithms and Programming

  • programming is all about designing and coding algorithms for solving problems Š the intended executor is the computer or a program executing on that computer Š instructions are written in programming languages which are more constrained and exact than human languages
  • the level of precision necessary to write programs can be frustrating to beginners Š but it is much easier than it was 50 years ago Š early computers (ENIAC) needed to be wired to perform computations Š with the advent of the von Neumann architecture, computers could be programmed instead of rewired ƒ an algorithm could be coded as instructions, loaded into the memory of the computer, and executed

Machine Languages

  • the first programming languages were known as

machine languages

Š a machine language consists of instructions that correspond directly to the hardware operations of a particular machine ƒ i.e., instructions deal directly with the computer’s physical components including main memory, registers, memory cells in CPU ƒ very low level of abstraction Š machine language instructions are written in binary ƒ programming in machine language is tedious and error prone ƒ code is nearly impossible to understand and debug

  • excerpt from a machine language program:

High-Level Languages

  • in the early 1950’s, assembly languages evolved from machine languages Š an assembly language substitutes words for binary codes Š much easier to remember and use words, but still a low level of abstraction (instructions correspond to hardware operations)
  • in the late 1950's, high-level languages were introduced Š high-level languages allow the programmer to write code closer to the way humans think (as opposed to mimicking hardware operations) Š a much more natural way to solve problems Š plus, programs are machine independent

Program Translation

  • using a high-level language, the programmer is able to reason at a high-level of abstraction Š but programs must still be translated into machine language that the computer hardware can understand/execute
  • there are two standard approaches to program translation Š interpretation Š compilation
  • real-world analogy: translating a speech from one language to another Š an interpreter can be used provide a real-time translation ƒ the interpreter hears a phrase, translates, and immediately speaks the translation ƒ ADVANTAGE: the translation is immediate ƒ DISADVANTAGE: if you want to hear the speech again, must interpret all over again Š a translator (or compiler ) translates the entire speech offline ƒ the translator takes a copy of the speech, returns when the entire speech is translated ƒ ADVANTAGE: once translated, it can be read over and over very quickly ƒ DISADVANTAGE: must wait for the entire speech to be translated

Speech Translation

  • Interpreter:
  • Translator (compiler):

Interpreters

  • for program translation, the interpretation approach relies on a program known as an interpreter to translate and execute high-level statements Š 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

Compilers

  • the compilation approach relies on a program known as a compiler to translate the entire high-level language program into its equivalent machine-language instructions Š the resulting machine-language program can be executed directly on the computer Š most languages used for the development of commercial software employ the compilation technique (C, C++)