Level up your toolkit with <cmath>
and <iomanip>
. You’ll use math functions—think sqrt
and pow
—to compute precise distances, and then present those numbers cleanly with setprecision
, fixed
, setw
, and friends. The goal isn’t just to get the right answer, but to make that answer readable, consistent, and easy to verify.
These habits matter beyond class: engineers calculate tolerances, data analysts report rates and errors, finance apps round currency correctly, and robotics/IoT code turns vector math into decisions—always with human-friendly output. Nail the math and the formatting here, and you’ll write programs that both compute and communicate well.
You are creating a video game and need to tell how far monsters are away from the player. This way the program knows if the player is close enough to attack a monster, or if the player gets too close to a monster that auto attacks. There are two ways to calculate the distance between the player and the monster: manhattan distance and euclidean distance. You want to know which is more accurate for the game. Write a program that computes the distance between the player and monster, then outputs the results. You will need to:
Read the (x, y) coordinates (as separate inputs for x and y) of the player into two integer variables.
Read the (x, y) coordinates (as separate inputs for x and y) of the monster into two integer variables.
Compute the distance along the x-axis and y-axis and save them into integer variables (see below for formulas).
Compute the manhattan and euclidean distances between the points and save them into floating-point variables (see below for formulas).
Output the distances then visualize the distances using <iomanip>
(see criteria on how to below).
X-Axis Distance:
Y-Axis Distance:
Manhattan Distance:
Euclidean Distance:
When outputting each distance it must be in the format:
Label:...........value p-m
For the first line, after the label is output for which distance is being output use a width to make the rest of the line total out to 22 spaces in the entire line. In this width output the value of the distance. Fill the blank spaces of the width with `’.``.
For the second line the amount of dashes between the p and m needs to be equal to the distance calculated (typecast as an integer if it is a floating-point value). Use a width with the blank spaces filled with ’-’
to achieve this.
Since the coordinates are integers the euclidean distance is the only distance that will be a floating-point value with a decimal. For the euclidean distance only apply a precision of 2.
These visualizations instructions are intentionally vague to give you practice with <iomanip>
.
An example of an interaction with your program is shown below. Your output must match this output exactly.
Player x: 3 Player y: 5 Monster x: 10 Monster y: 10 Distances from Player to Monster: X-Axis:..............7 p-------m Y-Axis:..............5 p-----m Manhattan:..........12 p------------m Euclidean:........8.60 p--------m
See CodeGrade for more examples.
Note: The line breaks after the inputs in the example output formatting above are the ones placed into the terminal by the user hitting enter
on their keyboard to input. CodeGrade does not enter values with a keyboard, but rather supplies input via Linux redirection to programs. So in CodeGrade's auto tests there will not be line breaks after the inputs.
Save: Save your code as main.cpp
. Do not ignore this step or save your file(s) with different names.
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.