Algorithm Pseudocode Flowchart Program start Read num 1

  • Slides: 28
Download presentation

การแปลง Algorithm เปนโปรแกรม Pseudo-code Flowchart Program start Read num 1, num 2 total =

การแปลง Algorithm เปนโปรแกรม Pseudo-code Flowchart Program start Read num 1, num 2 total = num 1+num 2 Print total num 1, num 2 total=num 1+num 2 total printf(“Enter Num 1 : “); scanf(“%d”, &num 1); printf(“Enter Num 2 : “); scanf(“%d”, &num 2); total = num 1+num 2; printf(“%d“, total); stop �������������������� �������

การแปลง Algorithm เปนโปรแกรม Pseudo-code Flowchart Program start Get max, min average = (max+min)/2 Display

การแปลง Algorithm เปนโปรแกรม Pseudo-code Flowchart Program start Get max, min average = (max+min)/2 Display average max, min average=(max+min)/2 average printf(“Enter Max : “); scanf(“%d”, &max); printf(“Enter Min : “); scanf(“%d”, &min); average=(max+min)/2; printf(“%d“, average); stop ���������

การแปลง Algorithm เปนโปรแกรม Pseudo-code Flowchart start Get lw, ll, hw, hl la = lw

การแปลง Algorithm เปนโปรแกรม Pseudo-code Flowchart start Get lw, ll, hw, hl la = lw * ll ha = hw * hl ya = la – ha Display ya lw, ll, hw, hl la = lw * ll ha = hw * hl ya = la - ha ya stop Program printf(“Enter Land Width : “); scanf(“%d”, &lw); printf(“Enter Land Length : “); scanf(“%d”, &ll); printf(“Enter House Width : “); scanf(“%d”, &hw); printf(“Enter House Length : “); scanf(“%d”, &hl); la = lw * ll; ha = hw * hl; ya = la – ha; printf(“%d“, ya); ���������

โครงสรางแบบทางเลอก n Multi-Selection n แบบใชโครงสราง if / else … Structure หลายชน เปนแบบตรวจสอบเงอนไขหลายชน การตรวจสอบแตละชนจะไดผลการตร วจสอบเชนเดยวกบแบบ

โครงสรางแบบทางเลอก n Multi-Selection n แบบใชโครงสราง if / else … Structure หลายชน เปนแบบตรวจสอบเงอนไขหลายชน การตรวจสอบแตละชนจะไดผลการตร วจสอบเชนเดยวกบแบบ Double Selection False if (condition 1( statement 1 else if (condition 2( statement 2. . . if (condition. N( statement. N else statement. N+1 Condition True False Condition True Process 4 Process 3 Process 2 Process 1

ตวอยางโครงสรางแบบทางเลอก Start READ(SC) SC < 50 N SC < 60 N SC < 70

ตวอยางโครงสรางแบบทางเลอก Start READ(SC) SC < 50 N SC < 60 N SC < 70 N SC < 80 N GR = ‘A’ Print(SC, GR) Y GR = ‘F’ Y GR = ‘D’ Y GR = ‘C’ Y GR = ‘B’ scanf(“%d”, &sc); if (sc<=50) GR=‘F’; else if (sc<=60) GR=‘D’; else if (sc<=70) GR=‘C’; else if (sc<=80) GR=‘B’; else GR=‘A’; printf(“%d %c”, sc, GR); ���������

โครงสรางแบบทางเลอก n Multi-Selection (Cont. ) n แบบใชโครงสราง case or switch … Structure เปนการตรวจสอบเงอนไขวาตวแปรตรวจสอบมคาตรงกบคา (Value)

โครงสรางแบบทางเลอก n Multi-Selection (Cont. ) n แบบใชโครงสราง case or switch … Structure เปนการตรวจสอบเงอนไขวาตวแปรตรวจสอบมคาตรงกบคา (Value) ใด Case 1. True Process False True Case 2. Process Case n. True Process False Process switch (selector) { case 1: statement 1; break; case 2: statement 2; break; . . . case n: statementn; break; default: statementd; //optional break; }

โครงสรางแบบทางเลอก n ตวอยาง การใช switch-case switch (selector) { case 1: statement 1; break; case

โครงสรางแบบทางเลอก n ตวอยาง การใช switch-case switch (selector) { case 1: statement 1; break; case 2: statement 2; break; . . . case n: statementn; break; default: statementd; //optional break; } switch (i) { case 1 : grade = 'A'; break; case 2 : grade = 'B'; break; case 3 : grade = 'c'; break; default : printf("%c not in range", i); break; }

โครงสรางแบบทางเลอก n ตวอยาง if (x == 0. 0( printf ("x is equal to 0.

โครงสรางแบบทางเลอก n ตวอยาง if (x == 0. 0( printf ("x is equal to 0. n; (" else printf ("x is greater or less than 0. n; (" if (x == 0. 0( } printf ("x is equal to 0. n; (" y = 0; { else { printf ("x is greater or less than 0. n"); y = 1; } ���������