LECTURE 6 Problem Solving

advertisement
LECTURE 6
Problem Solving
PROBLEM SOLVING
In the beginning of the course, we discussed how programming was essentially the
process of solving problems. Typically, before you start a program you have a
problem statement. For example,
Determine the class average for a set of test grades.
This is a perfectly valid starting point. We consider the problem at hand and then
design a solution. But sometimes, that task can be daunting. Today we’ll talk about a
method for designing and implementing solutions known as stepwise refinement.
TOP-DOWN STEPWISE REFINEMENT
Let’s talk about the technique of top-down stepwise refinement.
The “top-down” part of the name suggests that we first design an overall approach,
then break down the solution into more manageable pieces. This can help make
daunting problems seem more manageable.
TOP-DOWN STEPWISE REFINEMENT
Let’s talk about the technique of top-down stepwise refinement.
The “top-down” part of the name suggests that we first design an overall approach,
then break down the solution into more manageable pieces. This can help make
daunting problems seem more manageable.
• Start with an initial problem statement.
TOP-DOWN STEPWISE REFINEMENT
Let’s talk about the technique of top-down stepwise refinement.
The “top-down” part of the name suggests that we first design an overall approach,
then break down the solution into more manageable pieces. This can help make
daunting problems seem more manageable.
• Start with an initial problem statement.
• Create a solution with a few general steps.
TOP-DOWN STEPWISE REFINEMENT
Let’s talk about the technique of top-down stepwise refinement.
The “top-down” part of the name suggests that we first design an overall approach,
then break down the solution into more manageable pieces. This can help make
daunting problems seem more manageable.
• Start with an initial problem statement.
• Create a solution with a few general steps.
• Take each step and break it up into more detailed steps.
TOP-DOWN STEPWISE REFINEMENT
Let’s talk about the technique of top-down stepwise refinement.
The “top-down” part of the name suggests that we first design an overall approach,
then break down the solution into more manageable pieces. This can help make
daunting problems seem more manageable.
• Start with an initial problem statement.
• Create a solution with a few general steps.
• Take each step and break it up into more detailed steps.
• Repeat this process until you have steps that are relatively specific.
TOP-DOWN STEPWISE REFINEMENT
Let’s talk about the technique of top-down stepwise refinement.
The “top-down” part of the name suggests that we first design an overall approach,
then break down the solution into more manageable pieces. This can help make
daunting problems seem more manageable.
• Start with an initial problem statement.
• Create a solution with a few general steps.
• Take each step and break it up into more detailed steps.
• Repeat this process until you have steps that are relatively specific.
• Create pseudocode for each step, then translate it into real code.
EXAMPLE
We’ll do an example together to see how we can approach a problem statement and
create an program to solve the problem at hand. Let’s look at our problem statement
again.
Determine the class average for a set of test grades. The test grades should be input by the user.
The number of test grades they will enter is not known in advance.
Notice that there are some additional details in our problem statement. Typically, a
problem statement is associated with some extra information that will impact your
solution.
BREAKDOWN INTO STEPS
Ok, so let’s break down the problem into steps.
BREAKDOWN INTO STEPS
Ok, so let’s break down the problem into steps.
• Declare and initialize variables.
• Input grades (prompt user and grab input).
• Compute class average and output result.
BREAKDOWN OF STEPS
Let’s take the computation step and break it down further.
• Declare and initialize variables.
• Input grades (prompt user and grab input).
• Compute class average and output result.
BREAKDOWN OF STEPS
Let’s take the computation step and break it down further.
• Declare and initialize variables.
• Input grades (prompt user and grab input).
• Compute class average and output result.
• Add the grades.
• Count the grades.
• Divide the sum by the count.
BREAKDOWN OF STEPS
Let’s take the computation step and break it down further.
• Declare and initialize variables.
• Input grades (prompt user and grab input).
• Compute class average and output result.
• Add the grades.
• Count the grades.
• Divide the sum by the count.
We have a problem! I don’t know how many grades will be entered, so to add the
grades after they’ve all been entered, I’ll need to have a variable for each one.
BREAKDOWN OF STEPS
Let’s revise our plan to fix this problem. Why don’t I add and count the grades as
they are input? That way, I don’t need to store them to be added later on.
• Declare and initialize variables.
• Input grades (prompt user and grab input).
• Add and count grades as they are entered.
• Compute class average and output result.
• Divide the sum by the count.
Don’t be afraid to revise your plans 
BREAKDOWN OF STEPS
Let’s talk about how we can break down our steps further.
Gathering Input
loop until the user enters the sentinel value (-1 would be good).
prompt user to enter a grade (give them needed info, like -1 to quit).
allow user to type in a grade (store in a temporary variable).
add the grade into a variable used for storing the sum.
add 1 to a counter (to track how many grades).
This looks like we could use a do-while loop!
BREAKDOWN OF STEPS
Gathering Input
do
prompt user to enter a grade (give them needed info, like -1 to quit).
allow user to type in a grade (store in a temporary variable).
add the grade into a variable used for storing the sum.
add 1 to a counter (to track how many grades).
while user has NOT entered the sentinel value (-1 would be good).
BREAKDOWN OF STEPS
Gathering Input
do
prompt user to enter a grade (give them needed info, like -1 to quit).
allow user to type in a grade (store in a temporary variable).
If the user did not enter the sentinel value:
add the grade into a variable used for storing the sum.
add 1 to a counter (to track how many grades).
while user has NOT entered the sentinel value (-1 would be good).
BREAKDOWN OF STEPS
Gathering Input
do
prompt user to enter a grade (give them needed info, like -1 to quit).
allow user to type in a grade (store in a temporary variable).
If the user did not enter the sentinel value:
add the grade into a variable used for storing the sum.
add 1 to a counter (to track how many grades).
while user has NOT entered the sentinel value (-1 would be good).
BREAKDOWN OF STEPS
So now we can see what kinds of variables we need.
Declare and Initialize Variables
a temporary grade variable (to store the user entry).
a sum variable (initialized to 0).
a counter variable (initialized to 0).
BREAKDOWN OF STEPS
Compute the Average and Output the Result
divide sum by counter.
print result.
We need to consider one more thing: It’s possible that the user will enter the sentinel value
immediately without entering any grades. In that case, counter will be zero. So let’s add an if
statement to catch that case.
BREAKDOWN OF STEPS
Compute the Average and Output the Result
If counter > 0:
divide sum by counter.
print result.
else:
print error message to user.
PUTTING IT ALL TOGETHER
Initialize Variables
a temporary grade variable (to store user entry).
a sum variable (initialized to 0).
a counter variable (initialized to 0).
Grade Entry
do
prompt user to enter a grade (give them needed info, like -1 to quit).
allow user to type in a grade (store in a temporary variable).
if the entered value is not the sentinel value:
add the grade into a variable used for storing the sum.
add 1 to a counter (to track how many grades).
while user has NOT entered the sentinel value (-1 would be good)
Compute Average
if counter > 0:
divide sum by counter.
print result.
else:
print error message to user.
FROM PSEUDOCODE TO CODE
Now, let’s take our pseudocode and turn it into real code.
Related documents
Download