# Computers and Algorithms

• Announcements:
• This week only, discussion sections will all meet in PAI 5.38 on Friday.

Today's Topics:
• hardware and software
• writing algorithms - a plan for how to solve a particular problem with the computer
• a simple example program

# What is a computer?

• A machine that processes information and performs calculations
• A computer executes a program, a detailed set of instructions that tell the computer what to do to solve a particular problem
• Hardware - the mechanical and electronic parts of the computer
• Software - the programs that tell the hardware what to do
• Components:
• 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, CDs. Relatively 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.

Storage

• 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
B. CPU
C. keyboard

# What is programming?

• program:
• set of detailed instructions which a computer carries out to solve some particular problem.
• Programs are written in a specific programming language, like Python, Java, C++, Haskell, C#, Lisp, ML.
• algorithm:
• a step by step description of a problem's solution
• often written in psuedocode
• a program converts an algorithm into a particular programming language
• An algorithm must have the following properties:
• finiteness - the process terminates; the number of steps in the algorithm is finite
• definiteness - each step is precisely stated

Algorithm Example

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 Python:

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
print sum

Programming Languages

• 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 devices
• 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

• Python is Different
• The Python interpreter is a program which converts each command in your Python program to machine language and executes it immediately.

# Algorithms: More Examples

• A step by step description of the solution to a specified problem.
• Algorithm to calculate the area of a rectangle:
• 1. Multiply length and width.

• Algorithm to compute the sum of a list of numbers:
1. Initialize the sum to 0.
2. If there are no numbers remaining, go to step 5.
3. Add the next number to the sum.
4. Go to step 2.
5. Output the sum.

Psuedocode for this algorithm:
• pseudocode is a mix of programming language structures and the English algorithm.
• Looping structures like the while statement are found in most programming languages.

Set sum to 0
while (more numbers remain) add next number to sum
print the 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.