1. Sep 1, 2015

### SnakeDoc

1. The problem statement, all variables and given/known data
I have make a program that solves the distance between 2 points on a triangle.

2. Relevant equations
none

3. The attempt at a solution

Code (Text):

/* homework 1 */

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

int main(void)
{
// Declare and initialize variables
float x1=1, y1=5, x2=4, y2=7;
float dx, dy, distance;

// Compute sides of a right triangle
dx = x2 - x1;
dy = y2 - y1;

distance = sqrt (dx*dx + dy*dy);

// Print distance
printf("The distance between the two points is: %5.2f \n",distance);

// Exit program
return(0);
}
I keep getting a an error about an undefined reference to sqrt
Did I do something wrong?

Last edited by a moderator: Sep 7, 2015
2. Sep 1, 2015

### Staff: Mentor

Your program runs fine for me, although I get this warning:

warning C4244: '=' : conversion from 'double' to 'float', possible loss of data

due to the fact that sqrt() returns a double, and you're storing the returned value in a smaller type. I suspect you don't have things set up correctly so that the compiler can't find the header files (math.h in particular).

3. Sep 2, 2015

4. Sep 3, 2015

### HallsofIvy

I use Pascal rather than C++ but in Pascal, if you reference a "math" library in order to use the "sqrt" function, you would have to use "math.sqrt(dx*dx+ dy*dy)".

5. Sep 3, 2015

### Staff: Mentor

This is C, not C++, and you simply use sqrt, without a math. attached to it. You also need to #include the math header,
Code (C):

#include <math.h>

For historical reasons, the math functions are not part of the standard library, so the math library must be specifically linked to. For instance
Code (Text):

gcc -O mymathcode.c -lm

6. Sep 3, 2015

### Staff: Mentor

I'm guessing that the OP doesn't have his compiler installation set up correctly, with the linker unable to find the code for the math library (DrClaude's earlier suggestion). Less likely, I think, is my earlier suggestion that the compiler can't find the headers, but this is still a possibility.

7. Sep 7, 2015