0

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?

Megan
  • 11
  • 1

2 Answers2

1
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.

musical_coder
  • 3,886
  • 3
  • 15
  • 18
  • How do I include the euclidean distance formula distance = sqrt(pow(x[0] + x[1], 2) + pow(y[0]-y[1]2); – Megan Oct 31 '14 at 02:24
  • First you need to attempt to write the code on your own. After that, if you run into any problems, please post a new question that includes the code you wrote, the line(s) that are having problems, and what the specific issues are. Don't tack it onto this post, since we like to keep one question per post. – musical_coder Oct 31 '14 at 02:30
  • NO -- **do not post a new question**. Better, simply edit this question and **add the new information as an addendum to the original**. After you write the code, compile it. You will need to include the math library `-lm` in your `gcc` command. (i.e. `gcc -Wall -Wextra -o myprogram myprogram.c -lm`) – David C. Rankin Oct 31 '14 at 03:27
0

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.

Pol
  • 103
  • 1
  • 2
  • 8