งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

คำสั่งวนซ้ำ (Looping)

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "คำสั่งวนซ้ำ (Looping)"— ใบสำเนางานนำเสนอ:

1 คำสั่งวนซ้ำ (Looping)
บทที่ 6 คำสั่งวนซ้ำ (Looping)

2 หัวข้อ ... คำสั่ง while คำสั่ง do-while คำสั่ง for คำสั่ง nested-for

3 คำสั่งวนซ้ำ ชนิดของการวนซ้ำ (Looping Types) while do-while
1. การวนซ้ำตามเงื่อนไข (Condition-controlled Loop) while do-while statement(s) do yes exit no while(condition) no exit statement(s) yes while(condition) 2. การวนซ้ำตามจำนวนนับ (Counting-controlled Loop) for i>N exit statement(s) iN for i=1 to N

4 6.1 คำสั่ง while รูปแบบ ถ้าเงื่อนไขใน expression เป็น
no exit statement(s) yes while(condition) รูปแบบ while (expression) statement; or while (expression) {statement;...} ถ้าเงื่อนไขใน expression เป็น TRUE จะทำ statement แต่ถ้า FALSE จะทำให้จบการทำซ้ำแบบ while no i = i+1 yes i = 1 Print i while i<=12 end ตัวอย่างเช่น การพิมพ์ค่า 1,2,3,4,5,6,7,8,9,10,11,12

5 ตัวอย่าง 6.1 เขียนโปรแกรมอ่าน Text (ที่เป็น String) ด้วย while ทีละตัว และจบ String ด้วย ENTER และตรวจสอบว่าแต่ละตัวอักษร จัดอยู่ในกลุ่มใด (A-Z, a-z, 0-9, อื่นๆ) และนับค่าเพิ่มในกลุ่ม นั้น (n1, n2, n3, n4) ผลลัพธ์ #include <stdio.h> void main() { char Text; int n1=0,n2=0,n3=0,n4=0; printf(“Enter text: ”); while ((Text=getchar()) != ‘\n’) { if ((Text>=‘A’) && (Text<=‘Z’)) n1++; else if ((Text>=‘a’) && (Text<=‘z’)) n2++; else if ((Text>=‘0’) && (Text<=‘9’)) n3++; else n4++; } printf(“\n#A-Z=%d, #a-z=%d, #0-9=%d\n”, n1, n2, n3); printf(“#special characters=%d\n”, n4); Enter text: C programming #A-Z=1, #a-z=11, #0-9=0 #special character=1 5

6 ตัวอย่าง 6.2 เขียนโปรแกรมอ่านข้อมูลของนักศึกษาทีละคน คือ ID, Name, X (คะแนน) ด้วย while และนับจำนวนนักศึกษา สมมติ เงื่อนไขสำหรับออกจาก while ด้วยค่า ID = -9 (Dummy) ผลลัพธ์ #include <stdio.h> void main() { int ID=0, X, n=0; char Name[20]; while (ID!=-9) { printf(“%d. Enter ID, Name, X : ”, n+1); scanf(“%d,%s,%d”, &ID, &Name, &X); n++; // count #students } printf(“#students =%d\n”, - -n); 1. Enter ID, Name, X: _ 50011, AA, 80 2. Enter ID, Name, X: _ 50012, BB, 67 3. Enter ID, Name, X: _ 50013, CC, 55 4. Enter ID, Name, X: _ 50014, DD, 90 5. Enter ID, Name, X: _ 50015, EE, 75 6. Enter ID, Name, X: _ -9, 0, 0 #students = 5 6

7 ตัวอย่าง 6.3 เขียนโปรแกรมหาค่าหรม (GCD: Great Common Divisor) ของ 2 ค่า เช่น GCD(150, 35) = 5 แนวคิด ค่าที่อาจเป็นได้ค่าแรก คือ ค่าน้อย (35) ระหว่าง 150,35 (ถ้า 150%35 = 0) แต่ 150%35 = 10 (เศษ 10) แสดงว่า GCD < 35 และค่าที่อาจเป็นได้ คือ 10 แต่ 35%10 = 5 (เศษ 5) แสดงว่า GCD < 10 และค่าที่อาจเป็นได้ คือ 5 m%n=0? แต่ 10%5 = 0 (จะได้ GCD = 5) m, n GCD(m, n) = GCD(n, m%n) = … = GCD(u, 0) = u #include <stdio.h> void main() { int m, n, r; printf(“Enter m, n: ”); scanf(“%d, %d”, &m, &n); while (n != 0) { ผลลัพธ์ Enter m, n: _ 150, 35 GCD = 5 r = m%n; m = n; n = r; } printf(“GCD =%d\n”, m); } 7

8 ตัวอย่าง 6.4 เขียนโปรแกรมคำนวณ ผลบวกของเลขนับ 1-100 ผลลัพธ์
(1+2+3+…+100 = ?) 1 3 1 100 6 หรือ Triangle number เช่น 10 15 21 1 28 36 45 #include <stdio.h> void main() { int i = 1, SUM = 0; while (i <= 100) { printf("%d+", i); 55 SUM=SUM+i i = i+1 yes while i<=100 start i = 1 SUM = 0 no SUM end 1 SUM= 0 2 SUM SUM = SUM + i; i++; } printf(“=%d\n”, SUM); SUM=SUM+i 3 ผลลัพธ์ 1+ 2+ ... 9+ 100 = 5050

9 6.2 คำสั่ง do-while รูปแบบ ทำ statement ก่อนทดสอบเงื่อนไข
do { statement; … } while (expression); ทำ statement ก่อนทดสอบเงื่อนไข statement(s) do yes exit no while(condition) แต่ เมื่อเปรียบเทียบกับคำสั่ง while ทดสอบเงื่อนไข expression ก่อน จึงทำ statement (ถ้าเงื่อนไขเป็นจริง) no exit statement(s) yes while(condition)

10 ตัวอย่าง 6.5 เขียนโปรแกรมคำนวณผลบวกของเลขนับ (1+2+3+…+100 = ?) โดยใช้ do-while SUM=SUM+i i = i+1 yes while i<=100 start i = 1 SUM= 0 no SUM end #include <stdio.h> void main() { int i=1, SUM=0; do { printf("%d + ", i); SUM = SUM + i; i++; } while (i <= 100); printf(" = %d\n", SUM); } 1 3 2 ผลลัพธ์ = 5050

11 ตัวอย่าง 6.6 เขียนโปรแกรมหาค่า Reverse ของเลขจำนวนเต็ม (เช่น Reverse ของ คือ 97531) โดยใช้ do-while แนวคิด ตัวเลขตัวสุดท้ายของ N = N%10 (ในที่นี้ คือ 9) ปรับค่า N = N/10 (ในที่นี้ คือ 1357) และทำซ้ำจนกระทั่ง N=0 #include <stdio.h> void main() { int N, R; printf(“Enter a number: ”); scanf(“%d”, &N); do { R = N%10; printf(“%d”, R); N = N/10; } while (N != 0); } 11

12 6.3 คำสั่ง for รูปแบบ ตัวอย่างเช่น exp1 เป็นการกำหนดค่าเริ่ม ( i=1 )
i > N exit loop statement(s) i  N for i=1 to N รูปแบบ for (exp1;exp2;exp3) Statement; or for (exp1;exp2;exp3) {Statement;…} ตัวอย่างเช่น for(i=1; i<=12; i++) M = i*2; i > N M = i*2 i  N for i=1 to 12 1 2 3 exp1 เป็นการกำหนดค่าเริ่ม ( i=1 ) exp2 เป็นการทดสอบเงื่อนไข (i<=12) ซึ่งมีผล 2 แบบ ถ้า TRUE จะทำ statement ถ้า FALSE จะจบคำสั่ง for exp3 เป็นการกำหนดค่าเพิ่ม (++) หรือลด (--) ( i++ )

13 ตัวอย่าง 6.7 เขียนโปรแกรมคำนวณ ผลบวกของเลขนับ (1+2+3+…+100 = ?) โดยคำสั่ง for #include <stdio.h> void main() { int i, SUM = 0; for (i=1; i<=100; i++) { printf("%d + ", i); SUM = SUM+i; } printf(" = %d\n", SUM); SUM=SUM+i yes for i=1to100 (i++) start SUM = 0 no SUM end 1 2 3 ผลลัพธ์ = 5050 การประยุกต์ใช้ … + 99 = ? ค่าเริ่มต้น = 1, ค่าสุดท้าย = 99, ค่าเพิ่ม = 2 … = ? ค่าเริ่มต้น = 2, ค่าสุดท้าย = 100, ค่าเพิ่ม = 2 13

14 ตัวอย่าง 6.8 เขียนโปรแกรมคำนวณ ผลบวกของเลขอนุกรม( …+100 = ?) โดย for 1 3 2 #include <stdio.h> void main() { int i, SUM = 0; for (i=5; i<=100; i+=5) { printf("%d + ", i); SUM = SUM+i; } printf(" = %d\n", SUM); ผลลัพธ์ = 1050

15 ตัวอย่าง 6.9 เขียนโปรแกรมคำนวณ ผลคูณของเลขนับ 1-10 (1x2x3x…x10 = ?) โดย for #include <stdio.h> void main() { int i; float MUL = 1; for (i=1; i<=10; i++) { printf("%d x ", i); MUL = MUL*i; } printf(" = %.0f\n", MUL); MUL = MULxi yes for i=1to10 (i++) start MUL = 1 no MUL end ผลลัพธ์ 1x2x3x4x5x6x7x8x9x10 =

16 ตัวอย่าง 6.10 เขียนโปรแกรมคำนวณค่า N! เมื่อ N เป็นค่าที่รับจาก Keyboard เมื่อ N! = Nx(N-1)x(N-2)x…x3x2x1 เช่น 5! = 5x4x3x2x1 #include <stdio.h> void main() { int i, N; float Fac=1; printf("Enter N: "); scanf("%d", &N); Fac = Fac*i yes for i=Nto1 (i--) start Input N Print N, Fac end no for (i=N; i>=1; i--) Fac = Fac * i; printf("%d! = %.0f\n", N, Fac); } ผลลัพธ์ Enter N : _ 5 5! = 120

17 ตัวอย่าง 6.11 เขียนโปรแกรมคำนวณตาราง สูตรคูณแม่ T
เมื่อ T เป็นค่าที่รับจาก Keyboard ผลลัพธ์ Enter Table = _ 2 Multiplication Table #2 #include <stdio.h> void main() { int i, T; float R; printf("Enter Table = "); scanf("%d", &T); 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 . . . 2 x 12 = 24 R = T*i yes for i=1to12 (i++) start Input T Print T,i,R end no printf("Multiplication Table #%d\n", T); for (i=1; i<=12; i++) { R = T * i; printf(" %d x %d = %.0f\n", T, i, R); } 17

18 6.4 คำสั่ง nested-for รูปแบบ for (exp1;exp2;exp3) { Statement; … }
Loop i เริ่มก่อน แต่จบที่หลัง Loop j เริ่มหลัง แต่จบก่อน

19 ตัวอย่าง การกำหนดค่าตัวแปร i,j สำหรับข้อมูล 2 มิติ (ขนาด5x5)
,2 ,3 ,4 ,5 for (i=1; i<=5; i++) { for (j=1; j<=5; j++) printf(“*”) printf(“\n”); } i=1 * * * * * i=2 * * * * * i=3 * * * * * i=4 * * * * * i=5 * * * * * j=1 ,2 ,3 ,4 ,5 for (i=1; i<=5; i++) { for (j=1; j<=i; j++) printf(“*”) printf(“\n”); } i=1 * i=2 * * i=3 * * * i=4 * * * * i=5 * * * * * priintf(“%d”, i); priintf(“%d”, j);

20 ตัวอย่าง 6.12 เขียนโปรแกรมพิมพ์ค่า j (1,2,3,…,N) แบบ 2 มิติ (i: row, j: col)ขนาดNxN end no start For i=1 to N Print \n Input N #include <stdio.h> void main() { int i, j, N; printf(“Enter N = ”); scanf(“%d”, &N); for (i=1; i<=N; i++) { for (j=1; j<=N; j++) printf(“%d”, j); printf(“\n”); } yes Print j For j=1 to N ผลลัพธ์ Enter N = _ 5 12345 12345 12345 12345 12345

21 ตัวอย่าง 6.13 เขียนโปรแกรมพิมพ์ผลบวก i+j ขนาด NxN เมื่อ i, j = 1,2, 3, … , N end no start For i=1toN Print \n Input N #include <stdio.h> void main() { int i, j, N; printf(“Enter N = ”); scanf(“%d”, &N); for (i=1; i<=N; i++) { for (j=0; j<N; j++) printf(“%d”, i+j); printf(“\n”); } yes Print i+j For j=0toN-1 ผลลัพธ์ Enter N = _ 5 12345 23456 34567 45678 56789

22 ตัวอย่าง 6.14 เขียนโปรแกรมพิมพ์ตาราง Latin Square ขนาด NxN เช่น N = 5
for i=1 to N Print L i >N end for j=0 to N-1 L = (i + j) jN-1 j > N-1 i N Input N start #include <stdio.h> void main() { int i, j, N, L; printf(“Enter N = ”); scanf(“%d”, &N); for (i=1; i<=N; i++) { for (j=0; j<N; j++) { L = i+j; printf("%d ", L); } printf(“\n”); L>N L = L-N yes no if (L>N) L = L-N; ผลลัพธ์ 5 Enter N = _ 12345 23456 1 34567 12 45678 123 56789 1234

23 Case Study-1 เขียนโปรแกรมแปลงค่าเลขฐาน 10 (N) เป็นเลขฐาน 2 (Y)
ผลลัพธ์ N = N/2 Enter N = _ 55 y = N%2 y0 = 1 #include <stdio.h> void main() { int N, y, i=0; printf("Enter N = "); scanf("%d", &N); do { y = N%2; N = N/2; printf(“y%d = %d\n”, i++, y); } while (N!=0); } y1 = 1 y2 = 1 y3 = 0 y4 = 1 y5 = 1 23

24 CS-1 เขียนโปรแกรมแปลงค่าเลขทศนิยมฐาน 10 (.F) เป็นเลขฐาน 2 (.f)
(F = F-f) ผลลัพธ์ F = F*2 Enter .F = _ .625 f = (int)F f-1 = 1 #include <stdio.h> void main() { float F; int f, i=1; printf("Enter .F = "); scanf("%f", &F); do { F = F*2; f = (int)F; // integer part of F*2 F = F-f; // fraction part of F*2 printf(“f-%d = %d\n”, i++, f); if (i>5) break; } while (F!=0); } f-2 = 0 f-3 = 1 24

25 Case Study-2 เขียนโปรแกรมเพื่อตรวจสอบว่า
N เป็นเลขเฉพาะ (Prime Number) ? (ตัวอย่าง Prime no. 2, 3, 5, 7, 11, 13, 17, 19, 23, ... ) Prime no. เป็นเลขเฉพาะที่หารไม่ลงตัวด้วยเลขใดๆ ยกเว้น N แนวคิด: N เป็น Prime no. ถ้าหารด้วยค่าใดๆ ไม่ลงตัว ยกเว้น 1 และ N (ตัวหารที่ต้องทดสอบคือ 2, 3, …, (N-1)) N%2≠0 N%3≠0 ... N%(N-1)≠0 (N-2 loops, i = 2,3,...,N-1) เช่น N=5 is prime? 5%2 = 1 5%3 = 2 5%4 = 1 (yes) แต่ N=9 is not prime 9%2 = 1 9%3 = 0 แต่หารลงตัวแม้เพียงค่าเดียว N จะไม่เป็น Prime no. 25

26 CS-2 i<n For i=2 to N-1 start i³n Not Pr end Input N N%i=0 yes no i³N Prime #include <stdio.h> void main() { unsigned long N, i; printf("Enter +N = "); scanf("%ld", &N); for (i=2; i<N; i++) // divisor i = 2, 3, 4, 5, …, N-1 if (N%i == 0) break; // i < N (not prime) if (i>=N) printf(“N=%ld is prime number\n", N); else printf(“N=%ld is not prime number\n", N); } 26

27 แนวคิด (วิธีที่เร็ว) N/2 … N-1 N การหาว่า N เป็น Prime? (แบบง่ายใช้ N-2 รอบ: i = 2, 3, 4, ..., N-1) เช่น N = 7, 7%2 = 1 (≠0) 7%3 = 1 7%4 = 3 เนื่องจาก Prime no. คือ 2, 3, 5, 7, 11, 13,... วิธีที่เร็วขึ้น (~N/2 รอบ: i = 3, 5, 7, ..., N-1) 7%5 = 2 7%6 = 1 เลขคู่ (Even no.) ไม่ใช่ prime (ยกเว้น 2) เลขคี่ (Odd no.) ตรวจสอบด้วยการหารด้วย 3, 5, 7, … , N-1 ถ้า n=7, N-2=5 ถ้า N=7, N/2=3 7%3 = 1 (≠0) 7%3 = 1 (≠0) 7%5 = 2 วิธีที่เร็วที่สุด (~N/4 รอบ: i = 3, 5, 7,..., N/2) เนื่องจาก N/(N/2) = 2 และ N/(N) = 1 (ถ้าหาร N ด้วย i = N/2+1, N/2+2, …, N-2 => N/i = [ ]) 27

28 in/2 For i=3 to N/2 start i>n/2 Not Pr end Input N N%i=0 yes no i>N/2 Prime N=1 Not pr N=2 N%2=0 CS-2(Efficient) #include <stdio.h> void main() { unsigned long N, i; printf("Enter +N = "); scanf("%ld", &N); if (N==1) printf(“N=%ld is not prime no.\n", N); else if (N==2) printf(“N=%ld is prime no.\n", N); else if (N%2==0) printf(“N=%ld is not prime no.\n", N); else { // Odd numners // some odd no. are prime for (i=3; i<N/2; i+=2) // divisor i=3, 5, 7, …, N/2 if (N%i == 0) break; // not prime if (i > N/2) printf(“N=%ld is prime no.\n", N); else printf(“N=%ld is not prime no.\n", N); } 28


ดาวน์โหลด ppt คำสั่งวนซ้ำ (Looping)

งานนำเสนอที่คล้ายกัน


Ads by Google