Megan
Megan

Reputation: 11

Declare and define the function distance() to find the Euclidean distance between the two points (x[0], y[0]) and (x[1], y[1]) in c

Declare and define the function distance() to find the Euclidean distance : √((x1 - x2)² + (y1 - y2)²) between the two points (x[0], y[0]) and (x[1], y[1]) This function should just calculate and return the answer.

C PROGRAM -----

double distance(double x[], double y[]) ;

What else am I supposed to put. Do I include the eculidean distance in this function or create a new one?

Upvotes: 0

Views: 1143

Answers (2)

Pol
Pol

Reputation: 103

This is the simplest distance() function definition:

`double distance(double x, double y){
    return 1/sqrt(((x[0]-x[1])*(x[0]-x[1])+(y[0]-y[1])*(y[0]-y[1])));
}`

Note that if you are using it to compare distances it's fastest to use the square of the distances.

Upvotes: 0

musical_coder
musical_coder

Reputation: 3896

double distance(double x[], double y[]);

is the function declaration.

double distance(double x[], double y[]) {
    //Write code here that returns a double
}

is the function definition.

Looks like the problem wants you to do both.

Upvotes: 1

Related Questions