The problem with square and sqrt function

B

Blue sky

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;
}
 
C

Chris Dollin

Blue said:
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
 
J

James Kuyper

Blue said:
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?"
 
C

Chris Dollin

Chris said:
Surely "/un/declared identifier".


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
 
F

Flash Gordon

Chris Dollin wrote, On 04/11/08 11:49:
Surely "/un/declared identifier".
Agreed.


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

James Dow Allen

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
 
F

Flash Gordon

James Dow Allen wrote, On 05/11/08 04:48:
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 :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top