If you're a beginning programmer and want to get more deeply into programming with variables, you've come to the right place. This article, the third of three parts, is excerpted from chapter two of the book Beginning C, written by Ivor Horton (Apress, 2004; ISBN: 1590592530).
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 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.
Real-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.
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.
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.
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.