Question

In: Computer Science

Define a structure Point. A point has an x- and a y-coordinate. Write a function double...

Define a structure Point. A point has an x- and a y-coordinate. Write a function double distance(Point a, Point b) that computes the distance from a to b. Write a program that reads the coordinates of the points, calls your function, and displays the result. IN C++

if possible please give //comments

Solutions

Expert Solution

// do comment if any problem arises

// code

#include <iostream>

#include <cmath>

using namespace std;

struct Point

{

    int x, y;

};

// this function calculates

double distance(Point a, Point b)

{

    // (x1-x2)^2 + (y1-y2)^2

    double d = pow(a.x - b.x, 2) + pow(a.y - b.y, 2);

    // squareroot of above

    return sqrt(d);

}

int main()

{

    Point a, b;

    // 1,1

    a.x = 1;

    a.y = 1;

    // 3,3

    b.x = 4;

    b.y = 5;

    cout << "Distance between (" << a.x << "," << a.y << ") and (" << b.x << "," << b.y << ") is: " << distance(a, b);

}

Output:


Related Solutions

Write  the following functions using the following structure for a point. struct Point { double x, y;...
Write  the following functions using the following structure for a point. struct Point { double x, y; }; (a) A function that passes a point and returns an integer (1, 2, 3, or 4) to indicate in which quadrant the point is located int findQuadrant(struct Point testpoint) { } (b) A function that passes two points and returns the distance between them. float distance(struct Point point1, struct Point point2) { } (c) A function that passes two points and generate the...
A Point Class Definition A two-dimensional point may be represented by an x- and y-coordinate. In...
A Point Class Definition A two-dimensional point may be represented by an x- and y-coordinate. In this problem, you will write a Point class which stores this information and provides useful methods to work with points. We have provided an __init__ definition for the Point class. You will note that this accepts two arguments, being the x- and y-coordinates of the point. We have also included a __repr__ method, which will provide a canonical string representation of the point. Make...
Begin by defining the data type Point that has two coordinate members x and y. Specifically,...
Begin by defining the data type Point that has two coordinate members x and y. Specifically, Point will be implemented as a structure; define operators as needed to satisfy problem requirements. b, prompt the user to input several (x,y) pairs. As the data is entered, store it in a vector of Points called orginal_points. Input is terminated with the EOF character (differs by OS type). To be clear, I am asking you to define an input stream operator to read...
In C++, 
 Define a structure Triangle that contains three Point members. Write a function that computes...
In C++, 
 Define a structure Triangle that contains three Point members. Write a function that computes the perimeter() of a Triangle. Write a program that reads the coordinates of the points, calls your function, and displays the result. C++
find the x-coordinate of the point, correct to two decimal places, on the parabola y=3.08-x^2 at...
find the x-coordinate of the point, correct to two decimal places, on the parabola y=3.08-x^2 at which the tangent line cuts from the first quadrant the triangle with the smallest area.
In object C Define a class called XYPoint that will hold a Cartesian coordinate (x, y),...
In object C Define a class called XYPoint that will hold a Cartesian coordinate (x, y), where x and y are integers. Define methods to individually set the x and y coordinates of your new point and retrieve their values. Write an Objective-C program to implement your new class and test it (main section of the file). Your test program needs to create two instances of your class. Make sure your program test prints out the values that you have...
public class Point { protected double x, y; // coordinates of the Point //Default constructor public...
public class Point { protected double x, y; // coordinates of the Point //Default constructor public Point() { setPoint( 0, 0 ); } //Constructor with parameters public Point(double xValue, double yValue ) { setPoint(xValue, yValue ); } // set x and y coordinates of Point public void setPoint(double xValue, double yValue ) { x = xValue; y = yValue; } // get x coordinate public double getX() { return x; } // get y coordinate public double getY() { return...
In a rectangular coordinate system, a positive point charge 5.0nC is placed at x=2cm, y=0cm and...
In a rectangular coordinate system, a positive point charge 5.0nC is placed at x=2cm, y=0cm and a negative point charge -5.0nC is placed at x=-2cm,y=0cm. Point P is at x=2cm, y=3cm K=8.99 x 10^9 (Nxm^2)/C^2 a) Calculate the magnitude of the force between the positive charge and negative charge b) Draw the electric field vector at point P, caused by the positive charge. Find its magnitude c) Draw the electric field vector at point P, caused by the negative charge....
Write a python function that accepts two integer values corresponding to the point (x, y). Check...
Write a python function that accepts two integer values corresponding to the point (x, y). Check whether the point is within the rectangle centered at (0, 0) with width 20 and height 15. For example, (-9, 7) is inside the rectangle and (11, 4) is outside the rectangle, as shown in the figure. Return True if the point falls within the rectangle and False otherwise
Write a function double mysqrt( double x ) which computes the value of the square root...
Write a function double mysqrt( double x ) which computes the value of the square root of x using the bisection method. First you need to set the left and right bounds for x. If 0<x<1 then lt = x and rt = 1 and the sqrt(x) is somewhere in between. If x > 1 then lt = 1 and rt = x and sqrt(x) is somewhere in between. In a loop you need to compute the mid value between...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT