flowcharts

Post on 21-Nov-2014

15 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

การออกแบบโปรแกรมอย่�างง�าย่โดย่การใช้� Flowchart

เนื้��อหาThe Three Basic Control Structures

1 Sequence2 Selection3 Repetition

Simple Algorithms That Use The Sequence Control Structure Example 3.1 Add Three numbersA defining diagramB Solution algorithmExample 3.2 Find average temperatureA Defining diagramB Solution algorithmExample 3.3 Calculate mowing timeA Defining diagramB Solution algorithm

Flowcharts and the Selection Control Structure Simple IF statementNull ELSE statementCombined IF statementNested IF statement

Simple Alogrithms That Use the Selection Control Structure Example 4.1 Read three charactersA Defining diagramB Solution algorithmExample 4.2 Process customer recordA Defining diagramB Solution algorithmExample 4.3 Calculate employee's payA Defining diagramB Solution algorithm

The CASE Structure Expressed as a Flowchart Example 4.4 Process customer recordA Defining diagramB Solution Algorithm

Flowchart and the Repetition Control StructureSimple Algorithms That Use the Repetition Control Structure

Example 5.1 Fahrenheit-Celsius conversionA Defining diagramB Solution algorithmExample 5.2 Print examination scoresA Defining diagramB Solution alogorithmExample 5.3 Process student enrolmentsA Defining diagramB Solution algorithmExample 5.4 Process inventory itemsA Defining diagramB Solution algorithm

Flowcharts and Modules Example 8.1 Read three charactersA Defining diagramB Hierarchy chartC Solution algorithm using a predefined process symbol

Flowcharts

Flowcharts are a visual outlining tool. They can be used to represent an algorithm. The flowing five symbols are enough to flowchart an algorithm.

The Three Basic Control Structures

Our programs are made up of the three basic constructures of: sequence, selection, and repetition.

1 Sequence

The sequence control structure is defined as the straight forward execution of one procesing step after another. Here is the general form of a sequence.

Here is a typical sequence of statements.

ประโย่คเลื�อก (Selection)

The selection control structure can be defined as the presentation of a condition, and the choice between two actions depending on whether the condition is true or false. Here is the general form of the selection construct.

Here is a typical selection statement.

Here is a selection with a empty false side.

การทำ�าซ้ำ��า (Repetition)

The repetition control structure can be defined as the presentation of a set of instructions to be performed repeatly, as long as a condition is true. Here is the general form of repetition.

Here is a repetition control structure with typical statements.

Simple Algorithms That Use the Sequence Control Structure

Here are the examples from chapter 3 using flowcharts.

ตั�วอย่�าง หาผลรวมของเลข 3 จำ�านวน

A program is required to read three numbers, add them together and print their total.

A Defining diagram

Here is the IPO (Input, Process, Output) chart.

Input Processing Output

number_1number_2number_3

อ�านื้ตั�วเลืขทำ��ง 3 จำ�านื้วนื้ แลื�วนื้�ามาบวกเข�าด�วย่ก�นื้จำากนื้��นื้พิ"มพิ#ออกจำอภาพิ

ผลืรวม

B Solution algorithm

Here is the algorithm as a flowchart.

Example 3.2 Find average temperature

A program is to get the maximum and minimum temperatures for a day and calculate and display the average temperature.

A Defining diagram

Here is the IPO chart.

B Solution algorithm

Here is the algorithm as a flowchart.

Example 3.3 Calculate mowing time

A program is required to read in the length and width of a lot, and the length and width of the rectangular house that has been built on the lot. Then it computes and displys the mowing time the lot.

A Defining diagram

Here is the IPO chart.

B Solution algorithm

Here is the algorithm as a flowchart.

Flowcharts and the Selection Control Structure

Here are the examples from chapter 4 using flowcharts.

โครงสร�าง IF อย่�างง�าย่

Simple selection occurs when a choice is made between two alternative paths.

Null ELSE statement

The null ELSE structure is a variation of the simple IF structure with the false side being empty.

Combined IF statement

A combined IF statement is one that contains multiple conditions in the decision symbol.

Nested IF statement

A nested IF statement has another IF in either the true or false branch.

Simple Algorithms That Use the Selection Control Structure

Finally here are the examples from chapter 4 rewritten as flowcharts.

Example 4.1 Read three characters

An algorithm prompts the operator for three characters, gets the characters, sorts them and ouputs them in ascending order.

A Defining diagram

Here is the IPO chart.

B Solution algorithm

Here is the algorithm as a flowchart.

Example 4.2 Process customer record

A program reads a customer's name, purchase amount, and a tax code. The tax code is validated, the sales tax is computed along with the toal. very thing is printed out.

A Defining diagram

Here is the IPO chart.

B Solution algorithm

Here is the algorithm as a flowchart.

Example 4.3 Calculate employee's pay

A program is to read payroll records, compute an employee's pay, and print out everything.

A Defining diagram

Here is the IPO chart for the program.

B Solution algorithm

Here is the solution algorithm as a flowchart.

The CASE Structure Expressed as a Flowchart

The CASE statement is great for a test that has many acceptable answers. Here is one way to represent a CASE statement in a flowchart.

Example 4.4 Process customer record

A program is to read customer records, validate the tax code, compute the sales tax and total and print out everything but the tax code.

A Defining diagram

Here is the IPO chart.

B Solution algorithm

Here is the solution algorithm as a flowchart.

Flowcharts and the Repetition Control Structure

Repetition is used to repeat a group of instructions 0 or more times. It makes for economic programming. Here is the DOWHILE in a flowchart.

Simple Algorithms That Use the Repetition Control Structure

Here are some algorithms from chapter 5 rewritten as flowcharts.

Example 5.1 Fahrenheit-Celsius conversion

A program is to get 15 Fahrenheit temperatures and convert them to Celsius and display them with a "all temperatures processed" message.

A Defining diagram

Here is the IPO chart.

B Solution algorithm

Here is the solution algorithm rewritten as a flowchart.

Example 5.2 Print examination scores

A program is to read and print a series of student's names and exams scores. Then it is to print the average score..

A Defining diagram

Here is the IPO chart.

B Solution algorithm

Here is the Solution algorithm as a flowchart.

Example 5.3 Process student enrolments

A program is to read a student records file and print out only the records of students taking Programming I course.

A Defining diagram

Here is the IPO chart.

B Solution algorithm

Here is the Solution algorithm as a flowchart.

Example 5.4 Process inventory items

The program is to read an inventory file and produce a low stock items report.

A Defining diagram

Here is the IPO chart

B Solution algorithm

Here is the Solution algorithm as a flowchart.

Flowcharts and Modules

Flowcharts can use the predefined symbol to break a program up into modules

Example 8.1 Read three characters

Here is the read three characters and print them out in ascending order program.

A Defining diagram

Here is the IPO chart.

B Hierarchy chart

Here is a possible hierarchy chart.

C Solution algorithm using a predefined process symbol

Here is the Solution algorithm using flowchats.

แบบฝึ)กห�ดว"เคราะห#อ�ลืกอร"ทำ*ม

จำากโจำทย่�ท��ก�าหนดให� จำงว�เคราะห�อ�ลกอร�ท�มโดย่ใช้� Flowchart และแปลงเป#น pseudo

code และท�าเป#นเอกสารส�ง โดย่การเข�ย่นด�วย่ลาย่ม%อเข�ย่น

Alternate between these two methods by doing the flow chart first on one problem, then write the pseudocode for that problem. For the next problem, start with a pseudocode solution, then go to the flow chart.

Once you have completed a couple of problems, take time to have another learner review your work while you review theirs.

Remember - small details are important here!

1) Count to 100 2) Count to 100 by 2's 3) Count to 100 by 3's 4) Add the numbers from 1 to 100 5) Add up 10 numbers that are given to you - that is, you don't know what they are until someone gives

them to you.... 6) Find the average of 10 numbers given to you 7) Find the average, high number, low number, sum of those 10 numbers 8) Display three numbers in order from smallest to largest (the numbers are given to you) 9) Display three words in order (the words are given to you) 10) Display 10 numbers in order 11) Display 10 words in order 12) Find the batting average of your favorite baseball/softball player 13) Display 10 baseball/softball players in order of their batting average (high to low) 14) Find the area of a rectangle (two sides are given to you) 15) Find the area of a triangle 16) Find the area of a circle 17) Find the area of a snowman (3 circles - we'll stay 2 dimensional here!) 18) Find the area of a house (a rectangle and a triangle) 19) Find the area of a rectangle with a triangular hole in it 20) Modify your area solutions to check for values given to you that are less than zero and ask for a new

number 21) Ask a person for a number between 1 and 100, ask again if they give you a number outside that range 22) Swap the contents of two glasses of water - what will you need to do? 23) Calculate a student's grade point average - A = 4, B=3, C=2 and D = 1 24) Find the factorial of a set of numbers (multiply from 1 to n) 25) Find the sum of a set of numbers (add from 1 to n)

top related