Question #24421

Write, compile and execute a C++ program that calculates the distance between two points whose coordinates are (7, 12) and (3, 9). Use the fact that the distance between two points having coordinates (x1, y1) and (x2, y2) is distance = sqrt ([x1 - x2]2 + [y1 - y2]2). When you have verified that your program works correctly, by calculating the distance between the two points manually, use your program to determine the distance between the points (-12, -15) and (22, 5).

Expert's answer

#include <cmath>

#include <conio.h>

#include <iostream>

using namespace std;

double distance(double x1, double y1, double x2, double y2)

{

return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));

}

int main()

{

cout << "The distance between points (7, 12) and (3, 9) is: "

<< distance(7, 12, 3, 9) << endl;

cout << "The distance between points (-12, -15) and (22, 5) is: "

<< distance(-12, -15, 22, 5) << endl;

_getch();

return 0;

}

#include <conio.h>

#include <iostream>

using namespace std;

double distance(double x1, double y1, double x2, double y2)

{

return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));

}

int main()

{

cout << "The distance between points (7, 12) and (3, 9) is: "

<< distance(7, 12, 3, 9) << endl;

cout << "The distance between points (-12, -15) and (22, 5) is: "

<< distance(-12, -15, 22, 5) << endl;

_getch();

return 0;

}

## Comments

## Leave a comment