Problem Solving
The following problem-solving method is adapted from Polya's How to Solve
It and can be used to solve problems in any course.
The most important part of programming is solving the problem using
your brain along with paper and pencil. The worst thing you can do
is sit down at the keyboard and try to solve the problem as you write code.
- Understand the problem. Determine both what you want to know and what
information you are given. For example, if the problem is to find
the area of a rectangle given its length and width, you want to know
the area and you are given the length and width. Separating this step
into knowns and unknowns can be helpful for complex problems.
- Can I solve this problem directly?
- Yes - Write down the solution. For the problem, find the area of a
rectangle, the solution is
Area = length * width
- No - Go to Step 3.
- Have I solved this problem before?
- Yes - Use the solution you used before. For example, determine
the amount of carpeting needed to carpet a room. This is really
just finding the area of a rectangle, so use the same solution.
- No - Go to Step 4.
- Have I solved a problem similar to this before?
- Yes - Modify the solution to the similar problem so that it solves
this problem. For instance, if the capacity of an elevator is
determined by assuming each occupant takes 4 square feet of space,
how many people can an elevator of a certain length and width hold?
This problem is just the area of a rectangle divided by 4, so modify
the solution to solve this problem.
Capacity = area / 4
- No - Go to Step 5.
- Divide the problem into two or more smaller problems and go to Step 4.
Suppose that a homeowner wants to know the cost of redecorating the
living room. The redecoration will consist of putting in new carpeting,
covering the walls with paper instead of paint, and changing the style of
draperies for the windows. This problem can be solved by summing
the cost of carpeting, the cost of papering the walls, and
the cost of the new drapes. (Note: each of the "smaller" problems
will end up being a subprogram once the solution is translated into
a procedural programming language.)
- Repeat Steps 1 - 5 for each of the smaller problems created in Step 5.