C++ Assignment: How Far is That Plane?

Thu Aug 14 2025
Updated: Fri Aug 15 2025
Facebook share linkTwitter/X share linkLinkedIn share linkReddit share linkReddit share link

Introduction

This problem set will give you practice on arithmetic and introduce you to C++ libraries. In this problem set, you will use variables to store data input by a user, perform arithmetic using the data contained within the variables, and output your findings using the terminal. Be sure to read this problem set thoroughly.


Program: Airplane Compass

You own an airplane company that needs software to be developed for their flight systems. Specifically, the company wants to know how far their airplanes are from their airports. From your algebra classes you recall being able to find the distance between two points using either the manhattan distance:

manDist=x2x1+y2y1manDist = |x2 - x1| + |y2 - y1|

Or the euclidean distance:

eucDist=((x2x1)2+(y2y1)2)eucDist = √((x2 - x1)^2 + (y2 - y1)^2)

So you devise an algorithm to calculate the distance between two points (i.e. the plane and the airport) using both of these formulas as well as calculating the x-axis distance and y-axis distance so that the pilots and air traffic controllers have multiple measures of the plane’s distance.

This algorithm follows the following steps:

  1. The (x, y) coordinate of the plane is read into floating-point variables.
  2. The (x, y) coordinate of the airport is read into floating-point variables.
  3. The x-axis distance is calculated by performing the |x2 - x1| (where x2 is the plane’s x-coordinate and x1 is the airport’s x-coordinate) part of the manhattan distance formula.
  4. The y-axis distance is calculated by performing the |y2 - y1| (where y2 is the plane’s y-coordinate and y1 is the airport’s y-coordinate) part of the manhattan distance formula.
  5. The manhattan distance is calculated (where x2/y2 are the plane coordinates and x1/y1 are the airport coordinates).
  6. The euclidean distance is calculated (where x2/y2 are the plane coordinates and x1/y1 are the airport coordinates).
  7. The coordinates and values calculated are output to the screen in a pretty format using <iomanip>.

Note: The <cmath> library must be used to calculate the absolute value, square root, and exponents.


Output Formatting Tips

As with every assignment your output must match CodeGrade perfectly. Below are some tips to help match the formatting:

  • The columns of the coordinates table should be left aligned.
  • The columns of the distances table should be right aligned.
  • The headers rows can just be output as strings and the same separator can be used in both tables. The separator is:
    +-----------+-----------+-------------+-------------+
  • The ends of the coordinate/distance calculation row and separator between data pieces are produced by outputting: "| ", " | ", or " |".
  • All numerical values should be output with set widths:
    • The first and second columns should have a width of 9.
    • The third and fourth columns should have a width of 11.
  • The default fill should be used.
  • All numerical values should be output in fixed point notation.
  • All numerical values should be output with a precision of 2.

Example Program Interaction

An example of an interaction with your program is shown below. Your output must match this output exactly.

Output 1

Alexs-iMac:A2 alex$ g++ main.cpp Alexs-iMac:A2 alex$ ./a.out Enter plane’s x coordinate: 0 Enter plane’s y coordinate: 0 Enter airport’s x coordinate: 5 Enter airport’s y coordinate: 5 Coordinates: +-----------+-----------+-------------+-------------+ | plane-x | plane-y | airport-x | airport-y | +-----------+-----------+-------------+-------------+ | 0.00 | 0.00 | 5.00 | 5.00 | +-----------+-----------+-------------+-------------+ Distances: +-----------+-----------+-------------+-------------+ | x-axis | y-axis | manhattan | euclidean | +-----------+-----------+-------------+-------------+ | 5.00 | 5.00 | 10.00 | 7.07 | +-----------+-----------+-------------+-------------+

See CodeGrade for more examples.

Note: The line breaks after the inputs in the example output formatting above are the default ones C++’s cin and getline() automatically apply after reading an input from the user. CodeGrade supplies input in a different manner (Linux redirection) to programs so in CodeGrade's auto tests there will not be line breaks after the inputs.


Submission Instructions

  1. Save: Save your code as main.cpp. Do not ignore this step or save your file(s) with different names.

  2. Submit: Your program source code must be submitted via CodeGrade as a properly named .cpp file prior to the deadline to receive full credit. Any submissions after the deadline will be subject to the class’ late policy.