Answer to Question #18913 in C++ for mounisha

Question #18913
C++ program that takes the (x,y) coordinates of two points
and outputs the distance between them using constructors?
1
Expert's answer
2012-11-20T11:35:56-0500
#include <iostream>
#include <math.h>
using namespace std;

struct Point {
double x;
double y;
Point() { //constructor 1
& x = 0;
& y = 0;
}
Point(double _x, double _y) { //constructor 2
& x = _x;
& y = _y;
}
};

int main() {
double x1, x2, y1, y2;
cout << "Enter coordinate of two points " << endl << endl;
cout << "Point 1" << endl;
cout << " x: ";
cin >> x1;
cout << " y: ";
cin >> y1;
cout << "Point 2" << endl;
cout << " x: ";
cin >> x2;
cout << " y: ";
cin >> y2;
Point p1(x1, y1), p2(x2, y2); //use constructor
double dx = p2.x - p1.x;
double dy = p2.y - p1.y;
double distance = sqrt(pow(dx, 2) + pow(dy, 2));
cout << endl << "Distance between two points is " << distance << endl;
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

LATEST TUTORIALS
APPROVED BY CLIENTS