570 likes | 585 Views
ขั้นตอนวิธีและเครื่องมือในการพัฒนาโปรแกรม. (Algorithm and Program Development Tools) อ.จรรยา สายนุ้ย CS.313 ภาควิชาวิทยาการคอมพิวเตอร์. Notion of Algorithm. problem. algorithm. Program or Software. output. input. Algorithm Definition.
E N D
ขั้นตอนวิธีและเครื่องมือในการพัฒนาโปรแกรมขั้นตอนวิธีและเครื่องมือในการพัฒนาโปรแกรม (Algorithm and Program Development Tools) อ.จรรยา สายนุ้ย CS.313 ภาควิชาวิทยาการคอมพิวเตอร์
Notion of Algorithm problem algorithm Program or Software output input
Algorithm Definition An algorithm is a sequence of unambiguous instructions for solving a problem for any legitimate input in a finite time.
แนวทางการเขียนแสดงขั้นตอนวิธีที่ดีแนวทางการเขียนแสดงขั้นตอนวิธีที่ดี • เขียนแสดงการทำงานเป็นลำดับ • ตามหมายเลข หรือ จากบนลงล่าง • ชัดเจนและกะทัดรัด • เข้าใจได้ง่ายและใช้เวลาเรียนรู้ไม่นาน • แก้ปัญหาและให้คำตอบที่ถูกต้องได้อย่างมีประสิทธิภาพ
Algorithm Representation • ใช้สำหรับอธิบายขั้นตอนวิธีที่ออกแบบ • ใช้ติดต่อสื่อสารกับโปรแกรมเมอร์หรือผู้ใช้ • นำไปใช้เป็นแนวทางในการพัฒนาโปรแกรม • ใช้ตรวจสอบข้อผิดพลาดในการทำงานของโปรแกรมได้ • เป็นส่วนหนึ่งในเอกสารประกอบโปรแกรม
Methods of Specifying an Algorithm • There are 2 options that are most widely used nowadays: • Pseudocode • Flowchart
Pseudocode (รหัสจำลอง) • A pseudocode is a mixture of a natural language and programming language like constructs. • ไม่ขึ้นกับรูปแบบการเขียนของภาษาการโปรแกรมใดๆ • เขียนอธิบายเพื่อให้อ่านได้เข้าใจง่าย • แปลงเป็นโปรแกรมภาษาระดับสูงได้ง่าย • มีข้อมูลเพียงพอสำหรับการแปลขั้นตอนวิธีจากรหัสลำลองไปเป็นโปรแกรมซึ่งเขียนด้วยภาษาการโปรแกรมใดๆ ที่ต้องการได้
ตัวอย่างรหัสจำลอง Algorithm max, min • read number • max number , min number • Loop (not end of file) 2.1 read number 2.2if number > max 2.2.1max number 2.3 if number < min • 2.3.1min number • Print max, min • end Algorithm mean • total 0 , count 0 • Loop (not end of file) 2.1read number 2.2 total total + number 2.3 count count + 1 • average total / count • Print average • end
ตัวอย่างรหัสจำลอง Algorithm mean • read N • total 0 • For count 1 toN do 3.1 read number 3.2 total total + number 3.3 count count + 1 • average total / count • Print average • end Algorithm mean • read N • total 0 , count 0 • while count < N do 2.1 read number 2.2 total total + number 2.3 count count + 1 • average total / count • Print average • end
Flowchart (ผังงาน) • ใช้สัญลักษณ์ที่มีความหมายเฉพาะอย่าง • มีทิศทางการทำงานโดยใช้ลูกศร • แปลเป็นโปรแกรมได้ง่ายเรียงตามลำดับที่แสดง
สัญลักษณ์ที่ใช้ในผังงานสัญลักษณ์ที่ใช้ในผังงาน Start /End Decision Input/Output Process Print Connector
ตัวอย่างผังงาน A Print max Start N not end of file Y read number read number Stop max number N number > max Y A max number
โครงสร้างพื้นฐานการทำงานภายในขั้นตอนวิธีโครงสร้างพื้นฐานการทำงานภายในขั้นตอนวิธี Standard Operations Basic Control Structures
Standard Operations • Declaration • Expression • Assigning Values
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
Expression • consist of operand , operator • types : • arithmetic: a + (b – c) * 4 • relational: x > y , i ≠ 10 • logical:(i < 5) and (j < 10)
ตัวดำเนินการทางคณิตศาสตร์ Arithmetic operator
เครื่องหมายเปรียบเทียบเครื่องหมายเปรียบเทียบ Relational operator
ตัวดำเนินการตรรกะ Logical operator
Assigning Value • Format: • e.g. x 1 j i * i sum sum + a[n] check (m<5) and (n<4) • variable expression
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
Basic Control Structures • Sequence Control • Selection/Decision • Iteration /Repetition
Sequence Control • Assignment stmt. • Input/Output stmt.
Sequence Control • Assignment stmt. • e.g. j i+k*2 name “hawaii” i 0
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, …
Selection/Decision • simple (if-then-else) • nested if • multiple (switch-case)
Simple Selection • if-then • if-then-else • sequence of if • if-then • if-then (-else)
Simple Selection • if-then • e.g. • if-then-else • e.g.
Simple Selection • sequence of if-then • e.g.
Simple Selection • sequence of if-then (-else) • e.g.
Nested Selection stmt. • if-then (if-then) • if-then-else (if-then-else) • if-then (if-then-else) -else (if-then-else)
Nested Selection stmt. รูปแบบ: if-then (if-then) ตัวอย่าง if (เงื่อนไข) then if (เงื่อนไข) then if (เงื่อนไข) then ….. end if end if end if
Nested Selection stmt. if-then-else (if-then-else) ตัวอย่าง if (เงื่อนไข) then …. Else if (เงื่อนไข) then … [else] …. end if end if
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
Multiple Selection stmt. case/switch case ans of 1 : x n * n 2 : x n * m 3 : x n * n * m 4 : x n * n * n else write “input error !” end case
Iteration /Repetition (Loop) • for • while-do • repeat-until (do-while)
Iteration /Repetition (Loop) for statement • e.g. 1.for i0 to 10 1.1 read num 1.2 sumsum+num 2.write “sum =”,sum Requirement specification คือ ? <ขั้นตอนวิธีนี้แก้ปัญหาอะไร?>
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
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
Exercise1 • เขียนขั้นตอนวิธีเพื่อหาค่า sum square (n) • แบบที่ 1 = 12 + 22 + 32 + … + n2 • แบบที่ 2 = {n (n+1)(2n+1)}/6
Answer แบบที่ 1 = 12 + 22 + 32 + … + n2 Algorithm sumsqr(n) • read n • sumsq 0 • for i 1 to n 3.1 sumsq sumsq + i * i 4. display “sumsquare of ”,n,“=”,sumsq 5. end
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
Exercise2 • เขียนโปรแกรมเพื่อคำนวณหาค่า BMI = น้ำหนัก(กิโลกรัม) / (ส่วนสูง(เมตร))2 ของกลุ่มตัวอย่างจำนวน 10 คน และสรุปตามเงื่อนไขดังนี้ ค่าBMIข้อสรุป >= 40 อ้วนเกินไป >= 30 แต่ไม่เกิน 40 อ้วน >= 25แต่ไม่เกิน30 ท้วม >= 18.5แต่ไม่เกิน25 เหมาะสม < 18.5 ผอมเกินไป 344-211 Algorithmic Process and Programming , created by DararatSaelee
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
Exercise3 • เขียนขั้นตอนวิธีด้วยรหัสลำลองเพื่อคำนวณหาค่า s จากสูตร s = a + ar + ar2 + ... + arn โดยการอ่านค่า a, r และ n เข้ามาทางแป้นพิมพ์ 344-211 Algorithmic Process and Programming , created by Dararat Saelee
การออกแบบข้อมูลสำหรับใช้ในการเขียนขั้นตอนวิธีการออกแบบข้อมูลสำหรับใช้ในการเขียนขั้นตอนวิธี Input/Output Data Constant/Variable Data
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)
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
Data Design Exercise • การแปลงค่าองศาเซลเซียสเป็นค่าองศาฟาเรนไฮต์ • ค่าองศาฟาเรนไฮต์ f = (9 /5)* c + 32 • ค่าคงที่ • ตัวแปร • ข้อมูลเข้า • ข้อมูลผลลัพธ์
เครื่องมือในการพัฒนาโปรแกรมเครื่องมือในการพัฒนาโปรแกรม Programming Environment Program Development Tools