Exercise 5-3 Use a function to calculate the distance between two points (10 point(s))

This question requires the realization of a function to find the distance between the coordinates (x1,y1) and (x2,y2) of any two points on a given plane.

Function interface definition:

double dist( double x1, double y1, double x2, double y2 );

The parameters passed by the user are the coordinates (x1, y1) and (x2, y2) of two points on the plane. The function dist should return the distance between the two points.

Sample referee test procedure:

#include <stdio.h>
#include <math.h>

double dist( double x1, double y1, double x2, double y2 );

int main()
{
    
        
    double x1, y1, x2, y2;

    scanf("%lf %lf %lf %lf", &x1, &y1, &x2, &y2);
    printf("dist = %.2f\n", dist(x1, y1, x2, y2));

    return 0;
}

/* 你的代码将被嵌在这里 */

Input sample:

10 10 200 100

Sample output:

dist = 210.24

answer:

double dist( double x1, double y1, double x2, double y2 )
{
    
    
	return sqrt(pow(x1-x2,2) + pow(y1-y2,2));
}

Guess you like

Origin blog.csdn.net/qq_44715943/article/details/114583335