42
Flowcharts Lecture 12

Flowcharts Lecture 12

Embed Size (px)

DESCRIPTION

Symbol Terminal Symbol: indicates the starting or stopping pointin the logic. Input/Output Symbol: Represents an input or output process in an algorithm Process Symbol: Represents any single process in an algorithm Predefined Process Symbol: Decision Symbol: Represents a decision in the logic involving the comparison Of two values.

Citation preview

Page 1: Flowcharts Lecture 12

Flowcharts

Lecture 12

Page 2: Flowcharts Lecture 12

Symbol

Terminal Symbol: indicates the starting or stopping pointin the logic.

Input/Output Symbol:Represents an input or output process in an algorithm

Process Symbol:Represents any single process in an algorithm

Decision Symbol:Represents a decision in the logic involving the comparisonOf two values.

Predefined Process Symbol:

Page 3: Flowcharts Lecture 12

The three basic control structures

Page 4: Flowcharts Lecture 12

1. Sequence

Statemement a

Statemement b

Statemement c

Page 5: Flowcharts Lecture 12

2. Selection

Condition p?

Statemement a Statemement b

T F

Page 6: Flowcharts Lecture 12

Selection (2)

Condition p?

Statemement a

T

F

Page 7: Flowcharts Lecture 12

3. Repetition

Condition p?

Statemement block

T

F

Page 8: Flowcharts Lecture 12

Example 12.1 Add three numbers

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

Page 9: Flowcharts Lecture 12

• Defining diagram

Input Processing Output

Number1Number2Number3

Read three numbersAdd number togetherPrint total number

total

Page 10: Flowcharts Lecture 12

Solution Algorithm

Add numbers to total

ReadNumber1Number2number3

Print total

Start

Stop

Page 11: Flowcharts Lecture 12

Example 12.2 Find average temperature

• A program is required to prompt the terminal operator for the maximum and minimum temperature readings on a particular day, accept those readings as integers, and calculate and display to the screen the average temperature, calculated by (maximum temperature + minimum temperature)/2.

Page 12: Flowcharts Lecture 12

• Defining diagram

Input Processing OutputMax_tempMin_temp

Prompt for temperaturesGet temperaturesCalculate average temperatureDisplay average temperature

Avg_temp

Page 13: Flowcharts Lecture 12

Solution Algorithm

Page 14: Flowcharts Lecture 12

Example 12.3 Compute mowing time

• A program is required to read from the screen the lenght and widht of a rectangular house block, and the lenght and width of the rectangular house that has been built on the block. The algorithm should then compute and display the mowing time required to cut the grass around the house, at the rate of two square metres per minute.

Page 15: Flowcharts Lecture 12

• Defining diagram

Input Processing OutputBlock_lenghtBlock_widthHouse_lenghtHouse_width

Prompt for block measurementsGet block measurementsPrompt for house measurementsGet house measurementsCalculate mowing areaCalculate mowing time

Mowing_time

Page 16: Flowcharts Lecture 12

Solution Algorithm

Page 17: Flowcharts Lecture 12

Flowchart and the selection control structure

Page 18: Flowcharts Lecture 12

Simple IF statement

Account_balance <

$300?

Service_charge = $5 Service_charge = $2

T F

Page 19: Flowcharts Lecture 12

Null ELSE statement

Student_attendance =

P/T?

Increment part_time_count

T

F

Page 20: Flowcharts Lecture 12

Combined IF statement

Student = P/T ANDGender =

F ?

Increment Female_part_time_count

T

F

Page 21: Flowcharts Lecture 12

Nested IF statement

IncrementCounter_A

Record

Code =`A‘ ?

IncrementCounter_B

IncrementCounter_C

IncrementError_counter

Record

Code =`A‘ ?

Record

Code =`A‘ ?

T

T

T

F

F

F

Page 22: Flowcharts Lecture 12

Example 12.4 Read three characters

• Design an algorithm that will prompt a terminal operator for three characters, accept those characters as input, sort them into ascending sequence and output them to the screen.

Page 23: Flowcharts Lecture 12

• Defining diagram

Input Processing Output

Char_1Char_2Char_3

Prompt for charactersAccept three charactersSort three charactersOutput three characters

Char_1Char_2Char_3

Page 24: Flowcharts Lecture 12

Solution Algorithm ?

Page 25: Flowcharts Lecture 12

Case Structure

CaseOf

variable

Statement_a Statement_b Statement_c Statement_d

Value 1 Value 2 Value 3 Value 4

Page 26: Flowcharts Lecture 12
Page 27: Flowcharts Lecture 12

Flowchart and Array

• Design a program that will prompt for and receive 18 examination scores from a mathematics test, compute the class average, and display all the scores and the class average to the screen.

Page 28: Flowcharts Lecture 12

• Defining diagram

Input Processing Output18 exam scores Prompt the scores

Get scoresCompute class averageDisplay scoresDisplay class average

18 exam scoresClass_average

Page 29: Flowcharts Lecture 12

Control Structures required

1. An array to store the exam scores – called ´scores´

2. An index to identify each element in the array

3. A DO loop to accept the scores4. Another DO loop to display the scores to

the screen.

Page 30: Flowcharts Lecture 12

Solution Algorithm

Start

Total_score = zero

I = 1

Add scores(I) to

Total score

I = I + 1

Calculate average

I = I + 1

Prompt and get

Scores (I)

I = 1

I <= 18 ?

DisplayScores (I)

I <= 18 ?

Displayaverage

Stop

T

F

T

F

Page 31: Flowcharts Lecture 12

Flowchart and Module

Design a solution algorithm that will prompt a terminal operator for three characters, accept those characters as input, sort them into ascending sequence and output them to the screen. The algorithm is to continue to read characters until ´XXX`is entered.

Page 32: Flowcharts Lecture 12

• Defining diagram

Input Processing OutputChar_1Char_2Char_3

Prompt for charactersAccept three charactersSort three charactersOutput three characters

Char_1Char_2Char_3

Page 33: Flowcharts Lecture 12

Hierarchy chart

Process_three_characters

Sort_three_characters

Page 34: Flowcharts Lecture 12

Process_three_charactersStart

PromptFor

characters

Sort_Three_

characters

Outpurcharacters

Get characters

PromptFor

characters

Getcharacters

Stop

CharactersNOT = xxx ?F

T

Page 35: Flowcharts Lecture 12

Sort_three_characters

Start

Char_2 >Char_3 ?

Char_1 >Char_2 ?

Char_1 >Char_2 ?

SwapChar_2,Char_3

SwapChar_1,Char_2

SwapChar_1,Char_2

Stop

T

F

T

T

F

F

Page 36: Flowcharts Lecture 12

Assignment: Calculate Employee‘s pay

A program is required by a company to read an employee‘s number, pay rate and the number of hours worked in a week. The program is then to validate the pay rate and the hours worked fields and, if valid, compute the employee‘s weekly pay and print it along with the input data.

Page 37: Flowcharts Lecture 12

Assignment (cont‘)Validation: According to the company‘s rules, the maximum

hours an emplooye can work per week is 60 hours, and the maximum hourly rate is $25.00 per hour. If the hours worked field or the hourly rate field is out of range, the input data and an appropriate message is to be printed and the emplooye‘s weekly pay is not to be calculated.

Weekly pay calculation: Weekly pay is calculated as hours worked times pay rate. If more than 35 hours are worked, payment for the overtime hours worked is calculated at time-and half.

Page 38: Flowcharts Lecture 12

• Defining diagram

Input Processing OutputEmp_noPay_rateHrs_worked

Read employee detailsValidate input fieldsCalculate employee payPrint employee details

Emp_noPay_rateHrs_workedEmp_weekly_payError_message

Page 39: Flowcharts Lecture 12

Hierarchy chart

Compute_employee_pay

Calculate_Employee_pay

Validate_input_fields

Read_employee_details

Employee_details

Print_employee_details

Valid_input_fields

Page 40: Flowcharts Lecture 12

Solution Algorithm

Page 41: Flowcharts Lecture 12

Compute_employee_pay

Start

Read_Employee_

details

MoreRecods?

Validate_Input:fields

Stop

FieldsValid?

Calculate_Employee_

pay

Print_Employee_

details

Read_Employee_

details

Page 42: Flowcharts Lecture 12

Validate_input_fields

Start

Valid_input_fields = T

Pay_rate > $ 25

PrintError

message

Valid_Input_

Fields = F

Hours_Worked

> 60

PrintError

message

Valid_Input_

Fields = T

Stop