# Computations

Document Sample

```					    Intro to CPSC100-
102/WMST425R, Continued

Course Preview
this lecture
• how does a computer do specific tasks?
• how can a computer adapt to whatever
software is run?
looking up a directory
looking up a directory
• open the directory in roughly the place where the
name might be. (Zhang near the end; Abbot near
the front, Lee near the middle)
• if opened at a page with names earlier (later) in the
alphabet than the desired name, flick a few pages
further on (further back)
• keep doing this until you find the right page
• scan the names on that page until you find the
right name
levels of abstraction
looking up a phone number could be
described in terms of neuronal activation in
the brain, but we don’t normally describe
this process in this way for good reason
looking up “algorithm” in
www.atomica.com
looking up “algorithm”
definition of “algorithm”
open up web browser;     pops up in window
access www.atomica.com
yet more
communication: get
type in “algorithm”
defn from server

use                  atomica web page server
communication        retrieves definition of
protocol to send     “algorithm” from its
request to server    memory
binary search algorithm:
programmer’s view
1. dictionary stored in an array (in computer memory)

2. compare middle array entry with query
– if entry < query, remove leftmost array entries up to and
including middle
– if entry > query, remove rightmost array entries starting
from middle
elements of a program
• data structures + operations on the data
– compare words
• control flow instructions
– while/repeat
– if ... then ... else
why did the programmer die in
the shower?

- the shampoo directions said
to lather, rinse, repeat
calculating the middle array index
• middle of array from 1 to 7?

1       2       3       4       5       6       7

• middle of array from 1 to 13?

1   2    3       4       5       6       7       8       9       10       11   12   13

• middle of array from 1 to n (n odd)?

1           2               ....                             n
calculating the middle index
range        middle index

1…3          2
1…5          3
1…7          4
…            …
1…n           (n+1)/2
calculating the middle index
range        middle index

3…9          6
3…11         7
3…n          (n+3)/2
…            …
m…n          (n+m)/2
real java code for binary search!
private int search(int target, int first, int last)
{ int mid;
mid = (first + last)/2;
if (target == a[mid] result = mid;
else if (target < a[mid])
result = search(target, first, mid-1);
else result = search(target, mid+1, last);
return result;                             }
lower level of abstraction
• information stored in bits
• complicated, multi-level storage
• primitive instruction set
... possibly executed by two or more processors
representing text in binary
symbol             bit representation

a                  01001001
b                  00001111
c                  10110110
d                  10010111

• bad is represented as “000011110100100110010111”
• what does “10110110 01001001 00001111” represent?
representing algorithms in binary
• an algorithm is just a piece of text, and so
can be represented in binary too
• ... therein lies the true power -
programmability- of the computer!
inner workings of the computer

• program + data are stored in the computer’s
memory
• the computer processor is an algorithm that
executes the instructions in the stored program
• thus, the heart of a computer is an algorithm that
takes as input another algorithm and executes it!
many middle layers
• compiler: algorithm that converts a
programmer’s code to the simple form that
the processor understands
• operating system: algorithm that organizes
programs and data in memory, and
coordinate interaction with users and other
systems
consequences
• high level programming language must be
automatically convertible to low-level code
• this puts constraints on the expressiveness
of computer programming languages
• in particular, programming languages have
to be very, very, precise
summary
• components of a computation: input, output,
programs, communication
• program: simple instructions on data plus
control flow statements
• programs and data are represented in binary
notation in a computer’s memory
• computer processor executes the instructions
in a program
vocabulary
•   algorithm, protocol, program, process...
•   binary search algorithm
•   data structure, array
•   control flow statements
•   binary representation of data
•   computer processor
•   compiler, operating system
to do