Home arrow C++ arrow Page 7 - First Steps in (C) Programming, conclusion
C++

First Steps in (C) Programming, conclusion


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).

Author Info:
By: Apress Publishing
Rating: 4 stars4 stars4 stars4 stars4 stars / 63
December 01, 2005
TABLE OF CONTENTS:
  1. · First Steps in (C) Programming, conclusion
  2. · More Numeric Data Types
  3. · Try It Out: Character Building
  4. · Unsigned Integers: Using Positive Integers
  5. · Try It Out: Finding the Limits
  6. · Mathematical Functions
  7. · Designing a Program
  8. · The Solution
  9. · Summary

print this article
SEARCH DEVARTICLES

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 Problem

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.

The Analysis

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:

  1. Input the values you need.
  2. Calculate the height of the tree using the equation in the diagram.
  3. Display the answer.

blog comments powered by Disqus
C++ ARTICLES

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials