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

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

Mar 08, 2018

Download

Documents

vuongtu
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Computer Programming 1 - wcalab.comwcalab.com/course/wp-content/uploads/2015/01/IntroFlowchart.pdf · Pseudocode is a "text-based" detail (algorithmic) design tool. ... Flowchart

Introduction to Computer Programming 1

Wimolsree Getsopon BIS.KKC.RMUTI

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

Topics

Program Development Cycle

Algorithm

Psuedocode

Flowchart

Page 3: Computer Programming 1 - wcalab.comwcalab.com/course/wp-content/uploads/2015/01/IntroFlowchart.pdf · Pseudocode is a "text-based" detail (algorithmic) design tool. ... 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

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

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. จบการท างาน

ตัวอย่าง

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

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

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

Psuedocode (Example1)

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

Print "passed"

else

Print "failed"

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

Psuedocode (Example2)

Begin Read Year Age = 2557-Year Write Age End

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

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

Algorithm Psuedocode

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

Flowchart Symbols

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

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

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

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

การประมวลผล

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

Flowchart Symbols

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

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

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

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

การประมวลผล

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

Psuedocode Statement

Start

input myNumber

set myAnswer = myNumber * 2

output myAnswer

Stop

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

Flowchart (Example 1)

Start

input myNumber

set myAnswer = myNumber * 2

output myAnswer

Stop

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

Flowchart (Example 2)

Begin Read Year Age = 2557-Year Write Age End

Psuedocode Flowchart

START

Read Year

Age = 2557-Year

Write Age

STOP

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

Structure Programming

• Sequence Construct

• Selection Construct

• Iteration Construct

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

Sequence Construct

Flowchart

START

Read Year

Age = 2556-Year

Write Age

STOP

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

Selection Construct

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

Iteration Construct

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

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

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

Repeating Instructions

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

fatal flaw – it never ends.

Infinite loop

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

Repeating Instructions

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

Flowchart (Example 3)

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

Input : ?

Output : ?

คะแนนสอบ (Score)

เกรด (Grade)

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

Flowchart (Example 3)

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

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

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

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

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

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

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

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

Algorithm

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

Flowchart (Example 3)

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

Psuedocode

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

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

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

Flowchart (Example 4)

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

Input : ?

Output : ?

ค่าน า้หนัก

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

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

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. จบการท างาน

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

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

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

Try It

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

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

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

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

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

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

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

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

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

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

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

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

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

Understanding Programming and User Environment

• A plain text editor

• Notepad

• Microsoft Visual Studio IDE

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

Understanding Programming and User Environment

• Command Line

• Graphic User Interface (GUI)

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

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

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

Exercise 1

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

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

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

Exercise 2

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

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

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

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

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

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

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