ขั้นตอนวิธีและเครื่องมือในการพัฒนาโปรแกรม
Download
Report
Transcript ขั้นตอนวิธีและเครื่องมือในการพัฒนาโปรแกรม
(Algorithm and Program Development Tools)
อ.จรรยา สายนุ้ย
CS.313 ภาควิชาวิทยาการคอมพิวเตอร์
1
Notion of Algorithm
problem
algorithm
input
Program
or
Software
output
2
Algorithm Definition
An algorithm is a sequence of
unambiguous instructions for
solving a problem for any
legitimate input in a finite
time.
3
แนวทางการเขียนแสดงขัน้ ตอนวิธีท่ ดี ี
เขียนแสดงการทางานเป็ นลาดับ
ตามหมายเลข หรือ จากบนลงล่าง
ชัดเจนและกะทัดรัด
เข้าใจได้งา่ ยและใช้เวลาเรียนรูไ้ ม่นาน
แก้ปญั หาและให้คาตอบทีถ่ กู ต้องได้อย่างมีประสิทธิภาพ
4
Algorithm Representation
ใช้สาหรับอธิบายขัน้ ตอนวิธที อ่ี อกแบบ
ใช้ตดิ ต่อสือ่ สารกับโปรแกรมเมอร์หรือผูใ้ ช้
นาไปใช้เป็ นแนวทางในการพัฒนาโปรแกรม
ใช้ตรวจสอบข้อผิดพลาดในการทางานของโปรแกรมได้
เป็ นส่วนหนึ่งในเอกสารประกอบโปรแกรม
5
Methods of Specifying an
Algorithm
There are 2 options that are most
widely used nowadays:
Pseudocode
Flowchart
6
Pseudocode (รหัสจาลอง)
A pseudocode is a mixture of a natural
language and programming language like
constructs.
- ไม่ขน้ึ กับรูปแบบการเขียนของภาษาการโปรแกรมใดๆ
- เขียนอธิบายเพือ่ ให้อ่านได้เข้าใจง่าย
- แปลงเป็ นโปรแกรมภาษาระดับสูงได้งา่ ย
- มีขอ้ มูลเพียงพอสาหรับการแปลขัน้ ตอนวิธจี ากรหัสลาลองไปเป็ น
โปรแกรมซึง่ เขียนด้วยภาษาการโปรแกรมใดๆ ทีต่ อ้ งการได้
7
ตัวอย่ างรหัสจาลอง
Algorithm mean
1. total 0 , count 0
2. Loop (not end of file)
2.1 read number
2.2 total total + number
2.3 count count + 1
3. average total / count
4. Print average
5. end
Algorithm max, min
1. read number
2. max number , min number
3. Loop (not end of file)
2.1 read number
2.2 if number > max
2.2.1 max number
2.3 if number < min
2.3.1 min number
4. Print max, min
5. end
8
ตัวอย่ างรหัสจาลอง
Algorithm mean
1.
2.
3.
read N
total 0 , count 0
while count < N do
Algorithm mean
1.
2.
3.
3.1 read number
3.2 total total + number
3.3 count count + 1
2.1 read number
2.2 total total + number
2.3 count count + 1
4.
5.
6.
average total / count
Print average
end
read N
total 0
For count 1 to N do
4.
5.
6.
average total / count
Print average
end
9
Flowchart (ผังงาน)
ใช้สญ
ั ลักษณ์ทม่ี คี วามหมายเฉพาะอย่าง
มีทศิ ทางการทางานโดยใช้ลกู ศร
แปลเป็ นโปรแกรมได้งา่ ยเรียงตามลาดับทีแ่ สดง
10
สัญลักษณ์ ท่ ใี ช้ ในผังงาน
Start /End
Input/Output
Decision
Process
Print
Connector
11
ตัวอย่ างผังงาน
A
Start
not end of file
N
Print max
Y
read number
read number
Stop
max number
A
N
number > max
Y
max number
12
Standard Operations
Basic Control Structures
13
Standard Operations
Declaration
Expression
Assigning Values
14
Declaration
module header : main, submodule
e.g. Algorithm Euclid() ,
Module Sum(), Subprogram Mean()
data : input/output
e.g. Input: integer n , character d ,
array A with integer 30 elements
Output: summation of n elements
15
Expression
• consist of operand , operator
• types :
• arithmetic: a + (b – c) * 4
• relational: x > y , i ≠ 10
• logical: (i < 5) and (j < 10)
16
ตัวดาเนินการทางคณิตศาสตร์
Arithmetic operator
17
เครื่ องหมายเปรียบเทียบ
Relational operator
18
ตัวดาเนินการตรรกะ
Logical operator
19
Assigning Value
Format:
variable expression
e.g.
x1
ji*i
sum sum + a[n]
check (m<5) and (n<4)
20
Example
Algorithm degree_conversion
Input : a number as degree Celcius
Output : an equivalent degree Fahrenheit
1. read celcius
2. fahrenheit 9/5*celcius+32
3. write celcius , fahrenheit
4. end
21
Basic Control Structures
Sequence Control
Selection/Decision
Iteration /Repetition
22
Sequence Control
Assignment stmt.
Input/Output stmt.
23
Sequence Control
Assignment stmt.
e.g.
j i+k*2
name “hawaii”
i 0
24
Sequence Control
Input/Output stmt.
e.g.
open file fn1, fn2
close file fn1
read var1, var2, … from fn1
write var1, var2, … to fn2
read var1, var2, …
write var1,var2, …
25
Selection/Decision
simple (if-then-else)
nested if
multiple (switch-case)
26
Simple Selection
if-then
if-then-else
sequence of if
if-then
if-then (-else)
27
Simple Selection
if-then
e.g.
if-then-else
e.g.
28
Simple Selection
sequence of if-then
e.g.
29
Simple Selection
sequence of if-then (-else)
e.g.
30
Nested Selection stmt.
if-then (if-then)
if-then-else (if-then-else)
if-then (if-then-else)
-else (if-then-else)
31
Nested Selection stmt.
รูปแบบ: if-then (if-then)
ตัวอย่าง
if (เงือ่ นไข) then
if (เงือ่ นไข) then
if (เงือ่ นไข) then
…..
end if
end if
end if
32
Nested Selection stmt.
if-then-else (if-then-else)
ตัวอย่าง
if (เงือ่ นไข) then
….
Else
if (เงือ่ นไข) then
…
[else]
….
end if
end if
33
Nested Selection stmt.
if-then (if-then-else)
-else (if-then-else)
ตัวอย่าง
if (เงือ่ นไข) then
if (เงือ่ นไข) then
….
[else]
….
end if
Else
if (เงือ่ นไข) then
…
[else]
….
end if
end if
34
Multiple Selection stmt.
case/switch
case ans of
1:xn*n
2:xn*m
3:xn*n*m
4:xn*n*n
else write “input error !”
end case
35
Iteration /Repetition (Loop)
for
while-do
repeat-until (do-while)
36
Iteration /Repetition (Loop)
for statement
e.g.
1.for i0 to 10
1.1 read num
Requirement specification คือ ?
<ขัน้ ตอนวิธนี ้แี ก้ปญั หาอะไร?>
1.2 sumsum+num
2.write “sum =”,sum
37
Iteration /Repetition (Loop)
while-do statement
e.g.
1.i 1 , sum 0
2.while (i<10) do
2.1 read num
2.2 sum sum + num
2.3 i i + 1
3.write “sum =”,sum
38
Iteration /Repetition (Loop)
repeat-until (do-while) statement
e.g.
1.i 1 , sum 0
2.repeat
2.1 read num
2.2 sum sum + num
2.3 i i + 1
until (i>10)
3.write “sum =”,sum
39
Exercise1
เขียนขัน้ ตอนวิธเี พือ่ หาค่า sum square (n)
แบบที่ 1 = 12 + 22 + 32 + … + n2
แบบที่ 2 = {n (n+1)(2n+1)}/6
40
Answer
แบบที่ 1 = 12 + 22 + 32 + … + n2
Algorithm sumsqr(n)
1. read n
2. sumsq 0
3. for i 1 to n
3.1 sumsq sumsq + i * i
4. display “sumsquare of ”,n,“=”,sumsq
5. end
41
Answer
แบบที่ 2 = {n (n+1)(2n+1)}/6
Algorithm sumsqr(n)
1. read n
2. sumsq n * (n+1) * (2n+1) / 6
3. display “sumsquare of ”,n,“=”,sumsq
4. end
42
Exercise2
เขียนโปรแกรมเพือ่ คานวณหาค่า
BMI = น้าหนัก(กิโลกรัม) / (ส่วนสูง(เมตร))2
จานวน 10 คน และสรุปตามเงือ่ นไขดังนี้
ค่า BMI
ข้อสรุป
>= 40
อ้วนเกินไป
>= 30 แต่ไม่เกิน 40
อ้วน
>= 25 แต่ไม่เกิน 30
ท้วม
>= 18.5 แต่ไม่เกิน 25
เหมาะสม
< 18.5
ผอมเกินไป
344-211 Algorithmic Process and Programming ,
created by Dararat Saelee
ของกลุ่มตัวอย่าง
43
Answer
Algorithm BMI
1. for i 1 to 10
1.1 read w,h
1.2 bmi w / (h * h)
1.3 display “BMI = ”, bmi
1.4 if (bmi >= 40) then display (“too fat”)
else if (bmi >= 30) then display (“fat”)
else if (bmi >= 25) then display (“quite fat”)
else if (bmi >=18.5) then display (“slim”)
else display (“too slim”)
2. end
344-211 Algorithmic Process and Programming ,
created by Dararat Saelee
44
Exercise3
เขียนขัน้ ตอนวิธดี ว้ ยรหัสลาลองเพือ่ คานวณหาค่า s จากสูตร
s = a + ar + ar2 + ... + arn
โดยการอ่านค่า a, r และ n เข้ามาทางแป้นพิมพ์
344-211 Algorithmic Process and Programming ,
created by Dararat Saelee
45
Input/Output Data
Constant/Variable Data
46
Input/Output Data
Input: data to be known before/while
processing a program
Output: result from program execution
Types: decide appropriate type of input
/output data ; e.g. int , longint, char, float ,
double
Structure: relationship of each data : struct
or record, series of data set (array)
47
Constant/Variable Data
Constant: its value is fixed before running
until stop running
e.g. 50, ‘y’ , “male”
Variable: its value can be changed during
the execution
e.g. x = y + z
48
Data Design Exercise
การแปลงค่าองศาเซลเซียสเป็ นค่าองศาฟาเรนไฮต์
ค่าองศาฟาเรนไฮต์ f = (9 /5)* c + 32
ค่าคงที่
ตัวแปร
ข้อมูลเข้า
ข้อมูลผลลัพธ์
49
Programming Environment
Program Development Tools
50
S/W Development Environments
set of tools and techniques provided for
users to develop programs in easier way
and more convenient use
e.g. compiler + editor + debugger + file
system + GUI or windowed interface
51
Programming Environment
Integrated Development Environment
(IDE) :
Editor
Compiler
Preprocessor
Libraries
debugging tools
and other utilities
e.g. Turbo C, C-Free, DevC
52
Programming Environment
Development kits :
compiler, libraries
e.g. JDK/JRE
Tools :
only editor
e.g. Notepad
editor & tools e.g. EditPlus
53
Program Development Tools
Editor
Libraries
Preprocessor
Compiler
Debugger
Utilities
54
Program Development Tools
Editors : Notepad , Turbo C/C++ editor ,
EditorPlus , EClipse
Libraries :
Header files (C - #include)
Packages (Java - import)
55
Program Development Tools
Preprocessor :
C - #define
Compilers :
C – gcc , cc , Turbo C/C++
Java – javac , java
56
Program Development Tools
Debuggers :
Turbo C/C++ trace , watch
DOS debug
Utilities :
C - online help
Java – help message
57