In: Computer Science
IN C++ LANGUAGE PLEASE:::
Distance calc. This question is fairly straightforward. Design (pseudocode) and implement (source code) a program to compute the distance between 2 points. The program prompts the user to enter 2 points (X1, Y1) and (X2, Y2). The distance between 2 points formula is: Square_Root [(X2 – X1)^2 + (Y2 – Y1)^2]
PSEUDO CODE:
1. enter first point : (x1,y1) and second point: (x2,y2)
2. calculateDistance( x1,y1,x2,y2):
i) find difference of the x co-ordinates and square
them (x2-x1)^2
ii) find difference of the y co-ordinates and square
them (y2-y1)^2
iii) find sum of (i) and (ii).
iv) find square of the sum found in (iv)
3. print the distance calculated.
c++
code
#include <iostream>
#include <cmath>
using namespace std;
float calculateDistance(float x1, float y1, float x2, float
y2)
{
return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2)); //
Square_Root [(X2 – X1)^2 + (Y2 – Y1)^2]
}
int main()
{
float x1,y1,x2,y2;
cout << "\n Enter the first point:\t";
cin>>x1>>y1;
cout << "\n Enter the second point:\t";
cin>>x2>>y2;
float distance = calculateDistance(x1,y1,x2,y2);
cout << "\n Distance between ("<< x1
<<"," <<y1<<") and ("<< x2 <<","
<<y2<<") is :
"<<distance<<"units."<<endl;
return 0;
}
/* OUPUT :
Enter the first point: 2 4
Enter the second point: 5 6
Distance between (2,4) and (5,6) is : 3.60555units.
*/