First Steps in (C) Programming, conclusion - Designing a Program
(Page 7 of 9 )
Now it’s time for the end-of-chapter real-life example. It would be a great idea to try out some of the numeric types in a new program. I’ll take you through the basic elements of the process of writing a program from scratch. This involves receiving an initial specification of the problem, analyzing the problem, preparing a solution, writing the program and, of course, running the program and testing it to make sure it works. Each step in the process can introduce problems, beyond just the theory.
The ProblemThe height of a tree is of great interest to many people. For one thing, if a tree is being cut down, knowing its height tells you how far away safe is. This is very important to those with a nervous disposition. Your problem is to find out the height of a tree without using a very long ladder, which itself would introduce risk to life and limb. To find the height of a tree, you’re allowed the help of a friend—preferably a short friend. You should assume that the tree you’re measuring is taller than both you and your friend. Trees that are shorter than you present little risk, unless they’re of the spiky kind.
The AnalysisReal-world problems are rarely expressed in terms that are directly suitable for programming. Before you consider writing a line of code, you need to be sure that you have a complete understanding of the problem and how it’s going to be solved. Only then can you estimate how much time and effort will be involved in creating the solution.
The analysis phase involves gaining a full understanding of the problem and determining the logical process for solving it. Typically this requires a significant amount of work. It involves teasing out any detail in the specification of the problem that is vague or missing. Only when you fully understand the problem can you begin to express the solution in a form that’s suitable for programming.
You’re going to determine the height of a tree using some simple geometry and the heights of two people: you and one other. Let’s start by naming the tall person (you) Lofty and the shorter person (your friend) Shorty. If you’re vertically challenged, the roles can be reversed. For more accurate results, the tall person should be significantly taller than the short person. Otherwise the tall person could consider standing on a box. The diagram in Figure 2-2 will give you an idea of what you’re trying to do in this program.
/mini-image_2.JPG)
Figure 2-2. The height of a tree
Finding the height of the tree is actually quite simple. You can get the height of the tree, h3, if you know the other dimensions shown in the illustration: h1and h2, which are the heights of Shorty and Lofty, and d1and d2, which are the distances between Lofty and Shorty, and from Lofty to the tree, respectively. You can use the technique of similar triangles to work out the height of the tree. You can see this in the simplified diagram in Figure 2-3.
/mini-image_3.JPG)
Figure 2-3. Similar triangles
Here, because the triangles are similar,height1divided bydistance1 is equal toheight2divided bydistance2. Using this relationship, you can get the height of the tree from the height of Shorty and Lofty and the distances to the tree as shown in Figure 2-4.
The triangles ADE and ABC are the same as those shown in the previous diagram. Using the fact that the triangles are similar, you can calculate the height of the tree as shown in the bottom equation in the diagram.
This means that you can calculate the height of the tree in your program from four values:
- The distance between Shorty and Lofty, d1in the diagram. You’ll use the variableshorty_to_loftyto store this value.
- The distance between Lofty and the tree, d2in the diagram. You’ll use the variablelofty_to_treeto store this value.
- The height of Lofty to the top of his head, h2in the diagram. You’ll use the variableloftyto store this value.
- The height of Shorty, but only up to the eyes, h1in the diagram. You’ll use the variableshortyto store this value.
You can then plug these values into the equation for the height of the tree.
/mini-image_4.JPG)
Figure 2-4. Calculating the tree height
Your first task is to get these four values into the computer. You can then use your ratios to find out the height of the tree, and finally output the answer. The steps are as follows:
- Input the values you need.
- Calculate the height of the tree using the equation in the diagram.
- Display the answer.
Next: The Solution >>
More C++ Articles
More By Apress Publishing
|
This article is excerpted from chapter two of the book Beginning C, written by Ivor Horton (Apress, 2004; ISBN: 1590592530). Check it out at your favorite bookstore. Buy this book now.
|
|