View
223
Download
0
Category
Tags:
Preview:
Citation preview
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:
The three basic control structures
1. Sequence
Statemement a
Statemement b
Statemement c
2. Selection
Condition p?
Statemement a Statemement b
T F
Selection (2)
Condition p?
Statemement a
T
F
3. Repetition
Condition p?
Statemement block
T
F
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
Number1Number2Number3
Read three numbersAdd number togetherPrint total number
total
Solution Algorithm
Add numbers to total
ReadNumber1Number2number3
Print total
Start
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_tempMin_temp
Prompt for temperaturesGet temperaturesCalculate average temperatureDisplay average temperature
Avg_temp
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_lenghtBlock_widthHouse_lenghtHouse_width
Prompt for block measurementsGet block measurementsPrompt for house measurementsGet house measurementsCalculate mowing areaCalculate mowing time
Mowing_time
Solution Algorithm
Flowchart and the selection control structure
Simple IF statement
Account_balance <
$300?
Service_charge = $5 Service_charge = $2
T F
Null ELSE statement
Student_attendance =
P/T?
Increment part_time_count
T
F
Combined IF statement
Student = P/T ANDGender =
F ?
Increment Female_part_time_count
T
F
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
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_1Char_2Char_3
Prompt for charactersAccept three charactersSort three charactersOutput three characters
Char_1Char_2Char_3
Solution Algorithm ?
Case Structure
CaseOf
variable
Statement_a Statement_b Statement_c Statement_d
Value 1 Value 2 Value 3 Value 4
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 scoresGet scoresCompute class averageDisplay scoresDisplay class average
18 exam scoresClass_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
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
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_1Char_2Char_3
Prompt for charactersAccept three charactersSort three charactersOutput three characters
Char_1Char_2Char_3
Hierarchy chart
Process_three_characters
Sort_three_characters
Process_three_charactersStart
PromptFor
characters
Sort_Three_
characters
Outpurcharacters
Get characters
PromptFor
characters
Getcharacters
Stop
CharactersNOT = xxx ?F
T
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
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 OutputEmp_noPay_rateHrs_worked
Read employee detailsValidate input fieldsCalculate employee payPrint employee details
Emp_noPay_rateHrs_workedEmp_weekly_payError_message
Hierarchy chart
Compute_employee_pay
Calculate_Employee_pay
Validate_input_fields
Read_employee_details
Employee_details
Print_employee_details
Valid_input_fields
Solution Algorithm
Compute_employee_pay
Start
Read_Employee_
details
MoreRecods?
Validate_Input:fields
Stop
FieldsValid?
Calculate_Employee_
pay
Print_Employee_
details
Read_Employee_
details
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
Recommended