Thread: Trying to create an exp(x) program

  1. #1
    Registered User toadkiwi's Avatar
    Join Date
    Feb 2008
    Posts
    31

    Trying to create an exp(x) program

    Hey all. I'm experimenting with a program that is in relation to an assignment I have. All I'm trying to do is have the user input a number, and then have the program raise "e" (as in 2.71828...) to that number. I've got something, but it's not working like I need it to. I know my program doesnt include it yet, but I want to have a printf statement saying something like "exp(x) is <answer>". Can anyone see what I'm doing wrong?

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
            double x;
            double b;
    
            printf("Enter an exponent for base e\n");
            scanf("&#37;lf", &x);
    
            b = double exp(x);
            return(b);
    }
    Last edited by toadkiwi; 02-28-2008 at 07:37 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Once you take out the erroneous double in "b = double exp(x);" everything looks like it should work.

  3. #3
    Registered User toadkiwi's Avatar
    Join Date
    Feb 2008
    Posts
    31
    I had tried that and I get a crazy error:

    Code:
    Undefined                       first referenced
     symbol                             in file
    exp                                 /var/tmp//ccsUdNwM.o
    ld: fatal: Symbol referencing errors. No output written to a.out
    collect2: ld returned 1 exit status

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And of course, somewhere along the way you should remember that to include the math library using gcc you need to add "-lm" to the end of your compilation line.

  5. #5
    Registered User toadkiwi's Avatar
    Join Date
    Feb 2008
    Posts
    31
    Forgot about that. But now there's a new problem. The program just returns exactly what I input. I.e. after it asks for a number, I input 6 and it returns the number 6.

  6. #6
    Registered User toadkiwi's Avatar
    Join Date
    Feb 2008
    Posts
    31
    Ok I figured it out:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    main()
    {
            double x;
            double b;
    
            printf("Enter an exponent for base e\n");
            scanf("%lf", &x);
    
            b = exp(x);
            printf("exp(%f) is equivalent to %lf\n", x, b);
    }
    But one last thing. The final printf (example) reads something like
    Code:
    exp(2.000000) is equivalent to 7.389056
    Is there a way to make it so that the program doesn't spit out so many decimal places? I.e. can it just say 2.00 instead of 2.0000000...?

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    printf uses &#37;f for doubles.

    you can use the field width thing:

    %1.2f

  8. #8
    Registered User toadkiwi's Avatar
    Join Date
    Feb 2008
    Posts
    31
    Thanks!

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    5
    what do you mean by add -lm to to the end of the compilation line

  10. #10
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    -l = GCC link 'flag', m = math library. So you're linking against the math library, ie support for math.h functions.

  11. #11
    Registered User
    Join Date
    Feb 2008
    Posts
    5
    so would it be like

    gcc-lm <filename>

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    More like:
    gcc -lm <filename>

    Without the space you are invoking the (presumably non-existent) gcc-lm instead of gcc.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Whenever on a Unix compliant system and need to know how to do something with a program, the first thing to do is 'man xxx' where xxx is the program you want to use. It might be a little difficult at first as man pages are mostly technical, but the first lines in the man page contain the various syntax rules to invoke xxx.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  3. How to create a file association program?
    By eShain in forum Windows Programming
    Replies: 1
    Last Post: 03-06-2006, 12:15 PM
  4. Trying to create a payroll calculation program
    By nadeni0119 in forum C++ Programming
    Replies: 1
    Last Post: 04-01-2003, 04:14 PM
  5. program won't create status bar
    By Unregistered in forum Windows Programming
    Replies: 7
    Last Post: 08-30-2001, 04:00 PM