- This week only, discussion sections will all meet in
PAI 5.38 on Friday.
- hardware and software
- writing algorithms - a plan for how to solve a
particular problem with the computer
- a simple example program
What is a
- A machine that processes information
- A computer executes a program, a detailed set of
instructions that tell the computer what to do to solve a particular
- the mechanical and electronic parts of
- the programs that tell the hardware
what to do
- Central Processing
Unit (CPU) - brain of the
computer. The CPU repeatedly fetches the next instruction from the
computer's memory and executes it. The CPU controls the other
components, and performs all calculations, such as addition and
subtraction and logical comparisons.
- Primary or Main Memory
- storage used to store
data and programs. Main memory consists of electronic components called
integrated circuit chips, and it is very fast. Information stored in
main memory is lost when the computer's power is turned off.
- Secondary Storage
- devices used for long-term,
permanent storage. E.g. magnetic disks, hard disks, magnetic tapes,
slow, but data is not lost when computer is turned off and large
amounts of data can be stored.
- Input Devices
- devices that bring data into
the computer. E.g. keyboard, mouse, scanner.
- Output Devices
- devices that display the
information stored in the computer. E.g. Speakers, printer, monitor.
- All data stored in binary
- Either 0 or 1
- Numbers: 1001 = 9
- Characters: 0100 0001 = 'A'
- bit: a unit of information on a computer (0 or 1)
- byte: 8 bits
- A character requires a byte of storage
Question: Which part
of a computer executes a program?
A. main memory
set of detailed instructions which
a computer carries out to solve some particular problem.
- Programs are written in a specific
language, like Python, Java, C++, Haskell, C#, Lisp, ML.
- a step by step description of a
- often written in psuedocode
- a program converts an algorithm into
- An algorithm must have the following
- finiteness - the process
terminates; the number of steps in the algorithm is finite
- definiteness - each step is
Write an algorithm to
compute the sum of the first 100 positive integers.
1. Set sum to 0.
2. Set n to 1.
3. Repeat while n <= 100:
3a. Set sum to sum + n.
3b. Set n to n+1.
4. Print sum.
Convert this algorithm into
sum = 0
n = 1
Add all integers from 1 to 100 to sum
while n <= 100: # do this as long as n is at most 100
sum = sum + n
n = n+1
- High-level Languages
- statements look a lot like English
- easily read and understood by humans
- computers do NOT understand high-level languages
- Machine Language
- instructions now in form of sequences of 0s and 1s
- 0s and 1s correspond to off and on in electronic
- Each chip (PowerPC, etc) has its own machine language
that it understands
- Programs called translators
convert high-level languages to machine/object code
- A compiler translates an entire high-level program
into machine language
- The Python interpreter
is a program which converts each command in your Python program to
machine language and executes it immediately.
- A step by step description
of the solution to a
- Algorithm to calculate the
area of a rectangle:
- 1. Multiply length and width.
1. Initialize the sum to 0.
- Algorithm to compute the sum of a list
If there are no numbers remaining, go to step 5.
Add the next number to the sum.
Go to step 2.
Output the sum.
for this algorithm:
- pseudocode is a mix of
language structures and the English algorithm.
- Looping structures like the while
statement are found in most
Set sum to 0
(more numbers remain) add next number to sum
Exercise: Write an algorithm
that prints the numbers from 1 to 100. (You can use psuedocode).
Exercise: Write an algorithm
that prints the sum of all the even
numbers from 1 to 100.