1 / 57

ขั้นตอนวิธีและเครื่องมือในการพัฒนาโปรแกรม

ขั้นตอนวิธีและเครื่องมือในการพัฒนาโปรแกรม. (Algorithm and Program Development Tools) อ.จรรยา สายนุ้ย CS.313 ภาควิชาวิทยาการคอมพิวเตอร์. Notion of Algorithm. problem. algorithm. Program or Software. output. input. Algorithm Definition.

sarak
Télécharger la présentation

ขั้นตอนวิธีและเครื่องมือในการพัฒนาโปรแกรม

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ขั้นตอนวิธีและเครื่องมือในการพัฒนาโปรแกรมขั้นตอนวิธีและเครื่องมือในการพัฒนาโปรแกรม (Algorithm and Program Development Tools) อ.จรรยา สายนุ้ย CS.313 ภาควิชาวิทยาการคอมพิวเตอร์

  2. Notion of Algorithm problem algorithm Program or Software output input

  3. Algorithm Definition An algorithm is a sequence of unambiguous instructions for solving a problem for any legitimate input in a finite time.

  4. แนวทางการเขียนแสดงขั้นตอนวิธีที่ดีแนวทางการเขียนแสดงขั้นตอนวิธีที่ดี • เขียนแสดงการทำงานเป็นลำดับ • ตามหมายเลข หรือ จากบนลงล่าง • ชัดเจนและกะทัดรัด • เข้าใจได้ง่ายและใช้เวลาเรียนรู้ไม่นาน • แก้ปัญหาและให้คำตอบที่ถูกต้องได้อย่างมีประสิทธิภาพ

  5. Algorithm Representation • ใช้สำหรับอธิบายขั้นตอนวิธีที่ออกแบบ • ใช้ติดต่อสื่อสารกับโปรแกรมเมอร์หรือผู้ใช้ • นำไปใช้เป็นแนวทางในการพัฒนาโปรแกรม • ใช้ตรวจสอบข้อผิดพลาดในการทำงานของโปรแกรมได้ • เป็นส่วนหนึ่งในเอกสารประกอบโปรแกรม

  6. Methods of Specifying an Algorithm • There are 2 options that are most widely used nowadays: • Pseudocode • Flowchart

  7. Pseudocode (รหัสจำลอง) • A pseudocode is a mixture of a natural language and programming language like constructs. • ไม่ขึ้นกับรูปแบบการเขียนของภาษาการโปรแกรมใดๆ • เขียนอธิบายเพื่อให้อ่านได้เข้าใจง่าย • แปลงเป็นโปรแกรมภาษาระดับสูงได้ง่าย • มีข้อมูลเพียงพอสำหรับการแปลขั้นตอนวิธีจากรหัสลำลองไปเป็นโปรแกรมซึ่งเขียนด้วยภาษาการโปรแกรมใดๆ ที่ต้องการได้

  8. ตัวอย่างรหัสจำลอง 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

  9. ตัวอย่างรหัสจำลอง 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

  10. Flowchart (ผังงาน) • ใช้สัญลักษณ์ที่มีความหมายเฉพาะอย่าง • มีทิศทางการทำงานโดยใช้ลูกศร • แปลเป็นโปรแกรมได้ง่ายเรียงตามลำดับที่แสดง

  11. สัญลักษณ์ที่ใช้ในผังงานสัญลักษณ์ที่ใช้ในผังงาน Start /End Decision Input/Output Process Print Connector

  12. ตัวอย่างผังงาน A Print max Start N not end of file Y read number read number Stop max number N number > max Y A max number

  13. โครงสร้างพื้นฐานการทำงานภายในขั้นตอนวิธีโครงสร้างพื้นฐานการทำงานภายในขั้นตอนวิธี Standard Operations Basic Control Structures

  14. Standard Operations • Declaration • Expression • Assigning Values

  15. 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

  16. Expression • consist of operand , operator • types : • arithmetic: a + (b – c) * 4 • relational: x > y , i ≠ 10 • logical:(i < 5) and (j < 10)

  17. ตัวดำเนินการทางคณิตศาสตร์ Arithmetic operator

  18. เครื่องหมายเปรียบเทียบเครื่องหมายเปรียบเทียบ Relational operator

  19. ตัวดำเนินการตรรกะ Logical operator

  20. Assigning Value • Format: • e.g. x  1 j i * i sum  sum + a[n] check  (m<5) and (n<4) • variable  expression

  21. 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

  22. Basic Control Structures • Sequence Control • Selection/Decision • Iteration /Repetition

  23. Sequence Control • Assignment stmt. • Input/Output stmt.

  24. Sequence Control • Assignment stmt. • e.g. j  i+k*2 name  “hawaii” i  0

  25. 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, …

  26. Selection/Decision • simple (if-then-else) • nested if • multiple (switch-case)

  27. Simple Selection • if-then • if-then-else • sequence of if • if-then • if-then (-else)

  28. Simple Selection • if-then • e.g. • if-then-else • e.g.

  29. Simple Selection • sequence of if-then • e.g.

  30. Simple Selection • sequence of if-then (-else) • e.g.

  31. Nested Selection stmt. • if-then (if-then) • if-then-else (if-then-else) • if-then (if-then-else) -else (if-then-else)

  32. Nested Selection stmt. รูปแบบ: if-then (if-then) ตัวอย่าง if (เงื่อนไข) then if (เงื่อนไข) then if (เงื่อนไข) then ….. end if end if end if

  33. Nested Selection stmt. if-then-else (if-then-else) ตัวอย่าง if (เงื่อนไข) then …. Else if (เงื่อนไข) then … [else] …. end if end if

  34. 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

  35. 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

  36. Iteration /Repetition (Loop) • for • while-do • repeat-until (do-while)

  37. Iteration /Repetition (Loop) for statement • e.g. 1.for i0 to 10 1.1 read num 1.2 sumsum+num 2.write “sum =”,sum Requirement specification คือ ? <ขั้นตอนวิธีนี้แก้ปัญหาอะไร?>

  38. 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

  39. 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

  40. Exercise1 • เขียนขั้นตอนวิธีเพื่อหาค่า sum square (n) • แบบที่ 1 = 12 + 22 + 32 + … + n2 • แบบที่ 2 = {n (n+1)(2n+1)}/6

  41. 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

  42. 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

  43. Exercise2 • เขียนโปรแกรมเพื่อคำนวณหาค่า BMI = น้ำหนัก(กิโลกรัม) / (ส่วนสูง(เมตร))2 ของกลุ่มตัวอย่างจำนวน 10 คน และสรุปตามเงื่อนไขดังนี้ ค่าBMIข้อสรุป >= 40 อ้วนเกินไป >= 30 แต่ไม่เกิน 40 อ้วน >= 25แต่ไม่เกิน30 ท้วม >= 18.5แต่ไม่เกิน25 เหมาะสม < 18.5 ผอมเกินไป 344-211 Algorithmic Process and Programming , created by DararatSaelee

  44. 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

  45. Exercise3 • เขียนขั้นตอนวิธีด้วยรหัสลำลองเพื่อคำนวณหาค่า s จากสูตร s = a + ar + ar2 + ... + arn โดยการอ่านค่า a, r และ n เข้ามาทางแป้นพิมพ์ 344-211 Algorithmic Process and Programming , created by Dararat Saelee

  46. การออกแบบข้อมูลสำหรับใช้ในการเขียนขั้นตอนวิธีการออกแบบข้อมูลสำหรับใช้ในการเขียนขั้นตอนวิธี Input/Output Data Constant/Variable Data

  47. 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)

  48. 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

  49. Data Design Exercise • การแปลงค่าองศาเซลเซียสเป็นค่าองศาฟาเรนไฮต์ • ค่าองศาฟาเรนไฮต์ f = (9 /5)* c + 32 • ค่าคงที่ • ตัวแปร • ข้อมูลเข้า • ข้อมูลผลลัพธ์

  50. เครื่องมือในการพัฒนาโปรแกรมเครื่องมือในการพัฒนาโปรแกรม Programming Environment Program Development Tools

More Related