Results 1 to 9 of 9

Thread: What's wrong ?

  1. #1
    Join Date
    Jun 2008
    Location
    Mumbai,India
    Posts
    43

    Default What's wrong ?

    What's wrong in this simple code ?


    #include<math.h>
    #include<stdio.h>
    #define pi 3.1406
    #define max 180
    int main()
    {
    int angle;
    float x,y;
    angle=0;
    printf(" Angle cos(angle)\n\n");
    while(angle<=max)
    {
    x=(pi/max)*angle;
    y= cos(x);
    printf("%15d %13.4f\n",angle,y);
    angle=angle+10;
    }
    return 0;
    }


    Error is
    cc first.c
    /tmp/ccrCUfIm.o: In function `main':
    first.c.text+0x3b): undefined reference to `cos'
    collect2: ld returned 1 exit status

    Any ideas ?

  2. #2
    Join Date
    Jun 2008
    Location
    Albuquerque, NM, USA
    Posts
    87

    Default Re: What's wrong ?

    Nothing is wrong with the above C program. What is wrong is not including the math library "-lm" when you compile. It's a linker option, "-llibrary". See man cc or #cc in konqueror.

    # cc first.c -lm
    It's really that much fun!
    Geeko Samurai (cc)
    My Recent Tracks

  3. #3
    Join Date
    Jun 2008
    Location
    Mumbai,India
    Posts
    43

    Default Re: What's wrong ?

    Here is the output
    cc first.c -lm
    /tmp/ccs6xrnu.o: In function `main':
    first.c: (.text+0x3b): undefined reference to `Cos'
    collect2: ld returned 1 exit status

  4. #4
    Join Date
    Jun 2008
    Location
    Mumbai,India
    Posts
    43

    Default Re: What's wrong ?

    thanks it worked... I had written 'Cos' instead of 'cos'..)

  5. #5
    ab@novell.com NNTP User

    Default Re: What's wrong ?

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    You could probably define 'pi' quite a bit closer too...

    3.14159265

    Good luck.





    tanmaya wrote:
    | thanks it worked... I had written 'Cos' instead of 'cos'..)
    |
    |
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.2 (GNU/Linux)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

    iD8DBQFIV08V3s42bA80+9kRArp9AKCI3Cutu/pARcRL2Q1yT5HJTiz1KwCff7jq
    joLcvFNvdAPcR/FF4hZaOpw=
    =dfXg
    -----END PGP SIGNATURE-----

  6. #6
    Join Date
    Jun 2008
    Posts
    42

    Default Re: What's wrong ?

    Option is -lm (dash el em), it really works.

  7. #7
    Join Date
    Jun 2008
    Location
    Mumbai,India
    Posts
    43

    Default Re: What's wrong ?

    yes i defined the value of pi more accurately and the results are bit different this time

  8. #8
    Join Date
    Mar 2008
    Location
    Phuket, Thailand
    Posts
    26,550
    Blog Entries
    37

    Default Re: What's wrong ?

    Please, next time you make such a post, can you make the Title more descriptive? "Whats wrong?" is not helpful as a Title.

    Thank-you.

  9. #9
    Join Date
    Jun 2008
    Location
    Mumbai,India
    Posts
    43

    Default Re: What's wrong ?

    i will take care of this mistake next time

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •