SI204 Practice 12-Week Practicum

Problem 1
Write a program that reads in a sequence of courses given by angle (in degrees from North), velocity (in knots), and a time (in hours), and prints out the destination angle (in degrees from North) and distance (in nautical miles). The input should come either from a file or from the screen. The input format is as given in the examples below:
Sample Run Sample Input File
Courses Source (filename or SCREEN): courses1.txt
Finish 16.1146 nautical miles away at 340.901 degrees (from North).
course 30 deg @ 10 knots for 0.5 hours
course 330 deg @ 7 knots for 1.4 hours
course 310 deg @ 5 knots for 0.75 hours
end
Courses Source (filename or SCREEN): SCREEN
course 58 deg @ 9 knots for 2 hours
course 110 deg @ 7 knots for 1.5 hours
end
Finish 25.8258 nautical miles away at 76.686 degrees (from North).
I'll provide the main function you must use without modification! You will simply add in function prototypes and definitions.
main analyzeTrip xyToBearingDist
int main()
{
  // Get data source
  cout << "Courses Source (filename or SCREEN): ";
  string source;
  cin >> source;

  // Abalyze data to find destination
  double x = 0, y = 0;
  if (source == "SCREEN")
    analyzeTrip(cin,x,y);
  else
  {
    ifstream fin(source.c_str());
    if (!fin)
    {
      cout << "File not found!" << endl;
      exit(1);
    }
    analyzeTrip(fin,x,y);
  }

  // Report ending position
  double theta, r;
  xyToBearingDist(x,y,theta,r);
  cout << "Finish " 
       << r << " nautical miles away at " << theta 
       << " degrees (from North)." << endl;

  return 0;
}
The analyzeTrip(in,x,y) function is supposed to read the courses from stream in and store the cartesian coordinates of the final position (in nautical miles) in x and y. Note: bearing θ degrees from North is the same as angle 90 - θ from the x-axis. The xyToBearingDist(x,y,θ,r) function should convert the position in cartesian coordinates (x,y) to bearing-distance coordinates and store them in the variables θ and r. Because it's tricky to get the angle right - especially since bearing is measured clockwise from North - I'll give you a useful code fragment:
  double a = toDeg(atan(y/x));
  if (x < 0 && y > 0)
    theta = 270 - a;
  else if (x < 0 && y < 0)
    theta = 270 - a;
  else 
    theta = 90 - a;

Add the prototypes and definitions of the above described functions, and any helper functions you choose to define, to the above main to finish off the program.

Problem 2
Write a terminal plotting program. Your program should read a data file like in1.txt or in2.txt and procduce a plot in the terminal window of the data.
Sample Run Sample Input File
Enter file name: in1.txt


        ****             
      **    **           
     *        **         
    *           *        
  **             *       
**                *      
                   **   *
                     *** 
25 values
-1 -1 0 0 1
2 3 3 4 4
4 4 3 3 2
2 1 0 -1 -2
-2 -3 -3 -3 -2
Enter file name: in2.txt


                                                           ***      
                                                         **   **    
               ***                                      *       *   
             **   **                                   *         ** 
            *       *                                 *            *
           *         *                               *              
          *           *       ***                   *               
         *             **   **   **                *                
*       *                ***       *              *                 
 **   **                            *            *                  
   ***                               **         *                   
                                       ***    **                    
                                          ****                      
68 values
-3 -4 -4 -5 -5 -5 -4
-4 -3 -2 -1 0 1 2 
2 3 3 3 2 2 1 
0 -1 -2 -2 -3 -3 -3
-2 -2 -1 -1 -1 -2 -2
-3 -4 -5 -5 -6 -6 -6
-7 -7 -7 -7 -6 -6 -5
-4 -3 -2 -1 0 1 2
3 4 4 5 5 5 4
4 3 2 2 1

You may assume that all values are integers. The basic idea is that the first row you print corresponds to the largest value you're plotting, then next row one less than that largest value, and so on down to the last row, which corresponds to the smallest value.


Christopher W Brown
Last modified: Mon Oct 25 14:08:16 EDT 2004