36
Introduction to Computer Programming 1 Wimolsree Getsopon BIS.KKC.RMUTI

Computer Programming 1 - wcalab.comwcalab.com/course/wp-content/uploads/2015/01/IntroFlowchart.pdf · Pseudocode is a "text-based" detail (algorithmic) design tool. ... Flowchart

  • Upload
    vuongtu

  • View
    230

  • Download
    6

Embed Size (px)

Citation preview

Introduction to Computer Programming 1

Wimolsree Getsopon BIS.KKC.RMUTI

Topics

Program Development Cycle

Algorithm

Psuedocode

Flowchart

Understanding the Program Development Cycle

Understand the problem

Plan the logic

Write the code

Translate the code

Test Program

Put the program in production

Maintain the program

Algorithm

An algorithm (pronounced AL-go-rith-um) is a procedure or formula for solving a problem

1. เร่ิมต้นท างาน 2. รบัค่า ปี พ.ศ.เกิด (Year) 3. ค านวณอาย(ุAge) จากสตูร

Age = 2557 - Year 4. แสดงผลอาย(ุAge) 5. จบการท างาน

ตัวอย่าง

Using Pseudocode Statements and Flowchart Symbols • Pseudocode

is an artificial and informal language that helps programmers develop algorithms. Pseudocode is a "text-based" detail (algorithmic) design tool.

• Flowchart

A flowchart is a type of diagram that represents an algorithm or process, showing the steps as boxes of various kinds, and their order by connecting them with arrows

Psuedocode (Example1)

If student's grade is greater than or equal to 60

Print "passed"

else

Print "failed"

Psuedocode (Example2)

Begin Read Year Age = 2557-Year Write Age End

1. เร่ิมต้นท างาน 2. รบัค่า ปี พ.ศ.เกิด (Year) 3. ค านวณอาย(ุAge) จากสตูร

Age = 2557 - Year 4. แสดงผลอาย(ุAge) 5. จบการท างาน

Algorithm Psuedocode

Flowchart Symbols

สัญลกัษณ์ ความหมาย

จุดเร่ิมตน้ และ จุดส้ินสุด

การรับและแสดงผลขอ้มูล

การแสดงผลทางเคร่ืองพิมพ ์

การประมวลผล

Flowchart Symbols

สัญลกัษณ์ ความหมาย

จุดเร่ิมตน้ และ จุดส้ินสุด

การรับและแสดงผลขอ้มูล

การแสดงผลทางเคร่ืองพิมพ ์

การประมวลผล

Psuedocode Statement

Start

input myNumber

set myAnswer = myNumber * 2

output myAnswer

Stop

Flowchart (Example 1)

Start

input myNumber

set myAnswer = myNumber * 2

output myAnswer

Stop

Flowchart (Example 2)

Begin Read Year Age = 2557-Year Write Age End

Psuedocode Flowchart

START

Read Year

Age = 2557-Year

Write Age

STOP

Structure Programming

• Sequence Construct

• Selection Construct

• Iteration Construct

Sequence Construct

Flowchart

START

Read Year

Age = 2556-Year

Write Age

STOP

Selection Construct

Iteration Construct

Repeating Instructions

Start

input myNumber

set myAnswer = myNumber * 2

output myAnswer

input myNumber

set myAnswer = myNumber * 2

output myAnswer

input myNumber

set myAnswer = myNumber * 2

output myAnswer

….

Stop

Repeating Instructions

Don’t Do It This logic saves step, but it has a

fatal flaw – it never ends.

Infinite loop

Repeating Instructions

Flowchart (Example 3)

• จงเขียนขั้นตอนการคิดเกรด โดยมีเง่ือนไขดงัน้ี ถา้ไดค้ะแนนสอบ 50 คะแนนข้ึนไป ใหเ้กรดเป็น ‘Pass’ ถา้ไดค้ะแนนต ่ากวา่ 50 คะแนน ใหเ้กรดเป็น ‘Fail’ แสดงผลลพัธ์ของเกรด

Input : ?

Output : ?

คะแนนสอบ (Score)

เกรด (Grade)

Flowchart (Example 3)

1. เร่ิมต้นท างาน

2. รบัค่า คะแนนสอบ (Score)

3. ถ้าคะแนนสอบตัง้แต่ 50 คะแนนขึน้ไป ไปขัน้ตอนท่ี 5

4. ถ้าคะแนนสอบน้อยกว่า 50 คะแนน ไปขัน้ตอนท่ี 6

5. ก าหนดให้ Grade = ‘Pass’ ไปขัน้ตอนท่ี 7

6. ก าหนดให้ Grade = ‘Fail’ ไปขัน้ตอนท่ี 7

7. แสดงผล เกรด (Grade)

8. จบการท างาน

Algorithm

Flowchart (Example 3)

Begin Read Score IF Score >= 50 THEN Grade = ‘Pass’ ELSE Grade = ‘Fail’ END IF Write Grade End

Psuedocode

Flowchart (Example 3)

Begin Read Score IF Score >= 50 THEN Grade = ‘Pass’ ELSE Grade = ‘Fail’ END IF Write Grade End

START

Score>=50 Y

N

Read Score

STOP

Grade = ‘Pass’

Grade = ‘Fail’

Write Grade

Flowchart (Example 4)

• จงเขียนขั้นตอนเพื่อรับค่าน ้าหนกัของเพื่อนใน Section (จ านวน 80 คน) แลว้ค านวณหาน ้าหนกัเฉล่ีย

Input : ?

Output : ?

ค่าน า้หนัก

ค่าน า้หนักเฉล่ีย

Flowchart (Example 4)

• Algorithm

1. เร่ิมต้นท างาน 2. ก าหนดค่าเร่ิมต้นให้

i = 1 (ส าหรบันับจ านวนคน) Sum = 0 (ส าหรบัหาค่าผลรวม)

3. ในขณะท่ี i <=80 ให้ • รบัน ้าหนัก(Weight) • หาค่าผลรวมสะสมจากสตูร

Sum = Sum+ Weight • เพ่ิมค่าตวัแปรส าหรบันับจ านวนคนอีก 1 จากสตูร i=i+1

4. หาค่าเฉล่ียจากสตูร Avg = Sum / 80 5. แสดงผลน ้าหนักเฉล่ีย (Avg) 6. จบการท างาน

Flowchart (Example 4) START

i = 1 , sum = 0

i <= 80

Read weight

Yes

sum = sum + weight i = i + 1

avg = sum / 80

No

Write avg

STOP

Try It

เมือ่ก ำหนดใหร้ับค่ำของ X= 100, Y=200

เมือ่ก ำหนดใหร้ับค่ำของ X= 10, Y=20

เมือ่ตอ้งกำรผลลพัธท์ี่แสดงเท่ำกบั 240 เมือ่ก ำหนดให ้X=15

ประโยชน์ของผงังาน

• ล าดบัขัน้ตอนการท างานของโปรแกรม และสามารถน าไปเขียนโปรแกรมได้โดยไมส่บัสน

• ตรวจสอบความถกูต้อง และแก้ไขโปรแกรมได้ง่าย เม่ือเกิดข้อผิดพลาด

• การปรับปรุง เปลี่ยนแปลง แก้ไข ท าได้อยา่งสะดวกและรวดเร็ว

• ท าให้ผู้ อ่ืนสามารถศกึษาการท างานของโปรแกรมได้อยา่งง่าย และรวดเร็วมากขึน้

Understanding Programming and User Environment

• A plain text editor

• Notepad

• Microsoft Visual Studio IDE

Understanding Programming and User Environment

• Command Line

• Graphic User Interface (GUI)

Understanding the Evolution of Programing Models • Procedural Programming

• top down design

• create functions to do small tasks

• communicate by parameters and return values

• Object-Oriented Programming

• design and represent objects

• determine relationships between objects

• determine attributes each object has

• determine behaviours each object will respond to

Exercise 1

• จงเขียนโฟล์วชาร์ต ในการค านวณหายอดช าระทั้งสิ้นและของที่ระลึกท่ีได้รับของระบบซื้อบัตรคอนเสิร์ต • บัตรคอนเสิร์ตใบละ 1,000 บาท • ส่วนลดค านวณได้จากเงื่อนไขต่อไปนี้ • ซื้อบัตรไม่ถึง 5 ใบ ไม่ได้รับส่วนลด • ซื้อบัตรตั้งแต ่5 - 10 ใบ จะได้รับส่วนลดใบละ 5% • ซื้อบัตรตั้งแต ่ 11 ใบขึ้นไป จะได้รับส่วนลดใบละ 10%

• ส่วนของการแจกของที่ระลึก มีเกณฑ์ดังนี้ 1. ถ้ายอดซ้ือ 5,000 บาทขึ้นไปจะได้รับเสื้อยืด 2 ตัว 2. ถ้ายอดซ้ือตั้งแต่ 10,000 บาท ขึ้นไปจะได้รับซีดีคอนเสิร์ต 5 ชุด

Exercise 2

• จงเขียนโฟล์วชาร์ต ในการค านวณหารายรับสทุธิประจ าเดอืนของพนักงานดงันี้

• เงินเดือนที่ไดร้ับต้องเสียภาษ ี 10% ของเงินเดือน

• เงินพิเศษค่าล่วงเวลาค านวณจากชั่วโมงการท างาน โดยหักชั่วโมงการท างานปกติ 40 ช.ม. ที่เหลือคือจ านวนชั่วโมงที่น ามาคิดค่าลว่งเวลา โดยมีเกณฑ์ดังนี้

1. ถ้าพนักงานมีเงินเดอืนไม่เกิน 10,000 บาท จะได้รับชั่วโมงละ 100 บาท

2. ถ้าพนักงานมีเงินเดอืนไม่ถึง 25,000 บาท จะได้รับชั่วโมงละ 300 บาท

3. ถ้าพนักงานมีเงินเดอืนตั้งแต่ 25,000 บาท จะได้รับชั่วโมงละ 500 บาท

• เงินสะสมโดยพนักงานต้องจ่ายเงนิสะสมเปน็จ านวนเงนิ 3% ของรายรับจริงทั้งหมด