Answer to Question #298085 in C++ for Kelly

Question #298085

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

1
Expert's answer
2022-02-15T08:15:10-0500
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment