Problem Solving and Algorithms: Problems, Solutions, and Tools
Problem Solving and Algorithms: Problems, Solutions, and Tools
Objects First
Knowing that Aunt Kay appreciates creative and unusual things, I have decided to hire a singing messenger to
deliver my thanks. In this context, the messenger is a tool, but one that needs instructions from me. I have to tell
the messenger where Aunt Kay lives, what time I would like the message to be delivered, and what lyrics I want
sung. A computer program is similar to my instructions to the messenger.
The story of Aunt Kay uses a familiar context to set the stage for a useful point of view concerning computers
and computer programs. The following list summarizes the key aspects of this point of view.
A computer is a tool that can be used to implement a plan for solving a problem.
sofia.cs.vt.edu/cs1114-ebooklet/chapter4.html 1/12
6/27/2019 4. Problem Solving and Algorithms
A computer program is a set of instructions for a computer. These instructions describe the steps
that the computer must follow to implement a plan.
This point of view sets the stage for a process that we will use to develop solutions to Jeroo problems. The basic
process is important because it can be used to solve a wide variety of problems, including ones where the
solution will be written in some other programming language.
There are many ways to write an algorithm. Some are very informal, some are quite formal and mathematical in
nature, and some are quite graphical. The instructions for connecting a DVD player to a television are an
algorithm. A mathematical formula such as πR2 is a special case of an algorithm. The form is not particularly
important as long as it provides a good way to describe and check the logic of the plan.
The development of an algorithm (a plan) is a key step in solving a problem. Once we have an algorithm, we can
translate it into a computer program in some programming language. Our algorithm development process
consists of five major steps.
The client is responsible for creating a description of the problem, but this is often the weakest part of the
process. It's quite common for a problem description to suffer from one or more of the following types of defects:
(1) the description relies on unstated assumptions, (2) the description is ambiguous, (3) the description is
incomplete, or (4) the description has internal contradictions. These defects are seldom due to carelessness by
sofia.cs.vt.edu/cs1114-ebooklet/chapter4.html 2/12
6/27/2019 4. Problem Solving and Algorithms
the client. Instead, they are due to the fact that natural languages (English, French, Korean, etc.) are rather
imprecise. Part of the developer's responsibility is to identify defects in the description of a problem, and to work
with the client to remedy those defects.
When determining the starting point, we should start by seeking answers to the following questions:
When determining the ending point, we need to describe the characteristics of a solution. In other words, how will
we know when we're done? Asking the following questions often helps to determine the ending point.
Analysis: I don't have a card. I prefer to buy a card rather than make one myself.
High-level algorithm:
This algorithm is satisfactory for daily use, but it lacks details that would have to be added were a computer to
carry out the solution. These details include answers to questions such as the following.
"How will I get there: walk, drive, ride my bicycle, take the bus?"
These kinds of details are considered in the next step of our process.
When our goal is to develop algorithms that will lead to computer programs, we need to consider the capabilities
of the computer and provide enough detail so that someone else could use our algorithm to write a computer
program that follows the steps in our algorithm. As with the birthday card problem, we need to adjust the level of
detail to match the ability of the programmer. When in doubt, or when you are learning, it is better to have too
much detail than to have too little.
Most of our examples will move from a high-level to a detailed algorithm in a single step, but this is not always
reasonable. For larger, more complex problems, it is common to go through this process several times,
developing intermediate level algorithms as we go. Each time, we add more detail to the previous algorithm,
stopping when we see no benefit to further refinement. This technique of gradually working from a high-level to a
detailed algorithm is often called stepwise refinement .
Stepwise refinement is a process for developing a detailed algorithm by gradually adding detail to a high-
level algorithm.
Does this algorithm solve a very specific problem or does it solve a more general problem? If it solves a
very specific problem, should it be generalized?
For example, an algorithm that computes the area of a circle having radius 5.2 meters (formula π*5.22)
solves a very specific problem, but an algorithm that computes the area of any circle (formula π*R2) solves a
more general problem.
Is this solution similar to the solution to another problem? How are they alike? How are they different?
sofia.cs.vt.edu/cs1114-ebooklet/chapter4.html 4/12
6/27/2019 4. Problem Solving and Algorithms
Differences: Different measurements are used. The triangle formula contains 0.5.
Start Finish
2. The flower is to be planted exactly two spaces South of its current location.
3. The Jeroo is to finish facing East one space East of the planted flower.
sofia.cs.vt.edu/cs1114-ebooklet/chapter4.html 5/12
6/27/2019 4. Problem Solving and Algorithms
2. This algorithm solves a very specific problem because the Jeroo and the flower are in very specific locations.
3. This algorithm is actually a solution to a slightly more general problem in which the Jeroo starts anywhere,
and the flower is 3 spaces directly ahead of the Jeroo.
A good programmer works incrementally , add small pieces one at a time and constantly re-checking the
work so far.
FIRST BUILD
To see this solution in action, create a new Greenfoot4Sofia scenario and use the Edit Palettes Jeroo
menu command to make the Jeroo classes visible. Right-click on the Island class and create a new subclass
with the name of your choice. This subclass will hold your new code.
sofia.cs.vt.edu/cs1114-ebooklet/chapter4.html 6/12
6/27/2019 4. Problem Solving and Algorithms
The instantiation at the beginning of myProgram() places bobby at (0, 0), facing East, with no flowers.
Once the first build is working correctly, we can proceed to the others. In this case, each build will correspond to
one step in the high-level algorithm. It may seem like a lot of work to use four builds for such a simple program,
but doing so helps establish habits that will become invaluable as the programs become more complex.
SECOND BUILD
This build adds the logic to "get the flower", which in the detailed algorithm (step 4 above) consists of hopping 3
times and then picking the flower. The new code is indicated by comments that wouldn't appear in the original
(they are just here to call attention to the additions). The blank lines help show the organization of the logic.
By taking a moment to run the work so far, you can confirm whether or not this step in the planned algorithm
works as expected.
THIRD BUILD
This build adds the logic to "put the flower". New code is indicated by the comments that are provided here to
mark the additions.
7 bobby.hop(3);
8 bobby.pick();
9
10 // --- Put the flower ---
11 bobby.turn(RIGHT); // <-- new code to turn right
12 bobby.hop(2); // <-- new code to hop 2 times
13 bobby.plant(); // <-- new code to plant a flower
14
15 // --- Hop East ---
16
17 } // ===== end of method myProgram() =====
Start Finish
sofia.cs.vt.edu/cs1114-ebooklet/chapter4.html 8/12
6/27/2019 4. Problem Solving and Algorithms
4. Jeroo_2 will have two flowers after receiving one from Jeroo_1.
One flower must be used to disable the net.
The other flower must be planted at the location of the net, i.e. (3, 2).
7. Each Jeroo will finish with 0 flowers in its pouch. One flower was used to disable the net, and the other was
planted.
sofia.cs.vt.edu/cs1114-ebooklet/chapter4.html 9/12
6/27/2019 4. Problem Solving and Algorithms
2. This algorithm solves a very specific problem, but the specific locations are not important. The only thing that
is important is the starting location of the Jeroos relative to one another and the location of the net relative to
the second Jeroo's location and direction.
FIRST BUILD
This build creates the main method, instantiates the Jeroos, and outlines the high-level algorithm. In this
example, the main method would be myProgram() contained within a subclass of Island .
sofia.cs.vt.edu/cs1114-ebooklet/chapter4.html 10/12
6/27/2019 4. Problem Solving and Algorithms
SECOND BUILD
This build adds the logic for Ann to locate Andy and give him a flower.
THIRD BUILD
This build adds the logic for Andy to locate and disable the net.
sofia.cs.vt.edu/cs1114-ebooklet/chapter4.html 11/12
6/27/2019 4. Problem Solving and Algorithms
19 andy.toss();
20
21 // --- Andy, place a flower at (3, 2) ---
22
23 // --- Andy, face South ---
24
25 } // ===== end of method myProgram() =====
FOURTH BUILD (final)
This build adds the logic for Andy to place a flower at (3, 2) and turn South.
sofia.cs.vt.edu/cs1114-ebooklet/chapter4.html 12/12