In mathematics, the distance between one point (A) and another point (B), each with coordinates (x,y), can be computed by taking the differences of their x coordinates and their y coordinates and then squaring those differences. The squares are added and the square root of the resulting sum is taken and… voila! The distance. Assume that Point has already been defined as a structured type with two double fields, x and y. Define a function dist that takes two Point arguments and returns the distance between the points they represent.

1
In mathematics, the distance between one point (A) and another point (B), each with coordinates (x,y), can be computed by taking the differences of their x coordinates and their y coordinates and then squaring those differences. The squares are added and the square root of the resulting sum is taken and… voila! The distance. Assume that Point has already been defined as a structured type with two double  fields, x and y. Define a function dist that takes two Point arguments and returns the distance between the points they represent.

LANGUAGE: C++

CHALLENGE:

In mathematics, the distance between one point (A) and another point (B), each with coordinates (x,y), can be computed by taking the differences of their x coordinates and their y coordinates and then squaring those differences. The squares are added and the square root of the resulting sum is taken and… voila! The distance. Assume that Point has already been defined as a structured type with two double fields, x and y. Define a function dist that takes two Point arguments and returns the distance between the points they represent.

SOLUTION:

double dist (Point p1, Point p2){
    return sqrt(  (p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y));
}

One comment

Add comment