Hi,
I'm a bit new to programming in general, really I've just picked it up
as a hobby, and so I've started by reading "Practical C Programming"
by Steve Oualline. Things have been going fine in the book until I
reached exercise 61 in the book, which reads:
Write a program to find the square of the distance between two points.
(For a more advanced problem, find the actual distance. This problem
involves using the standard function sqrt. Use your help system to
find out more about how to use this function.)
I did fine with the basic problem, but I can't seem to compile when I
try to use sqrt. Here is my code:
#include <stdio.h>
#include <math.h>
char coordinate[50]; /* input string */
int myx1; /* first x coordinate */
int myx2; /* second x coordinate */
int myy1; /* first y coordinate */
int myy2; /* second y coordinate */
double sqrdist; /* distances square */
double distance; /* actual distance between two points */
int absxsquare; /* absolute value of (x1 + x2) ^2 */
int absysquare; /* absolute value of (y1 + y2) ^2 */
int main()
{
printf("Enter the X value of the first coordinate: ");
fgets(coordinate, sizeof(coordinate), stdin);
sscanf(coordinate, "%d", &myx1);
printf("Enter the Y value of the first coordinate: ");
fgets(coordinate, sizeof(coordinate), stdin);
sscanf(coordinate, "%d", &myy1);
printf("Enter the X value of the second coordinate: ");
fgets(coordinate, sizeof(coordinate), stdin);
sscanf(coordinate, "%d", &myx2);
printf("Enter the Y value of the second coordinate: ");
fgets(coordinate, sizeof(coordinate), stdin);
sscanf(coordinate, "%d", &myy2);
absxsquare = fabs ((myx1  myx2) * (myx1  myx2));
absysquare = fabs ((myy1  myy2) * (myy1  myy2));
sqrdist = absxsquare + absysquare;
distance = sqrt(sqrdist);
printf("\nThe square of the distance between those two points
is %f",sqrdist);
printf("\nThe actual difference is %f\n",distance);
return(0);
}
And here are my compile errors:
/tmp/ccQe7XsT.o: In function `main':
/home/madducks/cprogs/ex61.c:34: undefined reference to `sqrt'
collect2: ld returned 1 exit status
I thought I had implemented the sqrt as the man page for sqrt
explained, but as you might guess by my code, I'm more than a bit
lost. Any help would be greatly appreciated.
Thanks,
Aric
