Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

B11 01 Introduction to Programming

Uploaded by

nthang0987
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

B11 01 Introduction to Programming

Uploaded by

nthang0987
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 52

Introduction to Programming

Lê Ngọc Hiếu
lnhieu@hcmute.edu.vn
1. Definition of Algorithms

2. Algorithm Properties

Lecture 3. Building blocks of


algorithms
Contents 4. Represent an Algorithm

5. Install Python Interpreter

6. Python IDE
2
ALGORITHMS

3
Fact: computers are dumb machines
• Basic property of a computer (a machine):

• Computers do what we tell them to do.


• Unfortunately, computer do not necessarily do
what we want them to do.
 Because we can make mistakes in telling the
computer what we want to do
 These mistakes are called “bugs”.

4
Definition of Algorithms

Algorithm = a step-by-step
procedure for solving a problem or
accomplishing some task,
especially by means of a computer

5
Algorithm Properties
• Input: Must come from a specified set of elements, amount and type of inputs
are specified.
• Output: The algorithm must specify the output and how it is related to the
input.
• Unambiguous: The steps in the algorithm must be clearly defined and detailed.
• Finiteness: The algorithm must terminate after a specific number of steps.
• Effectiveness: It must be possible to perform each step of algorithm correctly
and in a finite amount of time.
• Generality: The algorithm can apply to a class of problems.
• Feasible: The algorithm must be simple, generic and practical, such that it can
be executed upon will the available resources.
• Language Independent: The Algorithm designed must be language-
independent, i.e. it can be implemented in any language, and yet the output
will be same, as expected. 6
Problem definition
Specification of an algorithm

Problem Designing an algorithm


Developmen Analysis of an algorithm
t Steps Implementation of an
algorithm
Program testing
Documentation
7
Building blocks of algorithms
• An algorithm is made up of three basic building blocks:
sequencing, selection, and iteration.
1.Compute c = a
1.a = 1
+b 1.If (a > b) then
2.Loop until a >
2.Compute d = a max = a
10
–b 2.Otherwise, max =
2.1. a = a * 2
3.Compute r = b
2.2. Output a
c/d
Sequencing Selection Iteratio
n

8
Represent an Algorithm
Algorithm: Find root of
Input:
Output: Root of
Begin
If (a = 0) then
If (b = 0) then
print “Infinite
solutions”
Else
print “No solution”
End If
Else
x
print
End If
End
Natural
Flowchart Pseudoco
langua
s de
ge
9
Input:
Output: Root of Equation
1. Input 2 real numbers: a and b
2. If a = 0 then
Represent an 2.1. If b = 0 then
Algorithm 2.1.1. Infinite solutions
2.1.2. Stop.
using Natural 2.2. Otherwise,
2.2.1. No solution
Language 2.2.2. Stop.
3. Otherwise,
3.1. One solution:
3.2. Stop

10
Using Natural Language – Example 1
Input:
Output: Roots of Equation

11
Using Natural Language – Example 1
Input:
Output: Roots of Equation
1. Input a, b, c
2. If a = 0 then
2.1. If b = 0 then
2.1.1. If c = 0, then output “infinite solutions” and stop.
2.1.2. Otherwise, output “no solution” and stop.
2.2. Otherwise, output one solution and stop.
3. Otherwise,
3.1. Compute
3.2. If d < 0, then output “no solution” and stop.
3.3. Otherwise,
3.3.1. If d = 0, output one solution and stop.
3.3.2. Otherwise, output two solutions and stop.

12
Using Natural Language – Example 2
• Compute perimeter and area of a rectangle given the length
and width of the rectangle
Input:
Output: Perimeter and area of the rectangle

13
Using Natural Language – Example 2
• Compute perimeter and area of a rectangle given the length
and width of the rectangle
Input:
Output: Perimeter and area of the rectangle
1. Input a and b
2. If (a>0) and (b>0) then
2.1. Compute S = a*b
2.2. Compute C = (a + b) * 2
2.3. Output S, C.
2.4. Stop.
3. Otherwise, output “Cannot compute the perimeter and
area” and stop.
14
Using Natural Language – Example 3
• Find greatest common divisor (GCD) of two positive integers.
• Example of finding GCD:
• Given a = 15 and b = 25.
• 1st way:
• Since b>a  b = b – a = 25 – 15 = 10
• Since a>b  a = a – b = 15 – 10 = 5
• Since b>a  b = b – a = 10 – 5 = 5
• Since b=a  Stop. GCD of a and b is the value of b, i.e. 5
• 2nd way:
• Since a < b, swap a and b  a = 25 and b = 15
• r = a mod b = 25 mod 15 = 10
• Since r ≠ 0  a = b = 15, b = r = 10, r = a mod b = 15 mod 10 = 5
• Since r ≠ 0  a = b = 10, b = r = 5, r = a mod b = 10 mod 5 = 0
• Since r = 0  Stop. GCD of a and b is b, i.e. 5
15
16
Using Natural Language – Example 3
• Find greatest common divisor (GCD) of two positive integers.

17
Using Natural Language – Example 3
• Find greatest common divisor (GCD) of two positive integers.
Input: Input:
Output: The GCD of a and Output: The GCD of a and b
b 1. Input a and b
1. Input a and b 2. If (a < b) then swap a and b
2. If (a >0) and (b>0) then 3. If (a >0) and (b>0) then
2.1. r = a mod b
2.1. Loop until (a = b)
2.2. Loop until (r = 0)
2.1.1. If (a > b) then a
2.2.1. a = b
=a-b 2.2.2. b = r
2.1.2. Otherwise, b = 2.2.3. r = a mod b
b-a 2.2. Output b and stop.
2.2. Output b and stop. 3. Otherwise, output NULL and
3. Otherwise, output NULL stop. 18
Represent an Algorithm
Begin using Flowchart
Input: a, b

Yes Yes
a=0 b=0
No No
Output: x = - Infinite
No solution
b/a solutions

End
19
Begin
Conditio No
End n
Yes
Input
A
Flowchar Output
t
Symbols A A

Conditio No Yes Conditi


n on
Yes
No
20
Using Flowchart – Example 1
• Compute perimeter and area of a rectangle given the length
and width of the rectangle

21
Using Flowchart – Example 1
• Compute perimeter and area of a rectangle given the length
and width of the rectangle
Begi
n

Input: a, b

Yes S=a*b
a>0&b Output: S, C
C = (a + b)
>0
*2
No
Output: Invalid
inputs End

22
Using Flowchart– Example 2
• Find the solutions of the equation
• For simplicity, let assume that a ≠ 0.

23
24
25
Begi
n

Input: a, b, c

d = bb – 4 a c

d< No d= No
0 0
Yes Yes
Output: No
solution

Output: Output:

End
26
Begi
n More complete
solution
Input: a,
b, c

a= No
d = bb – 4 a c
0
Yes
b= Yes c= Yes d< No d= No
0 0 0 0
No No Yes Yes
Infinite Output:
Output: No
solution No
x = -c/b solution
s solution

Output: Output:
End

27
Using Flowchart – Example 3
• Find greatest common divisor (GCD) of two positive integers.
Input:
Output: The GCD of a and
b
1. Input a and b
2. If (a >0) and (b>0) then
2.1. Loop until (a = b)
2.1.1. If (a > b) then a
=a-b
2.1.2. Otherwise, b =
b-a
2.2. Output b and stop.
3. Otherwise, output NULL 28
Using Flowchart – Begi
n
Example 3 Input: a,
b
• Find greatest
Output:
common divisor a>0 & No
Invalid
(GCD) of two positive b>0 inputs
integers. Yes End
No
a≠b Output: b
Yes
No b = b -
a>b
a
Yes
a=a-
b

29
EXERCISES
1. Find the addition, subtraction, multiplication, and division
of two integers.
2. Compute the perimeter and area of a circle, given its
radius.
3. Check if a year is a leap year. Leap years satisfy one of
two conditions: (1) The year is multiple of 400. (2) The
year is multiple of 4 and not multiple of 100.
4. Compute the surface area and volume of a sphere, given
its radius (Area = , Volume =
5. Given 3 positive integers. Check if they are side lengths of
a triangle. If yes, compute the triangle perimeter and area.
6. Swap the values of two numbers.
31
7. Input a list of numbers. Count the number of positive
elements.
8. Input a list of numbers. Count the number of even elements.
9. Find the sum of all elements in a given array.
10.Find the sum of all even elements in a given array.
11.Find the multiplication of all elements in a given array.
12.Find the multiplication of all even elements in a given array.
13.Find the maximum value in a given array.
14.Find the minimum value in a given array.
15.Sort an array in ascending order.

32
33
PYTHON PROGRAMMING
LANGUAGE
Definition of programming languages
• Programming language = A vocabulary and set of grammatical
rules (syntax) for instructing a computer to perform specific
tasks.
• Programmers (developers) use programming languages to
communicate with computers.
High-level
language
• Three types: Complier Complier
• Machine language
• Assembly language Assembly
• High-level language language
Assembler

Machine
language
35
What is Python?
• Python is an interpreted, object-oriented, high-level programming
language with dynamic semantics.
• Its high-level built-in data structures, combined with dynamic
typing and dynamic binding, make it very attractive for Rapid
Application Development, as well as for use as a scripting or glue
language to connect existing components together.
• Python's simple, easy to learn syntax emphasizes readability and
therefore reduces the cost of program maintenance.
• Python supports modules and packages, which encourages
program modularity and code reuse.
• The Python interpreter and the extensive standard library are
available in source or binary form without charge for all major
36
Install Python Interpreter on Your
Computer
1st way: Download a
suitable version from
https://www.python.org/
downloads/

2st way (recommended):


Download a suitable
version from
https://www.anaconda.c
om/products/individual 37
If you want to use the first way, see your textbook
for the installation guide.
38
Install
Anaconda

1 2 3

Memorize the installation


folder

4 5 6
7 8

40
Verify if Anaconda is successfully
installed

Open Anaconda After opening Anaconda Prompt, enter conda list.


Prompt If Anaconda is installed and working, this will
(Windows: Click Start, display a list of installed packages and their
search, or select versions.
41
Execute Python Commands in Shell
1. Executes a single statement 2. Execute multiple statements
a) Open Anaconda Prompt (or Python (Python scripts)
shell if you install Python interpreter a) Create a *.py file
using the first way).
b) Open Anaconda Prompt (or Python
b) Start Python by typing python and shell)
press Enter.
c) Run the script by typing python
c) Enter a single statement and get the script_file_name.py
result.

42
Python IDE - Jupyter Notebook in
Anaconda
• Open Jupyter Notebook by typing jupyter notebook in Anaconda
Prompt.

43
44
1. Create a new Jupyter 2. Write a program using
Notebook with Python Jupyter Notebook
45
Python IDE - Pycharm
• Download and Install Pycharm from
https://www.jetbrains.com/pycharm/download/#section=windo
ws

46
Install Pycharm

47
Programing with Pycharm and Anaconda
1. Open Pycharm, accept the terms of User Agreement, then press Continue.
2. Choose New Project

48
Programing with Pycharm and Anaconda (continue)
3. Select location to store your project.
4. Select interpreter (see the video in the next slide for the details)

49
50
Write a program using Pycharm
51
Question: How to check
Python version?

52

You might also like