Professional Documents
Culture Documents
Monitor
Houses processor,
and
memory, buses, etc.
speaker
(output)
Set of instructions
Keyboard and to perform tasks to
mouse (input)
specifications
Programs are
software
http://www.tutorialspoint.com/computer_fundamentals/computer_quick_guide.htm
5
(Computer) Program
Sequence of instructions for a computer to
execute
Programming languages
Languages for writing programs
6
Use a compiler
to translate the
Use an editor to
source code into
create/modify
the source code Edit Compile executable
Process is iterative
9
int main(void) {
int a=27, b=6, c;
c = a%b;
printf("The value of c is %d.\n", c);
return 0;
}
10
Executable code
Compile produces
first.exe
eg: gcc first.c –o
%TEMP%\first.exe
Program output
Execute produces
eg: %TEMP%\first.exe The value of c is 3.
11
• Used by:
• Google, Yahoo!, Youtube
• Many Linux distributions
• Games and apps (e.g. Eve Online)
16
Windows: Mac OS X:
• Download Python from • Python is already installed.
http://www.python.org • Open a terminal and run python or
• Install Python. run Idle from Finder.
• Run Idle from the Start Menu.
Linux:
• Chances are you already have Python
installed. To check, run python
from the terminal.
• If not, install from your distribution's
package system.
Note: For step by step installation instructions,
see the course web site.
17
hello.py
1 print("Hello, world!")
19
swallows.py
1 print("Hello, world!")
2 print()
3 print("Suppose two swallows \"carry\" it
4 together.")
print('African or "European" swallows?')
20
Ghi chú
• Syntax:
# comment text (one line)
swallows2.py
1 # Suzy Student, CSE 142, Fall 2097
2 # This program prints important messages.
3 print("Hello, world!")
4 print() # blank line
5 print("Suppose two swallows \"carry\" it
6 together.")
print('African or "European" swallows?')
21
hello3.py
1 # Prints a helpful message.
2 def hello():
3 print("Hello, world!")
4 print("How are you?")
5
6 # main (calls hello twice)
7 hello()
8 hello()
23
Bài tập
• Rewrite the Figures lecture program in Python. Its output:
______
/ \
/ \
\ /
\______/
\ /
\______/
+--------+
______
/ \
/ \
| STOP |
\ /
\______/
______
/ \
/ \
+--------+
24
Bài giải
Algorithmic Problem
Solving
26
Write algorithm
Iterative
Design process
Produce
code Implementation What is an
algorithm?
Check for
correctness and Testing
efficiency
28
Algorithm (1)
An algorithm is a well-defined computational
procedure consisting of a set of instructions,
that takes some value or set of values as input,
and produces some value or set of values as
output.
Algorithm (2)
An algorithm has these properties:
The algorithm
Each step must
must
be exact. (Or it will
not be precise.) terminate.
Exact Terminate (Or no solution will be
obtained.)
The
algorithm The algorithm
must be must be
Effective General
effective. general.
(i.e. it must solve the (Within the constraints of
problem.) the system/language.)
30
Algorithm (3)
Ways of representing an algorithm:
Flowchart Pseudocode
lynda.com
31
Algorithm: Example #1
32
Yes end of
input?
No
Enter num
increment count
ave sum/count sum sum + num
No
end
33
Algorithm: Pseudocode
We will write algorithms in pseudocode instead of
flowchart as the former is more succinct
However, there are no standard rules on how
pseudocodes should look like
General guidelines:
Every step must be unambiguous, so that anybody is
able to hand trace the pseudocode and follow the
logic flow
Use a combination of English (keep it succinct) and
commonly understood notations (such as for
assignment in our previous example)
35
Sequence • Default
True False
?
• Also called
Selection branching
False
Repetition • Also called loop
?
True
37
Data Representation
Internal representation: bits (binary digits) 0 and 1
1 byte = 8 bits
In programming, we need variables to hold data. A
variable has an associated data type and occupies
memory space. In the following slides, variables are
shown as boxes.
Some data types in Python
Integers: int, short, long (int is most common)
Real numbers: float, double
Characters: char, string
38
num1 = final1
num2 = final2
// Display answer
print ans ans
Initialisation is
very important!
45
print ans
Output: 6
46
print("ans = %d"%(ans))
47
r could be obtained by A
modulo B (i.e. remainder of
A / B)
Result: 6
50
Bài tập
• Sinh viên cài đặt thuật toán Euclid
51
CÁM ƠN ĐÃ LẮNG
NGHE!