Using Struct
1. Program Sample Run 1
For first point:
Enter the value of x coordinate: 0
Enter the value of y coordinate: 3
For second point:
Enter the value of x coordinate: 4
Enter the value of y coordinate: 0
The distance between two given points is 5.
2. Program Sample Run 2
For first point:
Enter the value of x coordinate: -2.1
Enter the value of y coordinate: 3
For second point:
Enter the value of x coordinate: -9.12
Enter the value of y coordinate: 5
#include <iostream>
struct Point
{
double x, y;
};
double distance(Point p1, Point p2)
{
return sqrt((p2.x - p1.x) * (p2.x - p1.x) + (p2.y - p1.y) * (p2.y - p1.y));
}
int main()
{
Point p1, p2;
std::cout << "For first point:\nEnter the value of x coordinate : ";
std::cin >> p1.x;
std::cout << "Enter the value of y coordinate : ";
std::cin >> p1.y;
std::cout << "For second point:\nEnter the value of x coordinate : ";
std::cin >> p2.x;
std::cout << "Enter the value of y coordinate : ";
std::cin >> p2.y;
std::cout << "The distance between two given points is " << distance(p1, p2) << ".\n";
system("pause");
return 0;
}
Comments
Leave a comment