/****************************************************************************** * * Name : Scott Kristjanson * Number : 123456789 * email : skristja@sfu.ca * Course : Cmpt135 * Assmt : 1 * Question : B2 * * File : Distance.cpp * * This implements the Distance program for Assignment 1 Question B2. * This program prompts the user for two pairs of coordinates, stores them * in two Point structs, then computes the distance between these points to * three decimal points. * ******************************************************************************/ #include #include #include #include using namespace std; /****************************************************************************** * * Point * ===== * Data structure that stores the (x,y) coordinates for a point on a plane. * ******************************************************************************/ struct Point { int x; int y; }; /****************************************************************************** * * input * ===== * Reads in two integer values of cin and stores them in point * * Inputs: * point - updated with the user entered x and y coordinates * * Returns: * point - updated with new coordinates * ******************************************************************************/ void input(Point& point) { cin >> point.x >> point.y; } /****************************************************************************** * * output * ====== * Displays the coordinates of the specified point in the form (x,y) * * Inputs: * point - the Point to be displayed to cout * * Returns: * Nothing * ******************************************************************************/ void output(const Point& point) { cout << "(" << point.x << "," << point.y << ")"; } /****************************************************************************** * * Main function * ============= * Main promts for two points and computes the distance between them. * ******************************************************************************/ int main(int argc, char **argv) { Point point1, point2; int dx, dy; double distance; cout << "Specify point1 integer coordinates as x y: "; input(point1); cout << "Specify point2 integer coordinates as x y: "; input(point2); // Calculate the distance from point1 to point2 dx = abs(point1.x - point2.x); dy = abs(point1.y - point2.y); distance = sqrt(dx*dx + dy*dy); // Set up cout to display doubles with 3 decimal places cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(3); // Display the distance cout << "Distance from point1 "; output(point1); cout << " to point2 "; output(point2); cout << " = " << distance << endl; }