lecture 12
TRANSCRIPT
![Page 1: Lecture 12](https://reader033.vdocuments.net/reader033/viewer/2022061123/54753d51b4af9f53558b456d/html5/thumbnails/1.jpg)
Flowcharts
Lecture 12
![Page 2: Lecture 12](https://reader033.vdocuments.net/reader033/viewer/2022061123/54753d51b4af9f53558b456d/html5/thumbnails/2.jpg)
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: Lecture 12](https://reader033.vdocuments.net/reader033/viewer/2022061123/54753d51b4af9f53558b456d/html5/thumbnails/3.jpg)
The three basic control structures
![Page 4: Lecture 12](https://reader033.vdocuments.net/reader033/viewer/2022061123/54753d51b4af9f53558b456d/html5/thumbnails/4.jpg)
1. Sequence
Statemement a
Statemement b
Statemement c
![Page 5: Lecture 12](https://reader033.vdocuments.net/reader033/viewer/2022061123/54753d51b4af9f53558b456d/html5/thumbnails/5.jpg)
2. Selection
Condition p?
Statemement a Statemement b
T F
![Page 6: Lecture 12](https://reader033.vdocuments.net/reader033/viewer/2022061123/54753d51b4af9f53558b456d/html5/thumbnails/6.jpg)
Selection (2)
Condition p?
Statemement a
T
F
![Page 7: Lecture 12](https://reader033.vdocuments.net/reader033/viewer/2022061123/54753d51b4af9f53558b456d/html5/thumbnails/7.jpg)
3. Repetition
Condition p?
Statemement block
T
F
![Page 8: Lecture 12](https://reader033.vdocuments.net/reader033/viewer/2022061123/54753d51b4af9f53558b456d/html5/thumbnails/8.jpg)
Example 12.1 Add three numbers
A program is required to read three numbers, add them together and print their total.
![Page 9: Lecture 12](https://reader033.vdocuments.net/reader033/viewer/2022061123/54753d51b4af9f53558b456d/html5/thumbnails/9.jpg)
• Defining diagram
Input Processing Output
Number1Number2Number3
Read three numbersAdd number togetherPrint total number
total
![Page 10: Lecture 12](https://reader033.vdocuments.net/reader033/viewer/2022061123/54753d51b4af9f53558b456d/html5/thumbnails/10.jpg)
Solution Algorithm
Add numbers to total
ReadNumber1Number2number3
Print total
Start
Stop
![Page 11: Lecture 12](https://reader033.vdocuments.net/reader033/viewer/2022061123/54753d51b4af9f53558b456d/html5/thumbnails/11.jpg)
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: Lecture 12](https://reader033.vdocuments.net/reader033/viewer/2022061123/54753d51b4af9f53558b456d/html5/thumbnails/12.jpg)
• Defining diagram
Input Processing OutputMax_tempMin_temp
Prompt for temperaturesGet temperaturesCalculate average temperatureDisplay average temperature
Avg_temp
![Page 13: Lecture 12](https://reader033.vdocuments.net/reader033/viewer/2022061123/54753d51b4af9f53558b456d/html5/thumbnails/13.jpg)
Solution Algorithm
![Page 14: Lecture 12](https://reader033.vdocuments.net/reader033/viewer/2022061123/54753d51b4af9f53558b456d/html5/thumbnails/14.jpg)
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: Lecture 12](https://reader033.vdocuments.net/reader033/viewer/2022061123/54753d51b4af9f53558b456d/html5/thumbnails/15.jpg)
• 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: Lecture 12](https://reader033.vdocuments.net/reader033/viewer/2022061123/54753d51b4af9f53558b456d/html5/thumbnails/16.jpg)
Solution Algorithm
![Page 17: Lecture 12](https://reader033.vdocuments.net/reader033/viewer/2022061123/54753d51b4af9f53558b456d/html5/thumbnails/17.jpg)
Flowchart and the selection control structure
![Page 18: Lecture 12](https://reader033.vdocuments.net/reader033/viewer/2022061123/54753d51b4af9f53558b456d/html5/thumbnails/18.jpg)
Simple IF statement
Account_balance <
$300?
Service_charge = $5 Service_charge = $2
T F
![Page 19: Lecture 12](https://reader033.vdocuments.net/reader033/viewer/2022061123/54753d51b4af9f53558b456d/html5/thumbnails/19.jpg)
Null ELSE statement
Student_attendance =
P/T?
Increment part_time_count
T
F
![Page 20: Lecture 12](https://reader033.vdocuments.net/reader033/viewer/2022061123/54753d51b4af9f53558b456d/html5/thumbnails/20.jpg)
Combined IF statement
Student = P/T ANDGender =
F ?
Increment Female_part_time_count
T
F
![Page 21: Lecture 12](https://reader033.vdocuments.net/reader033/viewer/2022061123/54753d51b4af9f53558b456d/html5/thumbnails/21.jpg)
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: Lecture 12](https://reader033.vdocuments.net/reader033/viewer/2022061123/54753d51b4af9f53558b456d/html5/thumbnails/22.jpg)
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: Lecture 12](https://reader033.vdocuments.net/reader033/viewer/2022061123/54753d51b4af9f53558b456d/html5/thumbnails/23.jpg)
• Defining diagram
Input Processing Output
Char_1Char_2Char_3
Prompt for charactersAccept three charactersSort three charactersOutput three characters
Char_1Char_2Char_3
![Page 24: Lecture 12](https://reader033.vdocuments.net/reader033/viewer/2022061123/54753d51b4af9f53558b456d/html5/thumbnails/24.jpg)
Solution Algorithm ?
![Page 25: Lecture 12](https://reader033.vdocuments.net/reader033/viewer/2022061123/54753d51b4af9f53558b456d/html5/thumbnails/25.jpg)
Case Structure
CaseOf
variable
Statement_a Statement_b Statement_c Statement_d
Value 1 Value 2 Value 3 Value 4
![Page 26: Lecture 12](https://reader033.vdocuments.net/reader033/viewer/2022061123/54753d51b4af9f53558b456d/html5/thumbnails/26.jpg)
![Page 27: Lecture 12](https://reader033.vdocuments.net/reader033/viewer/2022061123/54753d51b4af9f53558b456d/html5/thumbnails/27.jpg)
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: Lecture 12](https://reader033.vdocuments.net/reader033/viewer/2022061123/54753d51b4af9f53558b456d/html5/thumbnails/28.jpg)
• Defining diagram
Input Processing Output18 exam scores Prompt the scores
Get scoresCompute class averageDisplay scoresDisplay class average
18 exam scoresClass_average
![Page 29: Lecture 12](https://reader033.vdocuments.net/reader033/viewer/2022061123/54753d51b4af9f53558b456d/html5/thumbnails/29.jpg)
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: Lecture 12](https://reader033.vdocuments.net/reader033/viewer/2022061123/54753d51b4af9f53558b456d/html5/thumbnails/30.jpg)
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: Lecture 12](https://reader033.vdocuments.net/reader033/viewer/2022061123/54753d51b4af9f53558b456d/html5/thumbnails/31.jpg)
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: Lecture 12](https://reader033.vdocuments.net/reader033/viewer/2022061123/54753d51b4af9f53558b456d/html5/thumbnails/32.jpg)
• Defining diagram
Input Processing OutputChar_1Char_2Char_3
Prompt for charactersAccept three charactersSort three charactersOutput three characters
Char_1Char_2Char_3
![Page 33: Lecture 12](https://reader033.vdocuments.net/reader033/viewer/2022061123/54753d51b4af9f53558b456d/html5/thumbnails/33.jpg)
Hierarchy chart
Process_three_characters
Sort_three_characters
![Page 34: Lecture 12](https://reader033.vdocuments.net/reader033/viewer/2022061123/54753d51b4af9f53558b456d/html5/thumbnails/34.jpg)
Process_three_charactersStart
PromptFor
characters
Sort_Three_
characters
Outpurcharacters
Get characters
PromptFor
characters
Getcharacters
Stop
CharactersNOT = xxx ?F
T
![Page 35: Lecture 12](https://reader033.vdocuments.net/reader033/viewer/2022061123/54753d51b4af9f53558b456d/html5/thumbnails/35.jpg)
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: Lecture 12](https://reader033.vdocuments.net/reader033/viewer/2022061123/54753d51b4af9f53558b456d/html5/thumbnails/36.jpg)
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: Lecture 12](https://reader033.vdocuments.net/reader033/viewer/2022061123/54753d51b4af9f53558b456d/html5/thumbnails/37.jpg)
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: Lecture 12](https://reader033.vdocuments.net/reader033/viewer/2022061123/54753d51b4af9f53558b456d/html5/thumbnails/38.jpg)
• 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: Lecture 12](https://reader033.vdocuments.net/reader033/viewer/2022061123/54753d51b4af9f53558b456d/html5/thumbnails/39.jpg)
Hierarchy chart
Compute_employee_pay
Calculate_Employee_pay
Validate_input_fields
Read_employee_details
Employee_details
Print_employee_details
Valid_input_fields
![Page 40: Lecture 12](https://reader033.vdocuments.net/reader033/viewer/2022061123/54753d51b4af9f53558b456d/html5/thumbnails/40.jpg)
Solution Algorithm
![Page 41: Lecture 12](https://reader033.vdocuments.net/reader033/viewer/2022061123/54753d51b4af9f53558b456d/html5/thumbnails/41.jpg)
Compute_employee_pay
Start
Read_Employee_
details
MoreRecods?
Validate_Input:fields
Stop
FieldsValid?
Calculate_Employee_
pay
Print_Employee_
details
Read_Employee_
details
![Page 42: Lecture 12](https://reader033.vdocuments.net/reader033/viewer/2022061123/54753d51b4af9f53558b456d/html5/thumbnails/42.jpg)
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