In this assignment, you will implement an algorithm that computes the distance b
ID: 3749549 • Letter: I
Question
In this assignment, you will implement an algorithm that computes the distance between two points using the distance formula.
Create a new file and name it distanceFormula.
Include the iostream and cmath header files.
Prompt the user to enter the x and y coordinates of the first point.
Read in the x and y values and save them in local variables called x1 and y1.
Prompt the user to enter the x and y coordinates of the second point.
Read in the x and y values and save them in local variables called x2 and y2.
Declare another variable called distance and set it equal to the distance. Use functionspow and sqrt to compute the power and square root.
Display the distance to the user.
Explanation / Answer
C++ Program according to the given instruction:
/*Include the iostream and cmath header files*/
#include <iostream>
#include <cmath>
using namespace std;
//main function
int main()
{
/*variable declaration*/
double x1,y1,x2,y2;
cout << "Enter the x and y coordinates of the first point";
cin >> x1>>y1;
cout << endl;
cout << "Enter the x and y coordinates of the first point";
cin >> x2>>y2;
cout << endl;
double distance= sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
cout << "The distance is " << distance;
return 0;
}
Output:
Enter the x and y coordinates of the first point18
24
Enter the x and y coordinates of the first point36
12
The distance is 21.6333