LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-16-2004, 10:23 AM   #1
nostrum
Member
 
Registered: Jun 2004
Location: New York
Distribution: Red Hat
Posts: 32

Rep: Reputation: 15
Problems with Sqrt()


Hi I'm basically having trouble getting this program to compute the distance between two points and according to gcc with -Wall flag enabled it's because sqrt() is an "undefined reference to sqrt", another thing people might want to notice is I used y3 instead of y1 because apparently according to gcc y1 is already taken variable. Any ideas why that is? Basically I just need to figure out the right syntax for sqrt() so any help would be greatly appreciated. Now here's the code:


/* Distance.C - Calculates the distance between two points on a plane */
#include <stdio.h>
#include <math.h>

char line[100]; /* line of input data */
double x1; /* coordinance of the first x coordinance */
double x2; /* coordinance of the seocnd x coordinance */
double y3; /* coordinance of the first y coordinance */
double y2; /* coordinance of the second y coordinance */
double distance; /* the distance between the two points */
double x; /* the square of the first coordinance */
double y; /* the square of the second coordinance */

int main()
{
printf("What are the coordinances of the two points(Use the format x1 y1 x2 y2)? ");

fgets(line, sizeof(line), stdin);
sscanf(line, "%lf %lf %lf %lf", &x1, &y3, &x2,&y2);

x = x2 - x1;
y = y2 - y3;
/* partial = ((x * x) + (y * y)); This was an attempt to get around the problem by using
a variable named partial and then just doing distance = sqrt(partial), but that dind't
workl either */


distance = sqrt((x * x) + (y * y));
printf("The distance between the points is %lf\n", distance);
return (0);
}
 
Old 07-16-2004, 11:37 AM   #2
backer
LQ Newbie
 
Registered: Jun 2004
Posts: 3

Rep: Reputation: 0
when you use something from math.h you have to link with libmath, using -lm on
the command line.
 
Old 04-15-2005, 12:24 PM   #3
SETXBera
LQ Newbie
 
Registered: Apr 2005
Location: Space Coast, FL
Distribution: Ubuntu 9.10
Posts: 17

Rep: Reputation: 1
nostrum, are you going through the Practical C Programming book by O'Reilly? If so, I am too and did my a little different.

For the variables i made them float instead of double to save memory, and my math was done a little different.

I did:
Code:
/*This program will find the distance between two points on a grid*/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

char line[10];/*input data to be converted*/
float xcoordf;/*first X coordinate*/
float ycoordf;/*first Y coordinate*/
float xcoords;/*second X coordinate*/
float ycoords;/*second Y coordinate*/
float xanswer;/*difference in X coordinates*/
float yanswer;/*difference in Y coordinates*/
float answer;/*average of the difference in both coordinates*/

main()
{
  (void)printf("Distance between two points.\n
      Enter the first x,y coordinates(using this format, no commas: x1 y1):");
  (void)fgets(line,sizeof(line),stdin);
  (void)sscanf(line,"%f %f",&xcoordf,&ycoordf);
 
  (void)printf("Enter the second set of coordinates(x2 y2, no commas):");
  (void)fgets(line,sizeof(line),stdin);
  (void)sscanf(line,"%f %f",&xcoords,&ycoords);
  
  xanswer=(xcoords-xcoordf)*(xcoords-xcoordf);
  yanswer=(ycoords-ycoordf)*(ycoords-ycoordf);
  answer=sqrt(xanswer+yanswer);
  (void)printf("The difference in the two points is %.2f.\n",answer);
  return(0);
}
Was running into the same brickwall as you until I linked to libmath under the suggestion of backer. Compiles and links with no errors or warnings of any kind.

Before I picked up Practical C Programming I was using C For Dummies, that book really walked you along slowly. I do not recommend it for anyone with a working knowledge of computers.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sqrt function lmvent Programming 6 10-20-2005 07:53 PM
red hat 9.0 gcc (ceil, floor and sqrt function errors) ?? gemini_shooter Linux - Enterprise 1 06-04-2005 04:23 PM
How do you write your own sqrt()? The_Nerd Programming 6 11-02-2004 08:08 PM
Bash sqrt problem Dimitris Programming 1 08-26-2003 03:30 PM
sqrt() function IamDaniel Programming 6 08-09-2003 10:24 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration