4 Programming Concepts BC 320 Introduction to Computer
บทท 4 แนวคดการเขยนโปรแกรม (Programming Concepts) BC 320 Introduction to Computer Programming 1
โครงสรางโปรแกรม l สวนประกาศ l สวนประโยคคำ สง #include<stdio. h> #include<conio. h> void main( ) { printf(“Hellon”); printf(“Welcome to BC 320”); getch( ); } BC 320 Introduction to Computer Programming 8
. 1วเคราะหปญหา แยกปญหาออกเปน 3 สวน 1. อนพต (input) 2) เอาทพต (output) 3) กระบวนการ (processing( Input Process Read: Output Print: BC 320 Introduction to Computer Programming 10
นคา และราคาสนคา เพอคำนวณและแสดงผลจำนวนเงนทตองชำร ะ Input Process Output Read: Amt Cost Price = Amt * Cost Print: Price BC 320 Introduction to Computer Programming 16
Algorithm (เฉลย( 1. Output: ผลการสอบ(Result( 2. Input: คะแนน(Score( 3. Process 3. 1 ���������� Result เปนคาวาง 3. 2 รบคา Score 3. 3 ตรวจสอบคา Score 3. 3. 1ถา Score>=60 ให Result=‘S’ 3. 3. 2ถา Score<=60 ให Result=‘U’ 3. 4 ���� Result 3. 5 ����� BC 320 Introduction to Computer Programming 23
ตวอยางท 4. 4 จงหาผลบวกของเลขคตงแต Input Read: A 1 ถง 100 Process While A <= 100 do Sum = Sum+A A = A +2 end-while BC 320 Introduction to Computer Programming Output Print: Sum 24
. 5 คำสงทใชเปรยบเทยบหรอทำงานตาม เงอนไข จะเขยนในรปแบบของ IF Clause ตวอยางของรหสเทยมเชน IF Number > 0 THEN Positive = Positive + 1 ELSE IF Number < 0 THEN Negative = Negative + 1 END IF BC 320 Introduction to Computer Programming 32
. 6คำสงเกยวกบการทำงานซำ ในกรณทมการทำงานซำมากกวา 1 รอบ สามารถเขยนได 2 รปแบบ คอ FOR REPEAT_UNTIL หรอ DO WHILE ตวอยาง เชน DO WHILE Total < 100 Read Employee Record Print Name, Age Add 1 to Total END DO BC 320 Introduction to Computer Programming 33
ตวอยางท 4. 6 จงเขยน Pseudo code เพอตรวจสอบและแสดงผลตวเลขทรบทางจอ ภาพวาเปนเลข + - หรอ 0 1. Output: ผลการตรวจสอบ (Issue(. 2 Input: ตวเลข (N(. 3 รหสเทยม Begin Set Issue to null Read N IF N>0 THEN Set Issue to ‘Positive’ ELSE IF N<0 THEN Set Issue to ‘Negative’ ELSE Set Issue to ‘Zero’ End BC 320 Introduction to Computer Programming 35
เฉลย. 1 Output : ผลการสอบ (Result(. 2 Input : คะแนน (Score(. 3 Pseudo Code Begin Set Result to null Read Score IF Score >= 60 THEN Set Result to ‘S’ ELSE Set Result to ‘U’ END IF Write Result End BC 320 Introduction to Computer Programming 38
การบาน บทท 4 หนา 70 BC 320 Introduction to Computer Programming 49
- Slides: 49