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.

  1. 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.
  2. Can I solve this problem directly?
  3. Have I solved this problem before?
  4. Have I solved a problem similar to this before?
  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.)
  6. Repeat Steps 1 - 5 for each of the smaller problems created in Step 5.