Distance Between 2 Places
Collapse Content
Show Content
You're creating travel software, and need to calculate the distance between 2 locations located at (x1, y1) and (x2, y2) on a 2D grid. You will be given a list of doubles as input in the following order: x1, y1, x2, y2. Calculate and print the distance between the cities, rounded to 3 decimal places.
For example, given {0.0, 5.5, -1.0, 3.5}
as input, calculate the distance between (0.0, 5.5) and (-1.0, 3.5), which is sqrt(5) ≃ 2.23607, so you should print 2.236
Challenge
Print the distance between 2 locations to 3 decimal locations.
Please sign in or sign up to submit answers.
Alternatively, you can try out Learneroo before signing up.
Comments
thales
Jul 20, 12:54 PMIsnt there an esier way to round up numbers to n decimal places?
a solution:
my code
Bolke
Jul 29, 9:05 AMWe have to make a precision, for which there is no method? That's brutal.
Bolke
Jul 29, 9:36 AMI finally got the precision done, but only by copying and pasting a solution for that issue from Quora. It really would be helpful if we were led to a solution, sometimes. Right now I don't feel I'm learning from this.
Learneroo
Jul 31, 5:22 PMIn this case, Googling for how to round numbers would be good practice for real world programming.