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 - The Solution (Page 8 of 9 )

This section outlines the steps you’ll take to solve the problem.

Step 1

Your first step is to get the values that you need to work out the height of the tree. This means that you have to include thestdio.hheader file, because you need to use bothprintf()andscanf(). You then have to decide what variables you need to store these values in. After that, you can useprintf()to prompt for the input andscanf()to read the values from the keyboard.

You’ll provide for the heights of the participants to be entered in feet and inches for the convenience of the user. Inside the program, though, it will be easier to work with all heights and distances in the same units, so you’ll convert all measurements to inches. You’ll need two variables to store the heights of Shorty and Lofty in inches. You’ll also need a variable to store the distance between Lofty and Shorty, and another to store the distance from Lofty to the tree—both distances in inches, of course.

In the input process, you’ll first get Lofty’s height as a number of whole feet and then as a number of inches, prompting for each value as you go along. You can use two more variables for this: one to store the feet value and the other to store the inches value. You’ll then convert these into just inches and store the result in the variable you’ve reserved for Lofty’s height. You’ll do the same thing for Shorty’s height (but only up to the height of his or her eyes) and finally the same for the distance between them. For the distance to the tree, you’ll use only whole feet, because this will be accurate enough—and again you’ll convert the distance to inches. You can reuse the same variables for each measurement in feet and inches that is entered. So here goes with the first part of the program:

/* Program 2.17 Calculating the height of a tree */ #include <stdio.h> void main() { long shorty = 0L; /* Shorty's height in inches */ long lofty = 0L; /* Lofty's height in inches */ long feet = 0L; /* A whole number of feet */ long inches = 0L; /* A whole number of inches */ long shorty_to_lofty = 0; /* Distance from Shorty to Lofty in inches */ long lofty_to_tree = 0; /* Distance from Lofty to the tree in inches */ const long inches_per_foot = 12L; /* Get Lofty's height */ printf("Enter Lofty's height to the top of his/her head, in whole feet: "); scanf("%ld", &feet); printf(" ...and then inches: "); scanf("%ld", &inches); lofty = feet*inches_per_foot + inches; /* Get Shorty's height up to his/her eyes */ printf("Enter Shorty's height up to his/her eyes, in whole feet: "); scanf("%ld", &feet); printf(" ... and then inches: "); scanf("%ld", &inches); shorty = feet*inches_per_foot + inches; /* Get the distance from Shorty to Lofty */ printf("Enter the distance between Shorty and Lofty, in whole feet: "); scanf("%ld", &feet); printf(" ... and then inches: "); scanf("%ld", &inches); shorty_to_lofty = feet*inches_per_foot + inches; /* Get the distance from Lofty to the tree */ printf("Finally enter the distance to the tree to the nearest foot: "); scanf("%ld", &feet); lofty_to_tree = feet*inches_per_foot; /* The code to calculate the height of the tree will go here */ /* The code to display the result will go here */ }

Notice how the program code is spaced out to make it easier to read. You don’t have to do it this way, but if you decide to change the program next year, it will make it much easier to see how the program works if it’s well laid out. You should always add comments to your programs to help with this. It’s particularly important to at least make clear what the variables are used for and to document the basic logic of the program.

You use a variable that you’ve declared asconst to convert from feet to inches. The variable name,inches_per_foot, makes it reasonably obvious what’s happening when it’s used in the code. This is much better than using the “magic number” 12 explicitly. Here you’re dealing with feet and inches, and most people will be aware that there are 12 inches in a foot. In other circumstances, the significance of numeric constants may not be so obvious, though. If you’re using the value 0.22 in a program calculating salaries, it’s much less apparent what this might be; therefore, the calculation may seem rather obscure. If you create aconst variabletax_ratethat you’ve initialized to 0.22 and use that instead, then the mist clears.

Step 2

Now that you have all the data you need, you can calculate the height of the tree. All you need to do is implement the equation for the tree height in terms of your variables. You’ll need to declare another variable to store the height of the tree.

You can now add the code that’s shown here in bold type to do this:

/* Program 2.17 Calculating the height of a tree */ #include <stdio.h> void main() { long shorty = 0L; /* Shorty's height in inches */ long lofty = 0L; /* Lofty's height in inches */ long feet = 0L; /* A whole number of feet */ long inches = 0L; /* A whole number of inches */ long shorty_to_lofty = 0; /* Distance from Shorty to Lofty in inches */ long lofty_to_tree = 0; /* Distance from Lofty to the tree in inches */ long tree_height = 0; /* Height of the tree in inches */ const long inches_per_foot = 12L; /* Get Lofty's height */ printf("Enter Lofty's height to the top of his/her head, in whole feet: "); scanf("%ld", &feet); printf(" ...and then inches: "); scanf("%ld", &inches); lofty = feet*inches_per_foot + inches; /* Get Shorty's height up to his/her eyes */ printf("Enter Shorty's height up to his/her eyes, in whole feet: "); scanf("%ld", &feet); printf(" ... and then inches: "); scanf("%ld", &inches); shorty = feet*inches_per_foot + inches; /* Get the distance from Shorty to Lofty */ printf("Enter the distance between Shorty and Lofty, in whole feet: "); scanf("%ld", &feet); printf(" ... and then inches: "); scanf("%ld", &inches); shorty_to_lofty = feet*inches_per_foot + inches; /* Get the distance from Lofty to the tree */ printf("Finally enter the distance to the tree to the nearest foot: "); scanf("%ld", &feet); lofty_to_tree = feet*inches_per_foot; /* Calculate the height of the tree in inches */ tree_height = shorty + (shorty_to_lofty + lofty_to_tree)*(lofty-shorty)/ shorty_to_lofty; /* The code to display the result will go here */ }

The statement to calculate the height is essentially the same as the equation in the diagram. It’s a bit messy, but it translates directly to the statement in the program to calculate the height.

Step 3

Finally, you need to output the answer. To present the result in the most easily understandable form, you’ll convert the result that you’ve stored intree_height, which is in inches, back into feet and inches:

/* Program 2.17 Calculating the height of a tree */ #include <stdio.h> void main() { long shorty = 0L; /* Shorty's height in inches */ long lofty = 0L; /* Lofty's height in inches */ long feet = 0L; /* A whole number of feet */ long inches = 0L; /* A whole number of inches */ long shorty_to_lofty = 0; /* Distance from Shorty to Lofty in inches */ long lofty_to_tree = 0; /* Distance from Lofty to the tree in inches */ long tree_height = 0; /* Height of the tree in inches */ const long inches_per_foot = 12L; /* Get Lofty's height */ printf("Enter Lofty's height to the top of his/her head, in whole feet: "); scanf("%ld", &feet); printf(" ... and then inches: "); scanf("%ld", &inches); lofty = feet*inches_per_foot + inches; /* Get Shorty's height up to his/her eyes */ printf("Enter Shorty's height up to his/her eyes, in whole feet: "); scanf("%ld", &feet); printf(" ... and then inches: "); scanf("%ld", &inches); shorty = feet*inches_per_foot + inches; /* Get the distance from Shorty to Lofty */ printf("Enter the distance between Shorty and Lofty, in whole feet: "); scanf("%ld", &feet); printf(" ... and then inches: "); scanf("%ld", &inches); shorty_to_lofty = feet*inches_per_foot + inches; /* Get the distance from Lofty to the tree */ printf("Finally enter the distance to the tree to the nearest foot: "); scanf("%ld", &feet); lofty_to_tree = feet*inches_per_foot; /* Calculate the height of the tree in inches */ tree_height = shorty + (shorty_to_lofty + lofty_to_tree)*(lofty-shorty)/ shorty_to_lofty; /* Display the result in feet and inches */ printf("The height of the tree is %ld feet and %ld inches.\n", tree_height/inches_per_foot, tree_height% inches_per_foot); }

And there you have it. The output from the program looks something like this:

-------------------------------------------- Enter Lofty's height to the top of his/her head, in whole feet first: 6 ... and then inches: 2 Enter Shorty's height up to his/her eyes, in whole feet: 4 ... and then inches: 6 Enter the distance between Shorty and Lofty, in whole feet : 5 ... and then inches: 0 Finally enter the distance to the tree to the nearest foot: 20 The height of the tree is 12 feet and 10 inches. --------------------------------------------