Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 42

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

Predefined Process Symbol:

Decision Symbol:
Represents a decision in the logic involving the comparison
Of two values.
The three basic control
structures
1. Sequence

Statemement a

Statemement b

Statemement c
2. Selection
T F
Condition p?

Statemement a Statemement b
Selection (2)

F
Condition p?

Statemement a
3. Repetition

F
Condition p?

Statemement block
Example 12.1 Add three numbers
 A program is required to read three numbers, add
them together and print their total.
• Defining diagram
Input Processing Output

Number1 Read three numbers total


Number2 Add number together
Number3 Print total number
Solution Algorithm
Start

Read
Number1
Number2
number3

Add numbers to total

Print total

Stop
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.
• Defining diagram

Input Processing Output


Max_temp Prompt for temperatures Avg_temp
Min_temp Get temperatures
Calculate average temperature
Display average temperature
Solution Algorithm
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.
• Defining diagram

Input Processing Output


Block_lenght Prompt for block measurements Mowing_time
Block_width Get block measurements
House_lenght Prompt for house measurements
House_width Get house measurements
Calculate mowing area
Calculate mowing time
Solution Algorithm
Flowchart and the selection
control structure
Simple IF statement
T F
Account_
balance <
$300?

Service_charge = $5 Service_charge = $2
Null ELSE statement

Student_ F
attendance =
P/T?

Increment
part_time_count
Combined IF statement

F
Student =
P/T AND
Gender =
F?

Increment
Female_part_time_count
Nested IF statement
T F
Record
Code =`A‘ ?

F
T
Record
Increment Code =`A‘ ?
Counter_A
F
Record
Code =`A‘ ?
Increment T
Counter_B

Increment
Increment Error_counter
Counter_C
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.
• Defining diagram

Input Processing Output

Char_1 Prompt for characters Char_1


Char_2 Accept three characters Char_2
Char_3 Sort three characters Char_3
Output three characters
Solution Algorithm ?
Case Structure
Case
Of
variable

Value 1 Value 2 Value 3 Value 4

Statement_a Statement_b Statement_c Statement_d


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.
• Defining diagram

Input Processing Output


18 exam scores Prompt the scores 18 exam scores
Get scores Class_average
Compute class average
Display scores
Display class average
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 scores
4. Another DO loop to display the scores to
the screen.
Solution Algorithm
Start

Calculate Display
Total_score = average
zero average

I=1
I=1

Stop

Prompt and
Display
get
Scores (I)
Scores (I)

Add scores(I)
to I=I+1
Total score

T
I=I+1 I <= 18 ?

F
T
I <= 18 ?

F
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.
• Defining diagram

Input Processing Output


Char_1 Prompt for characters Char_1
Char_2 Accept three characters Char_2
Char_3 Sort three characters Char_3
Output three characters
Hierarchy chart

Process_three_
characters

Sort_three_
characters
Process_three_characters
Start
Sort_
Three_
Prompt
characters
For
characters

Outpur
Get characters
characters

Prompt
Characters For
NOT = xxx ? characters
F
Stop
Get
T characters
Sort_three_characters
Start

T Char_1 >
Char_2 ?
Swap
F
Char_1,
Char_2

Char_2 >
Char_3 ?
T F
Swap
Char_2,
Char_3

Char_1 >
Char_2 ?
Swap
T F
Char_1,
Char_2

Stop
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.
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.
• Defining diagram

Input Processing Output


Emp_no Read employee details Emp_no
Pay_rate Validate input fields Pay_rate
Hrs_worked Calculate employee pay Hrs_worked
Print employee details Emp_weekly_pay
Error_message
Hierarchy chart

Compute_employee_pay

Employee_details

Valid_input_
fields

Read_employee_ Validate_input_ Calculate_ Print_employee_


details fields Employee_pay details
Solution Algorithm
Compute_employee_pay
Start Fields
Valid?
Read_
Employee_
details
Calculate_
Employee_
More pay
Recods?

Print_
Validate_ Stop Employee_
Input: details
fields

Read_
Employee_
details
Validate_input_fields
Start

Hours_
Valid_input_ Worked
fields = T > 60

Print
Error
Pay_rate message
> $ 25

Print
Error Valid_
message Input_
Fields = T

Valid_
Input_
Fields = F
Stop

You might also like