By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,930 Members | 1,280 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,930 IT Pros & Developers. It's quick & easy.

The problem with square and sqrt function

P: n/a
Hi ,I think the follow program is right in logical But why the
compiler output :"square:declared identifier"
#include<stdio.h>
#include<math.h>

int main()
{
double x1;
double x2;
double y1;
double y2;
double x;
double y;
double distance;

printf("Enter the local of points: ");
scanf("%1f%1f%1f%1f",&x1,&x2,&y1,&y2);

x=fabs(x1-x2);
y=fabs(y1-y2);
distance=sqrt(square(x)+square(y));

printf("The distance is %f",distance);

return 0;
}

Nov 4 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Blue sky wrote:
Hi ,I think the follow program is right in logical But why the
compiler output :"square:declared identifier"
Surely "/un/declared identifier".
distance=sqrt(square(x)+square(y));
Who is `square` when they're at home? (There's no `square` in
`<match.h>`.) Did you mean `sqrt`?

--
'Don't be afraid: /Electra City/
there will be minimal destruction.' - Panic Room

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

Nov 4 '08 #2

P: n/a
Blue sky wrote:
Hi ,I think the follow program is right in logical But why the
compiler output :"square:declared identifier"
If that is the correct text of the error message, then your compiler is
defective. If an identifier is declared, that's never a good reason, in
itself, for issuing a diagnostic message. More to the point, the message
is not only pointless, but also inaccurate. The identifier "square" is
not declared anywhere in your program.

It's more likely that the compiler said "undeclared identifier". That is
a perfectly accurate description of your program, and more to the point,
it's precisely the biggest problem with your program. What you need is
to provide a declaration for square(). A definition is also needed, but
the definition could be in a different module.

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

int main()
{
double x1;
double x2;
double y1;
double y2;
double x;
double y;
double distance;

printf("Enter the local of points: ");
scanf("%1f%1f%1f%1f",&x1,&x2,&y1,&y2);

x=fabs(x1-x2);
y=fabs(y1-y2);
You don't provide any definition for square(), so I can't be certain,
but I assume that square(x) calculates and returns the square of x. That
being the case, why are you bothering with the fabs() calls? The square
of (x1-x2) is always the same as the square of fabs(x1-x2), all you're
doing is wasting a small amount of space in your program and wasting a
small amount of time when it executes. Most importantly, you're creating
the potential for confusion when the next person who reads this code has
to waste time thinking "Is there some obscure reason why it might make
sense to call fabs() here?"
distance=sqrt(square(x)+square(y));

printf("The distance is %f",distance);

return 0;
}
Nov 4 '08 #3

P: n/a
Chris Dollin wrote:
Blue sky wrote:
>Hi ,I think the follow program is right in logical But why the
compiler output :"square:declared identifier"

Surely "/un/declared identifier".
> distance=sqrt(square(x)+square(y));

Who is `square` when they're at home? (There's no `square` in
`<match.h>`.) Did you mean `sqrt`?
(fx:later) Duh, I am the stupidz. No, Blue sky didn't mean `sqrt`.
Bad hedgehog. No slugs for you.

Blue sky -- you'll have to write your own `square` function,
or do the squaring inline. Since `x` and `y` are simple variables,

distance = sqrt( x * x + y * y );

will do handily.

--
'Don't be afraid: /Electra City/
there will be minimal destruction.' - Panic Room

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

Nov 4 '08 #4

P: n/a
Chris Dollin wrote, On 04/11/08 11:49:
Blue sky wrote:
>Hi ,I think the follow program is right in logical But why the
compiler output :"square:declared identifier"

Surely "/un/declared identifier".
Agreed.
> distance=sqrt(square(x)+square(y));

Who is `square` when they're at home? (There's no `square` in
`<match.h>`.) Did you mean `sqrt`?
I doubt it. I suspect the OP it programming "by guess and by god" and
incorrectly guessed that there is a square function.

Other points you did not mention include:

The OP should flush stdout after outputting the prompt otherwise it
might not be displayed.

The value returned by scanf needs to be checked to see if the data was
correctly entered. Also scanf is not easy to use correctly.

Why on earth bother using fabs? The OP should thing about the result of
squaring a negative number.

There should be a newline at the end of the output. Otherwise it is not
guaranteed to be displayed and even if it is the result is harder to
read on many systems without it.
--
Flash Gordon
If spamming me sent it to sm**@spam.causeway.com
If emailing me use my reply-to address
See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/
Nov 4 '08 #5

P: n/a
On Nov 4, 7:36*pm, Flash Gordon <s...@spam.causeway.comwrote:
Why on earth bother using fabs?
The OP should thin[k] about the result of
squaring a negative number.
Presumably, since he doesn't check that
scanf() sets its targets, he was concerned
about squaring an imaginary number. (g)

James
Nov 5 '08 #6

P: n/a
James Dow Allen wrote, On 05/11/08 04:48:
On Nov 4, 7:36 pm, Flash Gordon <s...@spam.causeway.comwrote:
>Why on earth bother using fabs?
The OP should thin[k] about the result of
squaring a negative number.

Presumably, since he doesn't check that
scanf() sets its targets, he was concerned
about squaring an imaginary number. (g)
:-)

The OP was using fabs on the difference between two numbers and then
squaring the result of that. I don't think fabs will make the difference
NaNy less imaginary than it was :-)
--
Flash Gordon
If spamming me sent it to sm**@spam.causeway.com
If emailing me use my reply-to address
See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/
Nov 5 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.