01001012 Principle of Computer Programming Problem Algorithms Representation
01001012 Principle of Computer Programming Problem Algorithms Representation Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 5 King Mongkut's Institute of Technology
Origami Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 01001012 Principle of Computer Programming 7 King Mongkut's Institute of Technology
01001012 Principle of Computer Programming Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 8 King Mongkut's Institute of Technology
����� : 01001012 Principle of Computer Programming ������� Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 9 King Mongkut's Institute of Technology
สญลกษณทใชในการเขยนผงงาน (1) 01001012 Principle of Computer Programming ������� Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 14 King Mongkut's Institute of Technology
สญลกษณทใชในการเขยนผงงาน (2) 01001012 Principle of Computer Programming ���������� Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 15 King Mongkut's Institute of Technology
สญลกษณทใชในการเขยนผงงาน (3) 01001012 Principle of Computer Programming ��������� Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 16 King Mongkut's Institute of Technology
สญลกษณทใชในการเขยนผงงาน (4) 01001012 Principle of Computer Programming ���������� Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 17 King Mongkut's Institute of Technology
สญลกษณทใชในการเขยนผงงาน (5) 01001012 Principle of Computer Programming �������� Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 18 King Mongkut's Institute of Technology
สญลกษณทใชในการเขยนผงงาน (6) 01001012 Principle of Computer Programming ���������� Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 19 King Mongkut's Institute of Technology
สญลกษณทใชในการเขยนผงงาน (7) 01001012 Principle of Computer Programming ���������� Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 20 King Mongkut's Institute of Technology
สญลกษณทใชในการเขยนผงงาน (8) 01001012 Principle of Computer Programming ���������� Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 21 King Mongkut's Institute of Technology
ตวอยางผงงานโปรแกรม 01001012 Principle of Computer Programming #include<stdio. h> int main() { printf ("Hello World!"); return 0; } Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 23 King Mongkut's Institute of Technology
แนวทางสรางโปรแกรมคำนวณหาพนทวงกลม (3) 01001012 Principle of Computer Programming float radius, pi, area; pi=22. 0/7; // pi = 3. 14; printf ("Enter Radius of Circular : "); scanf ("%f", &radius); area = pi * radius; printf ("Area of Circular : %f", area); Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 27 King Mongkut's Institute of Technology
แนวทางสรางโปรแกรมคำนวณหาพนทวงกลม (4) 01001012 Principle of Computer Programming #include<stdio. h> int main() { float radius, pi, area; pi = 22/7; printf ("Enter Radius of Circular : "); scanf ("%f", &radius); area = pi * radius; printf ("Area of Circular : %f", area); return 0; } Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 28 King Mongkut's Institute of Technology
โจทย : โปรแกรมรวมคะแนนเรยน 01001012 Principle of Computer Programming จงเขยนผงงาน และโปรแกรมรบชอนาม สกล รหสนกศกษา และคะแนนเกบ แลวแสดงผลคำนวณคะแนนรวม Enter your Name Surname : Jirasak Sittigorn – ตวอยางการรนโปรแกรม Enter your Student Enter your Score Laboratory (10%) Midterm (40%) Final (50%) Total (100%) = 98 Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ ID : 40010000 = 10 = 39 = 49 29 King Mongkut's Institute of Technology
ตวอยางการใชงานเครองหมายการเปรยบเทยบ 01001012 Principle of Computer Programming การเปรยบเท ยบ 7 == 9 7 != 9 8 > 8 8 >= 8 (10+9)<7 4 <= 3 Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ ผลทได False True False การเปรยบเท ยบ 22 == 22 (3+5)!=8 9 > 7 7 >= 9 7<(10+9) 3 <= 4 ผลทได True False True 32 King Mongkut's Institute of Technology
เครองหมายทางตรรกศาสตร เครองหมาย && || ! 01001012 Principle of Computer Programming ความหมาย และ (and) หรอ (or) ไม หรอ ตรงกนขาม Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ ตวอยาง x && y x || y (not) !x 33 King Mongkut's Institute of Technology
การใชงานเครองหมายทางตรรกศาสตร (2) 01001012 Principle of Computer Programming int num 1 = 10, num 2 = 20, num 3 = 30; num 1 == num 2 False num 1 > num 2 False (num 1<num 2) && (num 2<num 3) True (num 1>num 2) || (num 1>num 3) False (num 1>num 2) || (num 2<num 3) True Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 35 King Mongkut's Institute of Technology
การใชคำสง if 01001012 Principle of Computer Programming if (expression) statement; if (expression) { statement-1. 1; statement-1. 2; . . . statement-1. n; } Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 36 King Mongkut's Institute of Technology
การใชคำสง int a = ; int b = ; if (a>b) { statement } printf( Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ if กบนพจน 01001012 Principle of Computer Programming a = b = 1; 2; n; ); ; ; a > b False True statement 1; statement 2; statement 3; 37 King Mongkut's Institute of Technology
โปรแกรมตรวจสอบคะแนน | if START first, last, id, point, full first, last, id (4) 1 Principle of Computer Programming 01001012 point/full>=0. 54 False True "passed" point, full 1 END Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 41 King Mongkut's Institute of Technology
โปรแกรมตรวจสอบคะแนน | if (5) 01001012 Principle of Computer Programming #include<stdio. h> int main() { char first[20], last[20], id[9]; float point, full; printf ("Enter your Name : "); scanf ("%s", first); printf ("Enter your Surname : "); scanf ("%s", last); printf ("Enter your ID : "); scanf ("%s", id); //continue Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 42 King Mongkut's Institute of Technology
โปรแกรมตรวจสอบคะแนน | if (6) 01001012 Principle of Computer Programming printf ("Enter your examination points : "); scanf ("%f", &point); printf ("Enter your total points : "); scanf ("%f", &full); if ((point/full) >= 0. 54) { printf ("Name : %s %sn", first, last); printf ("ID : %sn", id); printf ("Examination points : %f / %fn", point, full); printf ("You passed, Congratulationn"); } return 0; } Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 43 King Mongkut's Institute of Technology
การใชคำสง if-else | ภายในเงอนไขมคำสงเดยว 01001012 Principle of Computer Programming if (expression) statement-1; else statement-2; Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 44 King Mongkut's Institute of Technology
การใชคำสง if-else | ภายในเงอนไขมหลายคำสง if (expression( } statement– 1. 1; statement-1. 2; . . . statement-1. n; { else } statement-2. 1; statement-2. 2; . . . statement-2. n; { Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 01001012 Principle of Computer Programming 45 King Mongkut's Institute of Technology
โปรแกรมหารเลข | if-else START (3) 101001012 Principle of Computer Programming num 1, num 2 != 0 num 1 True False "Error" num 1/num 2 1 Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ END 48 King Mongkut's Institute of Technology
โปรแกรมหารเลข | if-else #include<stdio. h> int main() { float num 1, num 2; printf ("Enter number 1 : scanf ("%f", &num 1); printf ("Enter number 2 : scanf ("%f", &num 2); if (num 2 != 0) printf ("%. 2 f / %. 2 f = else printf ("Error divided return 0; } Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ (4) 01001012 Principle of Computer Programming "); %. 2 f", num 1, num 2, num 1/num 2); by zeron"); 49 King Mongkut's Institute of Technology
การใชคำสง if-else if | ภายในเงอนไขมคำสงเดยว 01001012 Principle of Computer Programming if (expression-1( statement-1; else if (expression-2( statement-2; . . . else if (expression-m( statement-m; else statement-m+1; Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 50 King Mongkut's Institute of Technology
การใชคำสง if-else if | ภายในเงอนไขมหลายคำสง if (expression-1( } statement-1. 1; . . . statement-1. n{; else if (expression-2( } statement-2. 1; . . . statement-2. n; {. . . Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 01001012 Principle of Computer Programming . . . else if (expression-m( } statement-m. 1; . . . statement-m. n; { else } statement-m+1. 1; . . . statement-m+1. n; { 51 King Mongkut's Institute of Technology
โปรแกรมตรวจสอบเกรด | if-else if (4) 01001012 Principle of Computer Programming D C B A F 0 60 D F 0 Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 60 80 70 B C 70 90 80 100 A 90 100 55 King Mongkut's Institute of Technology
โปรแกรมตรวจสอบเกรด | if-else if START (6) 01001012 Principle of Computer Programming first, last, id, point>=90 True "Grade A" False a Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ b 57 King Mongkut's Institute of Technology
a โปรแกรมตรวจสอบเกรด | if-else if point>=80 True b (7) 01001012 Principle of Computer Programming "Grade B" False point>=70 True "Grade C" False point>=60 True "Grade D" False "Grade F" END Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 58 King Mongkut's Institute of Technology
point โปรแกรมตรวจสอบเกรด | if-else if point>=90 True A True B point>=70 && point<80 point>=60 && point<70 False point>=70 point>=90 && point<=100 point>=80 && point<90 False point>=80 (8) 01001012 Principle of Computer Programming True C point>=0 && point<60 False point>=60 True D False F Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 59 King Mongkut's Institute of Technology
การใชคำสง switch | คำสง switch (expression-1) { case constant-expr-1: statement-1. 1; statement-1. 2; . . . statement-1. n; break; case constant-expr-2: statement-2. 1; statement-2. 2; . . . statement-2. n; break; . . . Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 01001012 Principle of Computer Programming . . . case constant-expr-m: statement-m. 1; statement-m. 2; . . . statement-m. n; break; default: statement-d. 1; statement-d. 2; . . . statement-d. n; } 60 King Mongkut's Institute of Technology
การใชคำสง Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ switch | Flowchart 01001012 Principle of Computer Programming 61 King Mongkut's Institute of Technology
ตวอยางการใชคำสง switch-1 (1) 01001012 Principle of Computer Programming #include<stdio. h> int main () } int d ; printf("Enter a number from 1 to 9 ; (" : scanf("%d& , "d ; ( switch (d ( } case 1: puts("A stitch in time saves nine ; (". break ; case 2: case 6: case 9: puts("Handsome is as handsome does ; (". break ; default: puts("Very clever. Try again. "); { return 0; { Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 62 King Mongkut's Institute of Technology
ตวอยางการใชคำสง switch-1 (2) 01001012 Principle of Computer Programming d d 1 A stitch in time saves nine. Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 2, 6, 9 Handsome is as handsome does. default Very clever. Try again. 63 King Mongkut's Institute of Technology
ตวอยางการใชคำสง switch-2 (1) 01001012 Principle of Computer Programming #include<stdio. h> int main () } int d ; printf("Enter a number from 1 to 9 ; (" : scanf("%d& , "d ; ( switch (d ( } case 1: puts("A stitch in time saves nine ; (". break ; case 2: break; case 6: break; case 9: puts("Handsome is as handsome does ; (". break ; default: puts("Very clever. Try again. "); { return 0; { Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 64 King Mongkut's Institute of Technology
ตวอยางการใชคำสง switch-2 (2) 01001012 Principle of Computer Programming d d 1 A stitch in time saves nine. Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 2 6 default 9 Handsome is as handsome does. Very clever. Try again. 65 King Mongkut's Institute of Technology
ตวอยางการใชคำสง switch-3 (1) 01001012 Principle of Computer Programming #include<stdio. h> int main () } int d ; printf("Enter a number from 1 to 9 ; (" : scanf("%d& , "d ; ( switch (d ( } case 1: puts("A stitch in time saves nine ; (". break ; case 2: puts("Computer"); break; case 6: puts("Programming"); break; case 9: puts("Handsome is as handsome does ; (". break ; default: puts("Very clever. Try again. "); { return 0; { Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 66 King Mongkut's Institute of Technology
ตวอยางการใชคำสง switch-3 (2) 01001012 Principle of Computer Programming d d 1 2 6 9 A stitch in time saves nine. Computer Programming Handsome is as handsome does. Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ default Very clever. Try again. 67 King Mongkut's Institute of Technology
ตวอยางการใชคำสง switch-4 (1) 01001012 Principle of Computer Programming #include<stdio. h> int main () } int d ; printf("Enter a number from 1 to 9 ; (" : scanf("%d& , "d ; ( switch (d ( } case 1: puts("A stitch in time saves nine ; (". break ; case 2: puts("Computer"); case 6: puts("Programming"); case 9: puts("Handsome is as handsome does ; (". break ; default: puts("Very clever. Try again. "); { return 0; { Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 68 King Mongkut's Institute of Technology
ตวอยางการใชคำสง switch-4 (2) d 01001012 Principle of Computer Programming d 1 default 2 Computer 6 9 Programming A stitch in time saves nine. Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ Handsome is as handsome does. Very clever. Try again. 69 King Mongkut's Institute of Technology
จงเขยนผงงานจากโปรแกรมตอไปน 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 01001012 Principle of Computer Programming #include<stdio. h> int main () } float gpa ; printf("Enter GPA ; (" : scanf("%f& , "gpa); if (gpa > 4) printf ("Error GPA inputn"); printf ("Exit Program. "); else if (gpa >= 2) printf ("Your Status : Normal"); else ������ printf ("Your Status : Problem"); ���� return 0; { ������ Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 70 King Mongkut's Institute of Technology
โจทย 1 : จงเขยนผงงานจากโปรแกรมตอไปน 01001012 Principle of Computer Programming 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include<stdio. h> int main () } float gpa ; printf("Enter GPA ; (" : scanf("%f& , "gpa); if (gpa > 4) printf ("Error GPA inputn"); printf ("Exit Program. "); if (gpa >= 2) printf ("Your Status : Normal"); else printf ("Your Status : Problem"); return 0; { Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ 71 King Mongkut's Institute of Technology
โจทย 2 : โปรแกรมตรวจสอบสถานะการเรยน – ตวอยางการรนโปรแกรม Enter GPA (past) : 0. 74 Your Status : Retired Sorry, Try again. Enter GPA (past) : 1. 50 Your Status : Probation Enter GPS (present) : 2. 00 Enter GPA (present) : 1. 75 Your Status : Probation Enter GPA (past) : 1. 50 Your Status : Probation Enter GPS (present) : 3. 0 Enter GPA (present) : 2. 25 Your Status : Normal Jirasak Sittigorn http: //www. kmitl. ac. th/~ksjirasa/ (3) 01001012 Principle of Computer Programming Enter GPA (past) : 2. 50 Your Status : Normal Enter GPS (present) : 1. 00 Enter GPA (present) : 1. 75 Your Status : Probation Enter GPA (past) : 1. 50 Your Status : Probation Enter GPS (present) : 1. 00 Enter GPA (present) : 1. 25 Your Status : Retired Sorry, Try agin. 74 King Mongkut's Institute of Technology
- Slides: 74