The problem with square and sqrt function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Blue sky

    The problem with square and sqrt function

    Hi ,I think the follow program is right in logical But why the
    compiler output :"square:declar ed 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%1 f%1f",&x1,&x2,& y1,&y2);

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

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

    return 0;
    }

  • Chris Dollin

    #2
    Re: The problem with square and sqrt function

    Blue sky wrote:
    Hi ,I think the follow program is right in logical But why the
    compiler output :"square:declar ed identifier"
    Surely "/un/declared identifier".
    distance=sqrt(s quare(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

    Comment

    • James Kuyper

      #3
      Re: The problem with square and sqrt function

      Blue sky wrote:
      Hi ,I think the follow program is right in logical But why the
      compiler output :"square:declar ed 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%1 f%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(s quare(x)+square (y));
      >
      printf("The distance is %f",distance) ;
      >
      return 0;
      }

      Comment

      • Chris Dollin

        #4
        Re: The problem with square and sqrt function

        Chris Dollin wrote:
        Blue sky wrote:
        >
        >Hi ,I think the follow program is right in logical But why the
        >compiler output :"square:declar ed identifier"
        >
        Surely "/un/declared identifier".
        >
        > distance=sqrt(s quare(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

        Comment

        • Flash Gordon

          #5
          Re: The problem with square and sqrt function

          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:declar ed identifier"
          >
          Surely "/un/declared identifier".
          Agreed.
          > distance=sqrt(s quare(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 smap@spam.cause way.com
          If emailing me use my reply-to address
          See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/

          Comment

          • James Dow Allen

            #6
            Re: The problem with square and sqrt function

            On Nov 4, 7:36 pm, Flash Gordon <s...@spam.caus eway.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

            Comment

            • Flash Gordon

              #7
              Re: The problem with square and sqrt function

              James Dow Allen wrote, On 05/11/08 04:48:
              On Nov 4, 7:36 pm, Flash Gordon <s...@spam.caus eway.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 smap@spam.cause way.com
              If emailing me use my reply-to address
              See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/

              Comment

              Working...