You are on page 1of 62

Introduction to Computation

and Programming
Topic 1: Getting Started
Reading: [Guttag, Chapter 1]

Slides prepared by Louay Bazzi for EECE 230C, Fall 2018-19, MSFEA, AUB.
Updated with minor edits during the offerings of EECE 230, Spring 2018-19, and Fall 2020-21, MSFEA, AUB
Updated in 2020-21 in preparation for EECE 230X, MSFEA, AUB
Some of the material in these slides is taken from [Guttag, Chapter 1]

This work is licensed under a Creative Commons Attribution-


NonCommercial-ShareAlike 4.0 International License.
Outline
1. Algorithms and 5. Sample python code
computational problem Syntax
solving Static semantics
Finding square root Logical errors
Searching a sorted list Spyder IDE
References and useful
2. Stored program links
computers
3. Binary representation
4. Programming languages
EECE 230X - Introduction to Computation and Programming
Topic 1: Lesson 1

Algorithms and computational problem solving

EECE 230X - Introduction to Computation and Programming


Computers and problem solving
• A computer:
can perform calculations efficiently
can remember a huge amount of results and data

• Algorithm for a given problem:


a finite list of instructions which given an input
produces in a finite number of steps a desired output

• Computational problem solving:


design and implement an efficient algorithm to solve a
given problem

EECE 230X - Introduction to Computation and Programming


Problem 1: find square root
• Problem: given a nonnegative number x (the input),
approximate its square root
• E.g., for x = 10, x =3.16227….
• Interested in an approximation g of x such that g*g
is close enough of to x
• You are allowed to perform multiplications,
additions, and divisions
• Algorithm (due to Heron of Alexandria, around 2000
years ago):
e enough to x, Rationale
EECE 230X - Introduction to Computation and Programming
Problem 1: find square root
• Algorithm due to Heron of Alexandria, around 2000 years
ago:
Input: x and Starting from this key
tolerance parameter 𝜖>0 idea, try to figure out
the rest of Heron’s
Start with a guess g>0
algorithm?
If g*g is close enough to x, i.e.,
𝑔 ∗ 𝑔 − 𝑥 ≤ 𝜖: stop
Else update g: Rat
Key idea: 𝑥 must be between g and x/g: Why?
- If √𝑥< g and 𝑥/g, then 𝑥=√𝑥×√𝑥 < g × 𝑥/g = 𝑥, contradiction
- If √𝑥> g and 𝑥/g, then 𝑥=√𝑥×√𝑥 > g × 𝑥/g = 𝑥, contradiction
EECE 230X - Introduction to Computation and Programming
Problem 1: find square root
• Algorithm due to Heron of Alexandria, around 2000 years
ago:
Input: x and
tolerance parameter 𝜖>0
Start with a guess g>0
If g*g is close enough to x, i.e.,
𝑔 ∗ 𝑔 − 𝑥 ≤ 𝜖: stop
Else update g: Rat
Key idea: 𝑥 must be between g and x/g
Thus update guess to middle of this interval:
the average of g and x/g
EECE 230X - Introduction to Computation and Programming
Problem 1: find square root
• Algorithm due to Heron of Alexandria, around 2000 years
ago:
Input: x and
tolerance parameter 𝜖>0
Start with a guess g>0
If g*g is close enough to x, i.e.,
𝑔 ∗ 𝑔 − 𝑥 ≤ 𝜖: stop
Else update g: assign to g the value (g+x/g)/2
Repeat the above process until 𝑔 ∗ 𝑔 − 𝑥 ≤ 𝜖
Output: g
• Rationale
EECE 230X - Introduction to Computation and Programming
Problem 1: find square root
Consider x = 25 with initial guess of g = 3 and 𝜖 = 0.5
• Compute 3*3 = 9 and compare to 25 x: 25
• Not close enough to 25, so update g: 3
g to (3 + 25/3)/2 = 5.67 𝜖: 0.5
• Compute 5.67*5.67 = 32.15 and compare to 25
• Still not close enough to 25, so update
g to (5.67 + 25/5.67)/2 = 5.04
• Compute 5.04*5.04 = 25.04 and compare to 25
• Close enough, so stop and return 5.04
EECE 230X - Introduction to Computation and Programming
Problem 1: find square root
Consider x = 25 with initial guess of g = 3 and 𝜖 = 0.5
• Compute 3*3 = 9 and compare to 25 x: 25
• Not close enough to 25, so update g: 3
g to (3 + 25/3)/2 = 5.67 𝜖: 0.5
• Compute 5.67*5.67 = 32.15 and compare to 25
• Still not close enough to 25, so update
g to (5.67 + 25/5.67)/2 = 5.04
• Compute 5.04*5.04 = 25.04 and compare to 25
• Close enough, so stop and return 5.04
EECE 230X - Introduction to Computation and Programming
Problem 1: find square root
Consider x = 25 with initial guess of g = 3 and 𝜖 = 0.5
• Compute 3*3 = 9 and compare to 25 x: 25
• Not close enough to 25, so update g: 5.67
g to (3 + 25/3)/2 = 5.67 𝜖: 0.5
• Compute 5.67*5.67 = 32.15 and compare to 25
• Still not close enough to 25, so update
g to (5.67 + 25/5.67)/2 = 5.04
• Compute 5.04*5.04 = 25.04 and compare to 25
• Close enough, so stop and return 5.04
EECE 230X - Introduction to Computation and Programming
Problem 1: find square root
Consider x = 25 with initial guess of g = 3 and 𝜖 = 0.5
• Compute 3*3 = 9 and compare to 25 x: 25
• Not close enough to 25, so update g: 5.67
g to (3 + 25/3)/2 = 5.67 𝜖: 0.5
• Compute 5.67*5.67 = 32.15 and compare to 25
• Still not close enough to 25, so update
g to (5.67 + 25/5.67)/2 = 5.04
• Compute 5.04*5.04 = 25.04 and compare to 25
• Close enough, so stop and return 5.04
EECE 230X - Introduction to Computation and Programming
Problem 1: find square root
Consider x = 25 with initial guess of g = 3 and 𝜖 = 0.5
• Compute 3*3 = 9 and compare to 25 x: 25
• Not close enough to 25, so update g: 5.04
g to (3 + 25/3)/2 = 5.67 𝜖: 0.5
• Compute 5.67*5.67 = 32.15 and compare to 25
• Still not close enough to 25, so update
g to (5.67 + 25/5.67)/2 = 5.04
• Compute 5.04*5.04 = 25.4 and compare to 25
• Close enough, so stop and return 5.04
EECE 230X - Introduction to Computation and Programming
Problem 2: search for a number in a sorted list
• Assume that you have a large list of n numbers
sorted in memory in non-decreasing order
• Example: 1 4 5 7 11 20 57 66 67 81 92
• Think about memory as the whiteboard
• Given a number x, check if x is in the list: YES/NO
answer
• Examples:
For x = 7, answer: YES
For x = 10, answer: NO
• Naive algorithm: traverse the elements sequentially
and compare with x
• Try to come with a faster algorithm which takes
advantage of the fact that the numbers are sorted
EECE 230X - Introduction to Computation and Programming
Problem 2: search for a number in a sorted list
Binary search algorithm:
• Compare x to the middle element in the list
• If equal:
stop and output YES
• If x > middle:
narrow down search to upper sublist excluding middle
• If x< middle:
narrow down search to lower sublist excluding middle
• Repeat the above until:
x is found
or sublist is empty, in which case output NO
EECE 230X - Introduction to Computation and Programming
Problem 2: search for a number in a sorted list

• Example: start mid end


List:
1 4 5 7 11 20 57 66 67 81 92
0 1 2 3 4 5 6 7 8 9 10

Search for x = 7
Search for x = 10
start:0
mid:5
• Faster than sequential search end:10

EECE 230X - Introduction to Computation and Programming


Problem 2: search for a number in a sorted list

• Example: start end

List:
1 4 5 7 11 20 57 66 67 81 92
0 1 2 3 4 5 6 7 8 9 10

Search for x = 7
Search for x = 10
start:0
mid:5
• Faster than sequential search end:4

EECE 230X - Introduction to Computation and Programming


Problem 2: search for a number in a sorted list

• Example: start mid end

List:
1 4 5 7 11 20 57 66 67 81 92
0 1 2 3 4 5 6 7 8 9 10

Search for x = 7
Search for x = 10
start:0
mid:2
• Faster than sequential search end:4

EECE 230X - Introduction to Computation and Programming


Problem 2: search for a number in a sorted list

• Example: start
end

List:
1 4 5 7 11 20 57 66 67 81 92
0 1 2 3 4 5 6 7 8 9 10

Search for x = 7
Search for x = 10
start:3
mid:2
• Faster than sequential search end:4

EECE 230X - Introduction to Computation and Programming


Problem 2: search for a number in a sorted list

• Example: mid
start
end

List:
1 4 5 7 11 20 57 66 67 81 92
0 1 2 3 4 5 6 7 8 9 10

Search for x = 7: done: found!


Search for x = 10
start:3
mid:3
• Faster than sequential search end:4

EECE 230X - Introduction to Computation and Programming


Problem 2: search for a number in a sorted list

• Example: start mid end


List:
1 4 5 7 11 20 57 66 67 81 92
0 1 2 3 4 5 6 7 8 9 10

Search for x = 10

• Faster than sequential search start:0


mid:5
end:10

EECE 230X - Introduction to Computation and Programming


Problem 2: search for a number in a sorted list

• Example: start
end
List:
1 4 5 7 11 20 57 66 67 81 92
0 1 2 3 4 5 6 7 8 9 10

Search for x = 10

• Faster than sequential search start:0


mid:5
end:4

EECE 230X - Introduction to Computation and Programming


Problem 2: search for a number in a sorted list

• Example: start mid end


List:
1 4 5 7 11 20 57 66 67 81 92
0 1 2 3 4 5 6 7 8 9 10

Search for x = 10

• Faster than sequential search start:0


mid:2
end:4

EECE 230X - Introduction to Computation and Programming


Problem 2: search for a number in a sorted list

• Example: start end


List:
1 4 5 7 11 20 57 66 67 81 92
0 1 2 3 4 5 6 7 8 9 10

Search for x = 10

• Faster than sequential search start:3


mid:2
end:4

EECE 230X - Introduction to Computation and Programming


Problem 2: search for a number in a sorted list

• Example: start
mid
end
List:
1 4 5 7 11 20 57 66 67 81 92
0 1 2 3 4 5 6 7 8 9 10

Search for x = 10

• Faster than sequential search start:3


mid:3
end:4

EECE 230X - Introduction to Computation and Programming


Problem 2: search for a number in a sorted list

• Example:
start end
List:
1 4 5 7 11 20 57 66 67 81 92
0 1 2 3 4 5 6 7 8 9 10

Search for x = 10

• Faster than sequential search start:4


mid:2
end:4

EECE 230X - Introduction to Computation and Programming


Problem 2: search for a number in a sorted list

• Example: mid

start end
List:
1 4 5 7 11 20 57 66 67 81 92
0 1 2 3 4 5 6 7 8 9 10

Search for x = 10

• Faster than sequential search start:4


mid:4
end:4

EECE 230X - Introduction to Computation and Programming


Problem 2: search for a number in a sorted list

• Example: start
end
List:
1 4 5 7 []11 20 57 66 67 81 92
0 1 2 3 4 5 6 7 8 9 10

Search for x = 10: done: not found

start:4
mid:4
end:3

EECE 230X - Introduction to Computation and Programming


Problem 3: Sorting problem
• Problem: given a list of number, sort it in non-
decreasing order
• Example:
Input: 8 3 4 9 5 2 12 6
Desired output: 2 3 4 5 6 8 9 12
• Algorithm:
One algorithm is inspired by sorting a hand of
cards
Will study various sorting algorithms in this
course
EECE 230X - Introduction to Computation and Programming
Concepts from the above examples
• Input
• Output
• Memory
• Update value of a variable
• Add, divide, multiply, compare
• If a condition holds, do something
• Repeat something until a condition is met

EECE 230X - Introduction to Computation and Programming


Topic 1: Lesson 2

Stored program computers

EECE 230X - Introduction to Computation and Programming


Computers and Programs
• Early computing machines: fixed-program
computers, e.g., calculator

• Modern computing machines: stored-program


computer
General purpose
Program: sequence of instructions
Both program and data stored in memory

• History:
Turing machines, 1936
Von Neumann architecture, 1945
EECE 230X - Introduction to Computation and Programming
Computer Architecture Overview
Main memory
Central Processing
Unit (CPU) Data Input
Control Unit e.g., list of
numbers and
Instruction Register:
Instruction 2 Output
Program Counter Program:
… e.g.,
Instruction 1
Arithmetic Logic Unit Instruction 2 Keyboard
performs operations such Instruction 3 Display
as addition, ….
Disk drive
multiplication, AND, OR

EECE 230X - Introduction to Computation and Programming
Computer Architecture Overview
• Everything in memory is represented in binary, i.e.,
zeros and ones
• Computer circuitry process binary values
• Low-level instructions, i.e., machine-level
programing language
• More on computer architecture and machine-level
languages in second year courses EECE 320 and
EECE 321

EECE 230X - Introduction to Computation and Programming


Computer Architecture Overview
• In this course, we will study a high-level
programming language based on high-level
instructions
• Think of memory as the white board
• From the low-level world, we only need the basic
concept of binary representation

EECE 230X - Introduction to Computation and Programming


Topic 1: Lesson 3

Binary representation

EECE 230X - Introduction to Computation and Programming


Binary representation
• Binary digit (bit) : 0 or 1
• Byte: sequence of 8 bits
• How many possible combinations do we have
using 2 bits? 3 bits? 8 bits? n bits ?

EECE 230X - Introduction to Computation and Programming


Binary representation
Number
0 of bits In general, using n bits, get
1 2 3 4 2𝑛 combinations.
0 0 01 000 Proof by induction:
1 0 10 001 • Base case:
010
1 10 for n = 1, we have 21 =2: ok
0 1 11 011
0 100 • Induction:
101 Assume true for n-1.
110
111
For n, get 2× 2𝑛−1 = 2𝑛

Number of combinations Thus for 8 bits, we have


2 2*2=4 2*4 =8 8*2 = 16
28 = 256 combinations

EECE 230X - Introduction to Computation and Programming


Binary representation of integers
• Base 2 representation of integers
• In base 10, we use digits 0,1,…,9:
0 1 2 3 4 5 6 7 8 9 10 11 12 …
In base 2, we use digits 0 and 1:
0 1 10 11 100 101 110 111 1000 1001 1010 1011 1100 …
• In base 10: 𝑥𝑘 𝑥𝑘−1 … 𝑥0 = 𝑥𝑘 × 10𝑘 + 𝑥𝑘−1 × 10𝑘−1 + … + 𝑥0 × 100
In base 2: 𝑥𝑘 𝑥𝑘−1 … 𝑥0 = 𝑥𝑘 × 2𝑘 + 𝑥𝑘−1 × 2𝑘−1 + … + 𝑥0 × 20
Example: 1100  1× 23 + 1 × 22 + 0× 21 + 0 × 20 = 12
• A variation of this representation is used to allow for negative
integers
EECE 230X - Introduction to Computation and Programming
Topic 1: Lesson 4

Programming languages

EECE 230X - Introduction to Computation and Programming


Programming Languages
Used to implement algorithms
Programming Analogy with
language components English natural
language
primitive constructs Words
Python examples: Examples: I,
3.2, ‘abc’ , + fast, run
3.2 3.2 Run I run
Syntax Which sentences
are well formed?
static semantics Which sentences
<verb> <pronoun> <verb>
(semantic = meaning) are meaningful?
semantics Meaning of
sentences
EECE 230X - Introduction to Computation and Programming
Programming Languages
Used to implement algorithms
Programming Analogy with
language components English natural
language
primitive constructs Words
Python examples: Examples: I,
3.2, ‘abc’ , + fast, run
3.2 3.2 Run I run
Syntax Which sentences
3.2+’abc’ are well formed? I runs fast
static semantics Which sentences
(semantic = meaning) are meaningful?
semantics
<operand> <operator> <operand> Meaning<pronoun>
of <verb> <adverb>
sentences
EECE 230X - Introduction to Computation and Programming
Programming Languages
Used to implement algorithms
Programming Analogy with
language components English natural
language
primitive constructs Words
Python examples: Examples: I,
3.2, ‘abc’ , + fast, run
3.2 3.2 Run I run
Syntax Which sentences
3.2+’abc’ are well formed? I runs fast
static semantics Which sentences
(semantic = meaning) are meaningful?
semantics
<operand> <operator> <operand> Meaning<pronoun>
of <verb> <adverb>
sentences Issue related to meaning
EECE 230X - Introduction to Computation and Programming
Programming Languages
Used to implement algorithms
Programming Analogy with
language components English natural
language
primitive constructs Words
Python examples: Examples: I,
3.2, ‘abc’ , + fast, run
3.2 3.2 Run I run
Syntax Which sentences
3.2+’abc’ are well formed? I runs fast
3.2+3.2 static semantics Which sentences
(semantic = meaning) are meaningful? I run fast
semantics Meaning of
sentences
EECE 230X - Introduction to Computation and Programming
Programming Languages
Used to implement algorithms
Programming Analogy with
language components English natural
language
primitive constructs Words
Python examples: Examples: I,
3.2, ‘abc’ , + fast, run
3.2 3.2 Run I run
Syntax Which sentences
3.2+’abc’ are well formed? I runs fast
3.2+3.2 static semantics Which sentences
(semantic = meaning) are meaningful? I run fast
Only one
semantics Meaning of Multiple
meaning
sentences meanings
allowed
EECE 230X - Introduction to Computation and Programming allowed
Programming Languages
• Examples of high-level languages: Python (this
course), C/C++ (EECE 330), Java (EECE 332)
• Any algorithm that can be implemented in one
programming language can be implemented in any
other (general purpose) programming language

EECE 230X - Introduction to Computation and Programming


Topic 1: Lesson 5

Sample Python code

• Python Syntax
• Static semantics
• Logical errors
• Spyder: recommended Python IDE
• References and useful links

EECE 230X - Introduction to Computation and Programming


Sample python code
from math import pi Colors
# import pi = 3.141592653589793 from the math module Keywords
radius = float(input(“Enter radius:”)) Built in functions
Strings
# input(“Enter radius:”) # Comments
# displays string “Enter radius”
# then reads user input as a string Operators:
# the float() function casts the string into a float = assignment
# and stores it in the variable radius * multiplication
** power
area = pi*(radius**2)
# assign to variable area the value of the expression pi× 𝑟𝑎𝑑𝑖𝑢𝑠 2
print(”Area = ", area)
# displays the string “Area = ” followed by a space and the value of the area

EECE 230X - Introduction to Computation and Programming


Syntax
from math import pi
radius = float(input(“Enter radius:”))
area =
pi*(radius**2)
print(”Area = ", area)

Python interpreter produces a “syntax error” as the


statement “area =“ is syntactically incorrect

EECE 230X - Introduction to Computation and Programming


Syntax
from math import pi
radius = float(input(“Enter radius:”))
area = pi*(radius**2)
print(”Area = ", area)

Python interpreter produces a “syntax error” (more


specifically, “indentation error”) as it is sensitive to
indentation in the beginning of the line

EECE 230X - Introduction to Computation and Programming


Syntax
from math import pi
radius = float(input(“Enter radius:”))
area = pi*(radius 2)
print(”Area = ", area)

What do you get?

EECE 230X - Introduction to Computation and Programming


Syntax
from math import pi
radius = float(input(“Enter radius:”))
area = pi*(radius 2)
print(”Area = ", area)

Python interpreter produces a “syntax error” as


“radius 2” is not syntactically correct (power
operator ** missing)

EECE 230X - Introduction to Computation and Programming


Syntax
from math import pi
radius = float(input(“Enter radius:”)))
area = pi*(radius**2)
print(”Area = ", area)

Python interpreter produces a “syntax error” due the


extra unmatched parenthesis

EECE 230X - Introduction to Computation and Programming


Static semantics
from math import pi
radius = float(input(“Enter radius:”))
area = pi*(radius2)
print(”Area = ", area)

Python interpreter produces a “name error” : name


'radius2' is not defined

EECE 230X - Introduction to Computation and Programming


Static semantics
from math import pi
radius = input(“Enter radius:”)
area = pi*(radius**2)
print(“Area = ", area)

Python interpreter produces a “type error” as raising


a string to a power is not meaningful (we didn’t cast
string into a number)

EECE 230X - Introduction to Computation and Programming


Logical errors
from math import pi
radius = float(input(“Enter radius:”))
area = pi*radius
print(”Area = ", area)

The program will run but it will produce the wrong


output as we didn’t raise radius to the power 2

EECE 230X - Introduction to Computation and Programming


A program is a sequence of instructions
from math import pi
radius = float(input(“Enter radius:”))
area = pi*(radius**2)
print(”Area = ", area)
radius = float(input(“Enter another radius:”))
print(”Area = ", area)

What do you get?

Same area since assignment Operator (=) is not an


equation!
EECE 230X - Introduction to Computation and Programming
A program is a sequence of instructions
from math import pi
radius = float(input(“Enter radius:”))
area = pi*(radius**2)
print(”Area = ", area)
radius = float(input(“Enter another radius:”))
print(”Area = ", area)

What do you get?

Same area since assignment Operator (=) is not an


equation!
EECE 230X - Introduction to Computation and Programming
A program is a sequence of instructions

Need to update area after updating radius:

from math import pi


radius = float(input(‘”Enter radius:”))
area = pi*(radius**2)
print(”Area = ", area)
radius = float(input(“Enter another radius:”))
area = pi*(radius**2)
print(”Area = ", area)

EECE 230X - Introduction to Computation and Programming


Spyder IDE Installation
• IDE: Integrated Development Environment
• Python: most recent version (do not install Python 2)
• Spyder IDE
Spyder: Scientific python development environment
• Anaconda distribution: free and open source distribution
of Python including Spyder
• Installation: https://www.anaconda.com/download/
• Installation handout on Moodle

EECE 230X - Introduction to Computation and Programming


References and useful links

• Python documentations: https://docs.python.org/

• MIT 6.0001 OpenCourseWare: Introduction to Computer


Science and Programming in Python

• Visualizing python code: http://www.pythontutor.com/

EECE 230X - Introduction to Computation and Programming


Introduction to Computation and Programming

POWERED BY ABDULLA AL GHURAIR HUB FOR DIGITAL TEACHING AND LEARNING

This work is licensed under a Creative Commons Attribution-


NonCommercial-ShareAlike 4.0 International License.

You might also like