A Silent Computational Error

In this lesson, we will discuss errors that cause no error messages.

Silent errors

Some programs will give you incorrect results, but will not warn you with an error message. In our first Debugging Interlude, we named this kind of error a silent error. It is an error in the logic of our program. Such errors can be extremely dangerous.

Programs that perform numerical computations are susceptible to silent errors. Here is an example.

A problem solved: Computing a cylinder’s volume

Imagine that your grandmother has asked you to help her make some candles. She has collected some cardboard mailing tubes to serve as molds. How much wax will she need? To find out, we can compute the volume of each cylindrical tube.

We begin by writing some pseudocode to describe what we need to do. It consists of three straightforward steps:

  • Prompt for and read the cylinder’s height and radius.

  • Compute the volume.

  • Display the height, radius, and volume.

Since cylinders are the focus of this problem, let’s define a class Cylinder to represent the various cylindrical mailing tubes.

The class Cylinder

A cylinder has a radius and a height, so these components can be the data fields of our class. In addition to creating a constructor, we can define accessor methods for the radius and height, as well as one for the volume.

We need the formula for the volume of a cylinder. That volume is simply the area of the cylinder’s circular cross-section multiplied by the cylinder’s height. The cross-section area of a cylinder is the area of a circle or π × r2. Thus, we have the following formula for the volume of a cylinder:

Vc = h × π × r2

The program given below contains our definition of a class Cylinder.

Get hands-on with 1200+ tech skills courses.