Upload
randell-thompson
View
223
Download
0
Embed Size (px)
Citation preview
Problem Solving and Program Planning
ICS4U – Computer Science
Overview
Problem SolvingProblem Solving Model
PlanningProblem-Definition TableInput-Processing-Output ChartPseudocodeFlowcharting
SamplesPracticeAssignment
Problem Solving
Solve the following problem:
A flower has been placed in a coloured box. Each box carries a statement. Of the three statements, at most, only one is true. Which box contains the flower?
If you solve it, think about how you got to your solution. Will visit the solution at the end.
REDThe flower is in
this box.
YELLOWThe flower is not
in this box.
GREENThe flower is not in the red box.
Problem Solving Model
Identify the problem
What is the current situation/problem?
What is the problem to be solved?
Analyze the problem
What do we know already about the situation/problem?
What are our assumptions?
What is the knowledge base of the person for whom we're trying to problem-solve? What can or do they understand?
What don't we know yet about the whole situation/problem?
Brainstorm ideas
List all possible ideas to solve the problem.
No value judgment is to be passed on any suggestions.
The more ideas, the better.
Prioritize ideas
Establish pros and cons of each suggestion in step 3.
Rank the suggested ideas from step 3 according to a set criteria.
List the steps to be taken following the chosen solution
What are the steps/instructions we should take according to the top solution ranked in step 4?
Will these steps/instructions be understood according to the knowledge base established in step 2?
Evaluate/Test the Solution
Do we have the desired results after following the steps in 5?
Does the solution satisfy the needs of the person with the problem?
What are some of the areas that are not correct/satisfactory? (back to step1)
Planning
Problem-Definition Table
Known Facts User Requirements
Necessary Processing
What are some of the given facts? Do we need to gather more facts first?
What does the user want done and what is the user required to do?
What are the steps we have to take to produce user requirements?
Input-Processing-Output Chart
Input Data Processing Steps
Output Data
Fill this FIRST Fill this SECOND Fill this THIRD
Pseudocode
Step-by-step English explanation of how to solve the problem.
Pseudocode for a Grading Program
If student's grade is greater than or equal to 50 Print "passed" else Print "failed"
Flowchart
A diagram of the solution to the problem, using specific flowchart symbols.
Samples
Sample #1
Let’s look at the original problemA flower has been placed in a coloured box. Each box carries a statement. Of the three statements, at most, only one is true. Which box contains the flower?
If you solve it, think about how you got to your solution.
REDThe flower is in
this box.
YELLOWThe flower is not
in this box.
GREENThe flower is not in the red box.
Problem-Definition Table
Known Facts User Requirements
Necessary Processing
One box contains the flower Only 0 or 1 true statement exists
Find the box that contains the flower
Start from box 1 assuming it is true, and go through the statements in each box. Follow the true or false consequences of each statement to arrive at the solution
Input-Processing-Output Chart (next slide)
Input Data
Processing Steps Output Data
Each statement inscribed on each box
If red box is true, then yellow must also be true. However this is not possible because we would have two true statements and we are given that only one statement at most can be true.
If red is false, i.e., the flower is not in the red box, then the statement on the green box is true
If the green box’s statement is true, then the yellow box’s statement must be false because there can be at most only 1 true statement.
Therefore, the flower is in the yellow box
The Yellow Box contains the flower
Pseudocode 1. Read in the three boxes’ statements 2. Assume the red box statement is true 3. If (red box’s statement is true) then The yellow box’s statement is also true, and we have a contradiction
with what we are given, i.e. only 1 statement at maximum is true, but now we would have two true statements if red box’s statement is true, so
4. If (red box’s statement is false) then The flower is not in the red box, i.e., green box’s statement is true And the yellow must be false because we are given that only 1
statement at maximum is true, so 5. If (yellow box’s statement is false) then the flower is in the yellow
box.
Sample #2
Amy has a favourite colour in mind. Try guessing this colour. If you guess correctly, Amy lets you know that you are right and the program stops. Otherwise, the program keeps on asking you for a guess. However, if you give up, just type in ZZZ to stop.
Problem-Definition Table
Known Facts User Requirements
Necessary Processing
There is one and only predetermined colour to guess The guess is typed in from the user
If the guess is correct, a correct message is given and the program stops
If the guess is wrong, the program keeps on asking for a guess until the guess is correct or the user types ZZZ
Keep on guessing Amy’s favourite colour until the guess is correct or give up by typing ZZZ
Assign a colour as Amy’s favourite colour Ask user to type in a guess
While the guess is not equal to Amy’s colour and not equal to ZZZ, ask user to type in another guess
If the guess is right, display a congratulations message
End of program
Input-Processing-Output Chart
Input Data
Processing Steps Output Data
A guess typed in by the user
Assign a colour as Amy’s favourite colour Ask user to type in a guess
While the guess is not equal to Amy’s colour and not equal to ZZZ, ask user to type in another guess
If the guess is right, display a congratulations message
End of program
A congratulations message if the guess is correct.