Don't print this page!

If you want a printed copy go to this page


Program Comments
//Assignment 1 - Sample Solution 1
#include <iostream>
#include <math.h>
#include <ctype.h>
#define RAD (180.0/3.14159265359);
The usual header files are included along with a symbolic constant for the conversion of degrees to radians.
double find_distance(void);
double find_degrees(void);
double findheight(double, double);
char   get_distance_unit(void);
double getradians(double);
double findyards(double);
double findmetres(double);
void display_height(char, double);
double r = RAD;
Several function prototypes are declared and a global variable r. You can see that each function is typed, for instance findyards is of type double which means that this function will return a double precision floating point number. Some of the functions require arguments, display_height requires two arguments, a char and a double. The word void puzzles some people, all it means is nothing. If a function has a data type of void it means the function doesn't return a value. If a function has void arguments it means it has no arguments.
int main()
{
 double degrees, radians, 
        distance, height;
 char factor = ' ';

 degrees = find_degrees();
 distance = find_distance();
 radians = getradians(degrees);
 height = findheight( distance,radians);
 factor = get_distance_unit();
 display_height(factor, height);

 return 0;
}

Here is the main function. After declaring some local variables we make a number of calls to functions to get some values. The variables degrees, distance, radians and height should be quite self-explanatory but factor might puzzle. This variable is used to indicate what kind of unit the distance represents. If factor is set to M the distance is in metres, if factor is Y then distance is in yards, if factor is N then distance is in nautical miles.

There is an alternative main function shown below.

double find_degrees(void)
{double dd;
 cout << "Enter the elevation in degrees " ;
 cin >> dd;
 return dd;
}
The find_degrees() function prompts the user for an angle of elevation and returns this as a double floating point value which represents degrees.
double find_distance(void)
{double dd;
 cout << "Enter the distance " ;
 cin >> dd;
 return dd;
}
The find_distance() function prompts the user for distance measured by the laser and returns this as a double floating point value. The distance will be in yards, metres or nautical miles.
double getradians(double x)
{
  return (x / r);
}
The getradians() function converts the degrees entered by the user to radians. The conversion is required because the standard sin() function used in the find_height() function uses radians as its argument.
double findheight(double dist, double elev)
{
  return dist * sin(elev);
}
The find_height() function calculates the height if the object being measured. It is based on the trigonometric formula O = H*SIN(x). The height will be in the same units as the distance.
char get_distance_unit(void)
{
 char c;

 do
  { cout << "Enter M if the distance is in metres"
         << endl;
    cout << "Enter N if the distance is in nautical miles"
	     << endl;     
    cout << "Enter Y if the distance is in yards"
	     << endl;
    cin >> c;
    c = toupper(c);
  } while ((c != 'M') && 
           (c != 'N') &&
           (c != 'Y'));

 return c;
}
The get_distance_unit() function is used to determine what kind of units our laser is using. The function uses a do ... while loop and prompts the user for one of three characters. Each character that is entered is forced to upper case by the standard toupper() function. This loop will persist until the user enters either an m, an n or a y. The function then returns this character.
void display_height(char f, double h)
{ double metres, yards; 
 
  cout << "The height of the object is " ; 

  switch (f)
   { case 'Y' : //height is in yards
                metres = findmetres(h);
                break;
     case 'N' : //height is in nautical miles
                yards = findyards(h);
                metres = findmetres(yards);
                break;
     case 'M' : //height is in metres
                metres = h;
                break;
   }

  cout << metres
       << " metres."
       << endl;
}
The display_height() function has two arguments, a character which indicates what kind of units were used and a double which is the height of the measured object. A switch statement is used to test each of the cases of the unit factor f. A local variable metres will contain the object height in metres. If the unit is yards (f = 'Y') then a conversion from yards to metres is done. If the units is nautical miles (f = 'N') then the height is first converted to yards and then to metres. If the unit is metres then no conversion is required.
double findyards(double m)
{
  return (m * 6080.0) / 3.0;
}
The findyards() function receives height as the double argument m which represents height in nautical miles. It converts this to yards by multiply m by 6080 which is the number of feet in a nautical mile then dividing by 3 to get yards.
double findmetres(double y)
{
  return (y * 2.54 * 36.0) / 100.0;
}
The function findmetres() converts its argument y, which represents height in yards, to metres. The argument y is multiplied by the factors 2.54, the number of centimetres in an inch, and 36, the number of inches in a yard, then the expression is divided by 100 to give metres.

int main(void)
{
 return
  display_height( get_distance_unit(),
                  findheight( find_distance(),
                              getradians(find_degrees())
                            )
                );
}
This is an alternative main function which could be used in place of the main function shown earlier. What advantages would you gain by using this main function rather than the first shown?

Return to top of page

Return to lesson


Copyright © 1999 - 2001 David Beech