1 / 32

อัลกอริทึ่มทำงานวนซ้ำ

อัลกอริทึ่มทำงานวนซ้ำ. หลักการเขียนโปรแกรมคอมพิวเตอร์. เนื้อหา. ทบทวนการไหลของโปรแกรม โปรแกรมที่ทำงานแบบวนซ้ำ ลูปแบบ while ลูปแบบ do..while ลูปแบบ For. START. START. Statement 1. Statement. Statement 2. END. Statement 3. Statement n. END. การไหลของโปรแกรมแบบต่าง ๆ.

idola-barry
Download Presentation

อัลกอริทึ่มทำงานวนซ้ำ

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. อัลกอริทึ่มทำงานวนซ้ำอัลกอริทึ่มทำงานวนซ้ำ หลักการเขียนโปรแกรมคอมพิวเตอร์

  2. เนื้อหา • ทบทวนการไหลของโปรแกรม • โปรแกรมที่ทำงานแบบวนซ้ำ • ลูปแบบ while • ลูปแบบ do..while • ลูปแบบ For

  3. START START Statement1 Statement Statement2 END Statement3 Statementn END การไหลของโปรแกรมแบบต่าง ๆ • โปรแกรมอย่างง่าย มีการไหลจากบนลงล่าง โปรแกรมที่มีคำสั่งเดียว โปรแกรมที่มีหลายคำสั่ง

  4. START START condition true true condition false Statement false Statementt Statementf Statement Statementt Statementf END END การไหลของโปรแกรมแบบต่าง ๆ • โปรแกรมแบบมีเงื่อนไข คำสั่ง if คำสั่ง if..else

  5. การวนซ้ำ • กิจกรรมหลายอย่างมีลักษณะการทำงานแบบซ้ำไปมา • ตัวอย่างในชีวิตประจำวัน • ก้าวไปข้างหน้า 10 ก้าว • ขยี้ผ้าจนกว่าจะสะอาด • เรียนวิชาคอมพิวเตอร์จนกว่าจะผ่าน • ตัวอย่างในการเขียนโปรแกรม • วนรับตัวเลขมา 10 ตัวเพื่อหาค่าเฉลี่ย

  6. โปรแกรมแบบวนซ้ำดียังไง?โปรแกรมแบบวนซ้ำดียังไง? • เพื่อหลีกเลี่ยงการเขียนโค้ดแบบเดียวกันซ้ำหลายรอบ • ได้โปรแกรมที่สั้นและกระชับขึ้น • โปรแกรมจะมีความยืดหยุ่นมากกว่า • เช่นรับข้อมูลได้หลากหลายขนาด โดยไม่ต้องแก้โปรแกรม • ลองนึกถึงโปรแกรมที่หาผลรวมของตัวเลข 3 ตัว

  7. เกิดโจทย์เปลี่ยนเป็นหาผลรวมของเลข 100 ตัว? หรือกี่ตัวก็ได้จนกว่าผู้ใช้จะป้อนเลขลบ ตัวอย่าง: หาผลรวมของตัวเลข 3 ตัว Start Set sum=0 Print “Enter a value” Input n Compute Sum=sum+n Print “Enter a value” Input n Compute Sum=sum+n Print “Enter a value” Input n Compute Sum=sum+n stop 1st number 2nd number 3rd number

  8. START Let sum = 0 and i = 1 i num false true n Increment sum by n Increment i by 1 sum END หาผลรวมตัวเลข: โฟลว์ชาร์ต

  9. START false true Statement Statement END ลูปแบบ while • ทำ statementตราบเท่าที่ conditionเป็นจริง • ทำ stmt1,…,stmtNตราบเท่าที่ conditionเป็นจริง while (condition) statement; condition while (condition) { stmt1; stmt2; : stmtN; }

  10. หาผลรวมตัวเลข: โปรแกรม EX1. ให้เขียนโปรแกรมหาผลรวมตัวเลขโดยใช้ลูป while และให้รับค่าจำนวนลูป (num) จากผู้ใช้

  11. START Let sum = 0 and i = 1 i num false true n num Increment sum by n Increment i by 1 sum END หาผลรวมตัวเลข: โฟลว์ชาร์ต เงื่อนไขที่เปรียบเทียบระหว่างตัวนับรอบ กับ ค่าควบคุม loop ค่าควบคุม loop ตัวนับรอบ ซึ่งจะเดินเข้าหา ค่าควบคุมloop เสมอ

  12. หาผลรวมตัวเลข: โปรแกรม #include <stdio.h> #include <conio.h> void main() { int i, n, sum = 0, num; printf("How many values do you have: "); scanf(“%d”,&num); i = 1; while (i <= num) { printf("Enter a value: "); scanf(“%d”,&n); sum = sum + n; i = i + 1; } printf("The sum is %d.", sum); }

  13. ลับสมอง • เขียนโฟลว์ชาร์ตและโปรแกรมภาษา C เพื่อคำนวณค่า sum โดยที่ sum นิยามจากสูตร • โดยที่ค่า Nรับมาจากผู้ใช้ • เช่นถ้าN = 3, sum = 1+4+9 = 14 • ตัวอย่างผลลัพธ์ #include <stdio.h> #include <conio.h> voidmain() { intsum = 0, i = 1, N; printf("Enter N: "); scanf(“%d”,&N); while( i <= N ) { sum = sum + i*i; i = i+1; } printf("The sum is %d.",sum); } ??? ??? Enter N: 3 The sum is 14

  14. ลูปแบบดักค่า • ที่ผ่านมาเป็นตัวอย่างของลูปวนนับ (counter loop) • จำนวนครั้งที่วนซ้ำขึ้นอยู่กับค่าที่กำหนดไว้แล้ว • ทำอย่างไรหากเราไม่ทราบจำนวนล่วงหน้า • ใช้ลูปแบบดักค่า (sentinel loop) • นิยามค่าที่ใช้ดัก เพื่อให้โปรแกรมหลุดออกจากลูป • ตัวอย่าง ตัวดัก Enter a number, or -1 to quit: 3 Enter a number, or -1 to quit: 10 Enter a number, or -1 to quit: 15 Enter a number, or -1 to quit: -1 The sum is 28

  15. ตัวอย่าง: ลูปดักค่า #include <stdio.h>; #include <conio.h>; void main() { int n, sum = 0; printf("Enter a number, or -1 to quit: "); scanf (“%d“,n); while (n != -1) { sum = sum + n; printf("Enter a number, or -1 to quit: "); scanf (“%d“,n); } printf("The sum is %d.", sum); }

  16. START Statement Statement true false ลูปแบบ do...while • ทำstmt1...stmtNตราบเท่าที่เงื่อนไขยังเป็นจริง • stmt1...stmtNจะถูกทำงานอย่างน้อยหนึ่งครั้ง do { stmt1; stmt2; : stmtN; } while (condition); condition END

  17. ลูปแบบ do...while a = 5 do { a=a+1 print a } While (a<5) a = 5 While (a<5) { a=a+1 print a }

  18. ลูปแบบดักค่าแบบdo...while #include <stdio.h>; #include <conio.h>; void main() { int n, sum = 0; do { printf("Enter a number, or -1 to quit: "); scanf(“%d”,&n); if (n != -1) sum = sum + n; } while (n != -1); printf("The sum is {0}.", sum); }

  19. สรุป • การเขียนโปรแกรมแบบวนซ้ำ • ลูปวนนับ • ลูปดักค่า • โครงสร้างลูปแบบ while • โครงสร้างลูปแบบ do..while

  20. แบบฝึกหัด 6.1 • จงเขียนผังงานแสดงเลขคู่ที่อยู่ภายใน 1-100 ออกทางจอภาพโดยใช้โครงสร้างการวนซ้ำแบบ while…structure • จงเขียนผังงานหาผลรวม และหาค่าเฉลี่ยของข้อมูลตัวเลขทศนิยมชุดหนึ่ง จำนวน 100 ตัว โดยใช้โครงสร้างการวนซ้ำแบบ while…structure • จากโจทย์ข้อ 2 ให้เขียนด้วยโครงสร้างแบบ do/while…structure • จากโจทย์ข้อ 1 แก้ไขโปรแกรมให้สามารถแสดงเลขคู่ได้ไม่จำกัดรอบ จนกว่าผู้ใช้จะกรอกตัวเลข -1

  21. แบบฝึกหัด 6.1 1....... 2...... 3. ตรวจสอบว่า a <= 10 ใช่หรือไม่ - ใช่ - ทำ คำสั่งนี้... - วนกลับไปทำข้อ 3 - ไม่ใช่ ทำข้อ 4... 4. ... 1....... 2...... 3. ทำ คำสั่งนี้... • 4. ตรวจสอบว่า a <= 10 ใช่หรือไม่ • - ใช่ วนกลับไปทำข้อ 3 ... • - ไม่ใช่ ทำข้อ 5 • 5. .....

  22. START Let sum = 0 and i = 1 false true n Increment sum by n END จงเขียนผังงานหาผลรวม และหาค่าเฉลี่ยของข้อมูลตัวเลขทศนิยมชุดหนึ่ง จำนวน 100 ตัว 1. กำหนดให้ i = 1, sum = 0 2. ตรวจสอบ i <= 100 ใช่หรือไม่ - ใช่ - รับค่าตัวเลขเก็บใน n - sum = sum+n - i = i+1 - วนกลับไปทำข้อ 2 - ไม่ใช่ ทำข้อ 3 3. avg = sum/100 4. แสดงค่า sum 5. แสดงค่า avg i 100 Increment i by 1 avg = sum / 100 sum, avg

  23. การวนซ้ำโดยใช้โครงสร้าง for

  24. คำสั่งfor • สะดวกสำหรับการนับรอบของลูป • การดำเนินการที่เกี่ยวข้อง • ทำinit_stmtหนึ่งครั้ง • ตรวจสอบcondition; ถ้าเป็นจริงให้ทำงานในloop body • ทำupdate_stmt, แล้วทำงานในขั้นตอนที่ 2 for (init_stmt; condition; update_stmt) statement; for (init_stmt; condition; update_stmt){ statement1; statement2; : statementN; }

  25. START Initialize counter condition true Statement Statement END คำสั่ง for: ผังการทำงาน false Update counter

  26. #include <stdio.h> #include <conio.h> void main() { int i; i = 1; while (i <= 10) { printf(“%d”,i); i++; } } #include <stdio.h> #include <conio.h> void main() { int i; for (i = 1; i <= 10; i++) { printf(“%d”,i); } } for loop while loop for vs. while • คำสั่ง: พิมพ์เลข 1,2,...,10 ออกหน้าจอ

  27. ทดสอบ: พิมพ์ดาว • เขียนโปรแกรมเพื่อพิมพ์ดาวจำนวน N ดวง เมื่อ N กำหนดโดยผู้ใช้ • ตัวอย่าง #include <stdio.h>; #include <conio.h>; void main() { int i,N; printf("Enter N: "); scanf(“%d“,N); for (i = 1; i <= N; i++) { printf("*"); } printf(“\n”); } } Enter N: 3 *** Enter N: 8 ******** ? ? ? ???

  28. ตัวอย่าง • แสดงค่า 0,2,4,...,20 • แสดงค่า 1,3,5,...,19 • แสดงค่า 15,12,9,...,0 for (i = 0; i <= 20; i += 2) printf(“%d”,i); for (i = 1; i <= 19; i += 2) printf(“%d”,i); ? ? ? for (i = 15; i >= 0; i -= 3) printf(“%d”,i); ? ? ?

  29. ลูปหลายชั้น • โปรแกรมที่มีความซับซ้อนมากขึ้นอาจมีความต้องการใช้ลูปหลายชั้น • คล้ายกันกับคำสั่ง if • ตัวอย่าง #include <stdio.h>; #include <conio.h>; void main() { int sum = 0, i, j, N; printf("Enter N: "); scanf(“%d“,N); for (i = 1; i <= N; i++) { for (j = 1; j <= i; j++) { sum = sum + j; } } printf("Sum is %d", sum); } OuterLoop InnerLoop

  30. ตัวอย่าง: พิมพ์ดาวอีกครั้ง! • เพิ่มเติมจากโปรแกรมพิมพ์ดาวเดิม • ตัวอย่าง Enter N: 3 * ** *** #include <stdio.h>; #include <conio.h>; void main() { int i, j, N; printf(“Enter N:”); scanf(“%d”,&N); for (i = 1; i <= N; i++) { for (j = 1; j <= i; j++) { printf("*"); } printf(“\n”); } } Enter N: 5 * ** *** **** *****

  31. แบบฝึกหัด 6.2 • จงเขียนโปรแกรมเพื่อรับตัวเลขอินพุท Nจากผู้ใช้และแสดงตัวเลขทั้งหมดที่เป็นตัวประกอบของ N (นำไปหาร Nแล้วลงตัว)

  32. แบบฝึกหัด 6.3 • จงเขียนโปรแกรมเพื่อรับตัวเลขอินพุท Nจากผู้ใช้และแสดงตัวเลขทั้งหมดที่เป็นตัวประกอบของ N (นำไปหาร Nแล้วลงตัว)

More Related