cp1020 - week 4 making decisions. cp1020 ©university of wolverhampton - ian coulson & steve...
Post on 20-Dec-2015
221 views
TRANSCRIPT
![Page 1: CP1020 - Week 4 Making Decisions. CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Decisions Example: Driving to a lecture you notice](https://reader035.vdocument.in/reader035/viewer/2022062304/56649d435503460f94a1f49c/html5/thumbnails/1.jpg)
CP1020 - Week 4
Making Decisions
![Page 2: CP1020 - Week 4 Making Decisions. CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Decisions Example: Driving to a lecture you notice](https://reader035.vdocument.in/reader035/viewer/2022062304/56649d435503460f94a1f49c/html5/thumbnails/2.jpg)
CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner
DecisionsExample:
Driving to a lecture you notice that you do not have much petrol left. You will need to fill up soon, and approaching you can see a petrol station. The price is reasonable, but you do not have a lot of time to spare, so don't want to have to queue to fill-up.
What would you do?
?
![Page 3: CP1020 - Week 4 Making Decisions. CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Decisions Example: Driving to a lecture you notice](https://reader035.vdocument.in/reader035/viewer/2022062304/56649d435503460f94a1f49c/html5/thumbnails/3.jpg)
CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner
Decisions in Problem Solving
No!
Yes
"If the queue at the petrol station is short then I will stop there to fill up"
If (the queue at the petrol station is short)Then stop there and fill up
Decisions ...decisions..
![Page 4: CP1020 - Week 4 Making Decisions. CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Decisions Example: Driving to a lecture you notice](https://reader035.vdocument.in/reader035/viewer/2022062304/56649d435503460f94a1f49c/html5/thumbnails/4.jpg)
CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner
You may decide:
"if the queue at the petrol station is short then I will stop there to fill up".
We could write the algorithm for this decision as:
If queue at petrol station is short then
stop there and fill up
![Page 5: CP1020 - Week 4 Making Decisions. CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Decisions Example: Driving to a lecture you notice](https://reader035.vdocument.in/reader035/viewer/2022062304/56649d435503460f94a1f49c/html5/thumbnails/5.jpg)
CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner
Further Examples
if kettle has boiled then
make tea
if temperature less than 18 C. then
turn on central heating
![Page 6: CP1020 - Week 4 Making Decisions. CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Decisions Example: Driving to a lecture you notice](https://reader035.vdocument.in/reader035/viewer/2022062304/56649d435503460f94a1f49c/html5/thumbnails/6.jpg)
CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner
General form of IF statements
IF condition THEN
<action>
ENDIF
IF, THEN and ENDIF are RESERVED words
condition is the “test”, if the answer is YES then we carry out the <action>
![Page 7: CP1020 - Week 4 Making Decisions. CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Decisions Example: Driving to a lecture you notice](https://reader035.vdocument.in/reader035/viewer/2022062304/56649d435503460f94a1f49c/html5/thumbnails/7.jpg)
CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner
An example program
REM program : to demonstrate the IF statement
REM written by : S. Garner
REM date written 8/3/00
DIM iAge AS INTEGER
CLS ' clear the screen
INPUT "Please enter your age "; iAge
REM test the condition
IF iAge > 17 THEN
PRINT "You may vote at the next election"
END IF
END
![Page 8: CP1020 - Week 4 Making Decisions. CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Decisions Example: Driving to a lecture you notice](https://reader035.vdocument.in/reader035/viewer/2022062304/56649d435503460f94a1f49c/html5/thumbnails/8.jpg)
CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner
Two way decisions
We frequently need to do either one thing or another, depending on some condition
If age is greater than 65 then
retire gracefully
else
keep working
![Page 9: CP1020 - Week 4 Making Decisions. CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Decisions Example: Driving to a lecture you notice](https://reader035.vdocument.in/reader035/viewer/2022062304/56649d435503460f94a1f49c/html5/thumbnails/9.jpg)
CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner
Basic IF..THEN..ELSE
IF condition THEN
<action1>
ELSE
<action2>
ENDIF
![Page 10: CP1020 - Week 4 Making Decisions. CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Decisions Example: Driving to a lecture you notice](https://reader035.vdocument.in/reader035/viewer/2022062304/56649d435503460f94a1f49c/html5/thumbnails/10.jpg)
CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner
Example program
REM program : to demonstrate the IF statement
DIM iMark AS INTEGER
CLS ' clear the screen
INPUT "Please enter your mark(0-100) "; iMark
REM check mark for pass or fail
IF iMark < 40 THEN
PRINT "You have failed"
ELSE
PRINT "You have passed"
PRINT "Well Done!"
END IF
END
![Page 11: CP1020 - Week 4 Making Decisions. CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Decisions Example: Driving to a lecture you notice](https://reader035.vdocument.in/reader035/viewer/2022062304/56649d435503460f94a1f49c/html5/thumbnails/11.jpg)
CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner
Testing
We now have more than one possible “route” through our code
We must TEST each of these!
We should also test the “boundary”
![Page 12: CP1020 - Week 4 Making Decisions. CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Decisions Example: Driving to a lecture you notice](https://reader035.vdocument.in/reader035/viewer/2022062304/56649d435503460f94a1f49c/html5/thumbnails/12.jpg)
CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner
Test Data
Mark Expected Actual
Result Result
25 You have failed You have failed
60 You have passed You have passed
40 You have passed You have passed
39 You have failed You have failed
![Page 13: CP1020 - Week 4 Making Decisions. CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Decisions Example: Driving to a lecture you notice](https://reader035.vdocument.in/reader035/viewer/2022062304/56649d435503460f94a1f49c/html5/thumbnails/13.jpg)
CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner
The condition statement
Usually we check a value.
The symbols used are:
= equal <> not equal
< less than <= less than or equal
> greater than >= Greater than or equal
![Page 14: CP1020 - Week 4 Making Decisions. CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Decisions Example: Driving to a lecture you notice](https://reader035.vdocument.in/reader035/viewer/2022062304/56649d435503460f94a1f49c/html5/thumbnails/14.jpg)
CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner
Example Conditions
NOTE: Brackets help to clarify!
(iMark < 0)
(iAge >=18)
(iValueA = iValueB)
(iAge >= 16 AND iAge < 65)
![Page 15: CP1020 - Week 4 Making Decisions. CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Decisions Example: Driving to a lecture you notice](https://reader035.vdocument.in/reader035/viewer/2022062304/56649d435503460f94a1f49c/html5/thumbnails/15.jpg)
CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner
More Complex Decisions 1
Problem:
You are looking for new employees for your company.One of the criteria is that the employee must be no younger than 16 and no older than 65.
Note: We have two conditions to satisfy: condition 1 is the candidate at least 16 years old? condition 2 is the candidate no older than 65?
Both condition 1 AND condition 2 must be satisfied "TRUE" in order to accept the candidate
![Page 16: CP1020 - Week 4 Making Decisions. CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Decisions Example: Driving to a lecture you notice](https://reader035.vdocument.in/reader035/viewer/2022062304/56649d435503460f94a1f49c/html5/thumbnails/16.jpg)
CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner
More Complex Decisions 2
Algorithm:
Step1 Get age of candidate2 If ( age at least 16) AND (age less than
65 )2.1 Then candidate is eligible2.2 Else reject candidate
AND implies that both conditions must be true
![Page 17: CP1020 - Week 4 Making Decisions. CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Decisions Example: Driving to a lecture you notice](https://reader035.vdocument.in/reader035/viewer/2022062304/56649d435503460f94a1f49c/html5/thumbnails/17.jpg)
CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner
The code
INPUT “How old is the candidate > ”; iCandidatesAge
If (iCandidatesAge >= 16) And (iCandidatesAge <= 65) Then Print “You are eligible to apply”
ElsePrint “You are outside the age range!”
End If
![Page 18: CP1020 - Week 4 Making Decisions. CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Decisions Example: Driving to a lecture you notice](https://reader035.vdocument.in/reader035/viewer/2022062304/56649d435503460f94a1f49c/html5/thumbnails/18.jpg)
CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner
The OR condition
Alternatively test for ineligible candidates:
Step1 Get age of candidate2 If ( age less than 16) OR (age greater than 65 )
2.1 Then reject candidate2.2 Else candidate is eligible
OR implies that either one (or both) of the conditions needs to be satisfied
![Page 19: CP1020 - Week 4 Making Decisions. CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Decisions Example: Driving to a lecture you notice](https://reader035.vdocument.in/reader035/viewer/2022062304/56649d435503460f94a1f49c/html5/thumbnails/19.jpg)
CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner
Another Example
REM program : to demonstrate the IF statement
REM written by : I Coulson
REM date written: 8/3/00
DIM iCandAge AS INTEGER
CLS ' clear the screen
INPUT "Please enter your age "; iCandAge
IF ( iCandAge < 16) OR ( iCandAge > 65) THEN
PRINT ”Sorry you are outside the age range"
ELSE
PRINT “You are eligible to apply”
END IF
END
![Page 20: CP1020 - Week 4 Making Decisions. CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Decisions Example: Driving to a lecture you notice](https://reader035.vdocument.in/reader035/viewer/2022062304/56649d435503460f94a1f49c/html5/thumbnails/20.jpg)
CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner
Problem - Student Grades
When a piece of work is marked, it is given a percentage mark which needs converting to a FAIL, PASS, MERIT or DISTINCTION.
A Fail Upto 40 A Pass 40 - 59 A Merit 60 - 79 A Distinction 80 +
![Page 21: CP1020 - Week 4 Making Decisions. CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Decisions Example: Driving to a lecture you notice](https://reader035.vdocument.in/reader035/viewer/2022062304/56649d435503460f94a1f49c/html5/thumbnails/21.jpg)
CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner
Mark to Grade ConversionAlgorithm
Algorithm:Step1 Get a student's mark
2 If (mark is greater than 0) AND (mark less than 40)2.1 Then Grade is Fail
3 If (mark greater than or equal to 40) AND (mark is less than 60)3.1 Then Grade is Pass
4 If (mark is greater or equal to 60) AND (mark less than 80)4.1 Then Grade is Merit
5 If (mark is greater or equal to 80) AND (mark is no more than 100)5.1 Then Grade is Distinction
6 Display Grade
![Page 22: CP1020 - Week 4 Making Decisions. CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Decisions Example: Driving to a lecture you notice](https://reader035.vdocument.in/reader035/viewer/2022062304/56649d435503460f94a1f49c/html5/thumbnails/22.jpg)
CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner
Mark to Grade Conversion Improved Algorithm Further improved algorithm:
Step1 Get a student's mark
2 If (mark is less than 40%)2.1 Then Grade is Fail2.2 Else If (mark is less than 60%)
2.2.1 Then Grade is Pass2.2.2 Else If (mark is less than 80%)
2.2.2.1 Then Grade is Merit2.2.2.2 Else Grade is Distinction
3 Display Grade
This is known as nesting decisions
![Page 23: CP1020 - Week 4 Making Decisions. CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Decisions Example: Driving to a lecture you notice](https://reader035.vdocument.in/reader035/viewer/2022062304/56649d435503460f94a1f49c/html5/thumbnails/23.jpg)
CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner
The Program
Rem Author I Coulson
DIM iPercentage AS INTEGER
INPUT ”What percentage did you get "; iPercentage
IF iPercentage < 40 THEN
Print “Fail”
ELSEIF iPercentage < 60 THEN
Print “Pass”
ELSEIF iPercentage < 80 THEN
Print “Merit”
ELSEIF iPercentage >= 80 THEN
Print “Distinction”
END IF
![Page 24: CP1020 - Week 4 Making Decisions. CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Decisions Example: Driving to a lecture you notice](https://reader035.vdocument.in/reader035/viewer/2022062304/56649d435503460f94a1f49c/html5/thumbnails/24.jpg)
CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner
Questions
1 Write an algorithm to decide if a salesman should get a bonus - he needs to have sold at least £3000 worth of goods in the month.
2 Alter the algorithm such that that the salesman earns 15% commission on all sales if sells more than £3000 worth of goods in a month, but only 5% if he sells less than that.
3 Write the code to print the appropriate comment to a runner finishing a race:
1st place - “well done you are the winner”
2nd place - “congratulations you are runner up”
3rd place - “good, you have finished third”
unplaced - “You’ve finished, well done”
![Page 25: CP1020 - Week 4 Making Decisions. CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner Decisions Example: Driving to a lecture you notice](https://reader035.vdocument.in/reader035/viewer/2022062304/56649d435503460f94a1f49c/html5/thumbnails/25.jpg)
CP1020 ©University of Wolverhampton - Ian Coulson & Steve Garner
End of lecture