### Finally enter the distance from Lofty to the tree to the nearest foot

Hi, I'm trying to understand and learn C, reading the book From novice to professional5th edition. In end of 2nd chapter is this one code. What I don't understand is why the last input haven't been done also for inches but just feet??? I don't understand. In my view I would take the same input for feet and inches as previous ones. Maybe I'm dumb. Besides the whole program to find solution based on similar triangles theorem is pretty inaccurate and confusing in real life scenario it wouldn't produce the right answer. If anyone will have the patience with me and try to explain why's that I would really appreciated. Many thanks

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859`` `````` /* math01.c *program calculating height of a tree * */ #include #include int main(int argc, const char * argv[]) { long shorty = 0L; // Shorty's height in inches long lofty = 0L; // Lofty's height in inches long feet = 0L; long inches = 0L; long shorty_to_lofty = 0L; // Distance from Shorty to Lofty in inches long lofty_to_tree = 0L; // Distance from Lofty to the tree in inches long tree_height = 0L; // 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 from Lofty 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); return 0; } ``````
Last edited on
mato9099 wrote:
What I don't understand is why the last input haven't been done also for inches but just feet???

Because the input wants the distance rounded to the nearest foot (read the prompt)
Last edited on
Topic archived. No new replies allowed.