C++ Practice Problem – Calculate the Distance Between Two Points

Time Limit: 2s Memory Limit: 192MB

Problem Description

Input the coordinates of two points (X1,Y1), (X2,Y2), and calculate and output the distance between the two points.

Input Format

Input data consists of multiple groups, each group occupies one line, consisting of 4 real numbers representing x1, y1, x2, y2, separated by spaces.

Output Format

For each group of input data, output one line, with the result rounded to two decimal places.

Sample Input

0 0 0 1

0 1 1 0

Sample Output

1.00

1.41

Code

#include <iostream>#include <iomanip>#include <cmath>using namespace std;
int main() {    double x1, y1, x2, y2;
    // Multiple inputs    while (cin >> x1 >> y1 >> x2 >> y2) {        // Calculate distance        double distance = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
        // Output result, rounded to two decimal places        cout << fixed << setprecision(2) << distance << endl;    }
    return 0;}

Output Result

C++ Practice Problem - Calculate the Distance Between Two Points

Code Explanation

  1. Header Files

  • <span><iostream></span>: Input and output

  • <span><iomanip></span>: Format output (retain decimal places)

  • <span><cmath></span>: Mathematical functions (sqrt, pow)

  • Handling Multiple Inputs

    • Using <span>while (cin >> x1 >> y1 >> x2 >> y2)</span> to read multiple sets of data

  • Distance Calculation

    • <span>pow(x2 - x1, 2)</span>: Calculate the square of the difference in x-coordinates

    • <span>pow(y2 - y1, 2)</span>: Calculate the square of the difference in y-coordinates

    • <span>sqrt()</span>: Take the square root to get the distance

  • Formatted Output

    • <span>fixed</span>: Fixed decimal format

    • <span>setprecision(2)</span>: Retain two decimal places

    This is a problem to calculate the distance between two points, using the Euclidean distance formula.

    Distance Formula

    The distance formula between two points $(x_1, y_1)$ and $(x_2, y_2)$ is:

    C++ Practice Problem - Calculate the Distance Between Two Points

    Another Implementation (without using pow)

    double dx = x2 - x1;double dy = y2 - y1;double distance = sqrt(dx * dx + dy * dy);

    This method is slightly more efficient.C++ Practice Problem - Calculate the Distance Between Two Points

    C++ Basic Tutorial Collection

    C++ Practice Problem - Calculate the Distance Between Two PointsC++ Basic Materials

    1. C++ Output

    2. C++ Variables

    3. C++ Input

    4. C++ Expressions

    5. IF Statements

    6. IF Applications

    7. WHILE Loops

    8. FOR Loops

    9. Arrays

    10. One-Dimensional Arrays

    11. Two-Dimensional Arrays

    12. C++ Functions

    13. C++ File Operations – Writing Files

    If you find it useful, please click on me

    C++ Practice Problem - Calculate the Distance Between Two PointsC++ Practice Problem - Calculate the Distance Between Two PointsC++ Practice Problem - Calculate the Distance Between Two PointsC++ Practice Problem - Calculate the Distance Between Two Points

    Leave a Comment