IUP Computer Science
COSC 110  Fall 2007
 

Project #1
(Due  12 September 2007)

On the next two pages is a C++ program written in the style you will be expected to use throughout this course. Your first assignment is to enter, compile, link, and run this program.  As you enter the program, replace my name and section number with your own name and your section number. Otherwise, you should enter just what you see on the other pages. Do not worry that the program contains C++ features that we have not yet covered; these features will be covered soon. If you do this correctly, when you run the program, you will see a display similar to this:

Distance from pit to pole: 229.24 meters to 233.57 meters

Distance from boulder to pole: 215.46 meters to 222.26 meters

Press any key to continue

After you have entered the slightly modified program and run it to get the answers, print out a page with your program on it. (You MUST do this printing using the Visual Studio programming environment, NOT a word processor; you do not need to print in color.) Then, do one of the following, depending on the last digit of your Banner ID number.

Deliberately make one of the following mistakes in your program and then try to compile again - choose the mistake whose number matches the last digit of your Banner ID number. You should get one or more error or warning messages. By hand, write the last digit of your Banner ID number, and the error/warning message(s) you get on the bottom or back of your printout;  then hand it in.

 0.  Omit the semicolon from  lowSecond = BOULDER_POLE - 2.0;
 1.  In the declaration of cosSquared, double    highSecond;
         change the 'S' to lowercase, i.e., 's'.
 2.  In the line  int main ()     change the right parenthesis to ]
 3.  Replace the first <<  with >>  in
         cout << fixed << showpoint << setprecision(2);
 4.  Omit the // on the line  // Display results
 5.  Change the declaration,  const double   ONE_EIGHTY = 180.0;
         to    const double  ONE_EIGHTY;
 6.  Misspell GetDistances as GetDistance in the statement
          GetDistances (lowFirst, lowSecond, lowFirstDistance, lowSecondDistance);
 7.  Misspell double in  double     atPitAngle;    as   doble.
 8.  Change // to /* in  // Display results
 9.  Replace the  return 0; statement with    return "done";

Do NOT save your program with the error in it. Do NOT print out your program with the error in it. By hand, using your best handwriting, write the error/warning message(s) on the printout of the correct program. Be sure to look at the entire message window to find all error/warning messages.  You do not need to write repeated error messages; writing each error once is enough.

// *******************
// Program #1   Estimating distance based on bearings
//
// This program solves a cartography problem in which the mapper
// takes bearings on a utility pole while standing at a pit
// (the first bearing) and at a boulder (the second bearing).  Using
// the bearings and the measured distance between the pit and the
// boulder (and the bearing between them), the program calculates
// the distance between the pit and the pole and the boulder and
// the pole. It determines the angles at the corners of a triangle
// and uses the Sine Law to calculate the distances.  Bearings are
//  measured relative to magnetic north - two calculations are made
// to allow for error in the taking of the bearings.

// Author:  Jim Wolfe
// Due:  12 September 2007

// Input:  None (data is held as constants)
// Output: A range of values for each distance, assuming the bearings
//   are off by as much as 2 degrees.
// *********************

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

const double ONE_EIGHTY = 180.0;       // Degrees in a radian
const double PI = 3.14159265359;       // Accurate version of pi
const double DISTANCE_WALKED = 37.5;   // Between measurements (meters)
const double DIRECTION_WALKED = 22.0;  // Bearing between points
const double PIT_POLE = 88.0;          // Bearing from pit to pole
const double BOULDER_POLE = 97.0;      // Bearing from boulder to pole

void GetDistances (double, double, double &, double &);

int main ()
{
    double lowFirst;            // Low range of angle pit to pole
    double highFirst;           // High range of angle pit to pole
    double lowSecond;           // Low range of angle boulder to pole
    double highSecond;          // High range of angle boulder to pole
    double lowFirstDistance;    // Distance for 2 degrees less at pit
    double lowSecondDistance;   // Distance for 2 degrees less at boulder
    double highFirstDistance;   // Distance for 2 degrees more at pit
    double highSecondDistance;  // Distance for 2 degrees more at boulder

    lowFirst = PIT_POLE - 2.0;    // Adjust bearings if measured ones are high
    lowSecond = BOULDER_POLE - 2.0;
    GetDistances (lowFirst, lowSecond, lowFirstDistance, lowSecondDistance);

    highFirst = PIT_POLE + 2.0;   // Adjust bearings if measured ones are low
    highSecond = BOULDER_POLE + 2.0;
    GetDistances (highFirst, highSecond, highFirstDistance, highSecondDistance);
          // Display results
    cout << fixed << showpoint << setprecision(2);
    cout << "Distance from pit to pole: " << lowFirstDistance
         << " meters to " << highFirstDistance << " meters" << endl << endl;
    cout << "Distance from boulder to pole: " << lowSecondDistance
         << " meters to " << highSecondDistance << " meters" << endl << endl;
    return 0;
}

// GetDistances
// Calculate the distance from the pit and the boulder to the pole
//      Precondition:  bearings must be valid
//      Postcondition:  distances are returned to main
//      Inputs:  bearing from pit to pole and from boulder to pole in degrees
//      Outputs: distances from pit to pole and boulder to pole in meters

void GetDistances (double pitBearing,   // Modified bearing to pit
       double boulderBearing,           // Modified bearing to boulder
       double & pitDistance,            // Distance from pit to pole
       double & boulderDistance)        // Distance from boulder to pole
{
     double  atPitAngle;      // Triangle angle at pit
     double  atBoulderAngle;  // Triangle angle at boulder
     double  atPoleAngle;     // Triangle angle at pole
     double  radiansAtPole;   // Angle at pole in radians

     atPitAngle = pitBearing - DIRECTION_WALKED;
     atBoulderAngle = ONE_EIGHTY - (boulderBearing - DIRECTION_WALKED);
     atPoleAngle = ONE_EIGHTY - atPitAngle - atBoulderAngle;
     radiansAtPole = atPoleAngle / ONE_EIGHTY * PI;

          // Calculate distances (angle at pit and boulder are converted to
          // radians within the calculation)

     pitDistance = DISTANCE_WALKED * sin(atBoulderAngle / ONE_EIGHTY * PI) /
          sin(radiansAtPole);
     boulderDistance = DISTANCE_WALKED * sin(atPitAngle / ONE_EIGHTY * PI) /
          sin(radiansAtPole);
}