Thread: Function Pointers...Need Help

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    2

    Function Pointers...Need Help

    Hi everyone,
    A project has been assigned which I need to solve a given nonlinear equation by various methods. Everything's OK, but I've a problem about reading the equation from the file. For example, I need to solve the equation sin(x)+exp(-x)=0 (All equations will be of the form f(x)=0, so the file will contain sin(x)+exp(x) only). How can I plug this equation in the program? What I want to do is
    y=(The equation read from file) in somewhere in the code. Is there a way to implement that, using function pointers? Thanks for your help!

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    *sigh* So you need help with basic algebra and help with basic C, right? Ok, that was unnecessarily mean but nonetheless, you need to just read your lines into a buffer. Then parse that buffer. Then you can try and solve it as a linear expression.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Are you sure that the file will contain just the text "sin(x)+exp(x)", or will it perhaps be legitimate C code (as in a complete, compilable function)?

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Alright, look at this:

    Example:
    Code:
    #include <math.h>
    #include <stdio.h>
    
    typedef float (*function_1arg)(float);
    
    struct math_func
    {
        char name[64];
        function_1arg function;
    };
    
    struct math_func funcs[] = 
    {
        {"sin", sinf},
        {"cos",cosf},
        {"sqrt",sqrtf},
        {"exp",expf}
    };
    
    // You can handle the parsing part of your code yourself bud, but from here you'd do something
    // like
    if(stricmp(opcode, funcs[i].name) == 0)
      funcs[i].function(atof(operand));
    In my pseudo code part assume that opcode and operand are buffers containing the name of the function and the value which they are working with (respectively) and you are looping with i through the funcs array.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    2
    To tell the truth, I'm a newbie and I didn't know what "atof" does until you mentioned it. Anyway, I'm posting my code before asking some questions.

    Code:
    #include <stdio.h>
    #include <math.h>
      
    double f(double x)
    {
        return 5*cos(3*x) - 2*x*x*x;
    }
      
    double SecantMethod(double xn_1, double xn, double e, int m)
    {
        int n;
        double d;
        for (n = 1; n <= m; n++)
        {
            d = (xn - xn_1) / (f(xn) - f(xn_1)) * f(xn);
            if (fabs(d) < e)
                return xn;
            xn_1 = xn;
            xn = xn - d;
        }
        return xn;
    }
      
    int main(void)
    {
        printf("&#37;0.15f\n", SecantMethod(0, 1, 5E-11, 100));
        return 0;
    }
    So, what I need is that the code will read 5*cos(3*x) - 2*x*x*x from the file and use it in SecantMethod function. Thus, the main problem is I have to keep this function variables still as variables in the code too. Parsing this sentence from the file is too difficult to me, that's why I've asked if there was a way to put this "5*cos(3*x) - 2*x*x*x" directly into the code. I know it sounds stupid, but parsing this sentence requires additional effort on paranthesis and powers of x which fears me the most. By the way thanks for your help.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Code:
    Parsing sets the men apart from the boys (and without question the women apart from the boys too :) ). You could use some standard types of notation such as using { and } to delimit functions. A lot of math parsers use those. Its all a matter of preference, in the end. I don't think its too difficult to just break things into function and argument and/or operand/operator/operand. If you keep things boneheadedly simple like that then the hard part will be getting the data by itself, but using it will then be very simple. 
    
    atof() just converts a string like "2.171" to a float. But there is one thing I should point out. If this is a calculator program, you may want to read up on the accuracy of doubles. You will be astonished to find out they are not as good as grandpa makes them sound.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  2. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM
  3. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. function pointers and member functions
    By thor in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2002, 04:22 PM