Thread: functions...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    30

    functions...

    Hello,

    I was wondering if there is a simple way to do this. This is just an example, but I have a function defined as x*y.
    So obviously if I provide values for x and y, it can tell me the product.

    But what I'm wondering is if there is some way to enter something like
    function(x,2)=2;
    and have it tell me what x would be (in this case 1).

    Just doing this doesn't work, but it's the idea of what I want to do.
    Grateful for any suggestions to do this simply...I want to apply it to a complicated function that would be difficult to solve for x.
    Thanks.

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Now I assume you meant function(x,2) == 2, since assigning a value to a function has no meaning in C.
    Yes, you could use a function int that way. Like:

    Code:
    #define MAX_NUMBER 1000000
    for(i= -MAX_NUMBER; i < MAX_NUMBER; i++)
         if (function(i,2) == 2) {
             x = i;
             break;
        }
    And x is what you want. So generally search for everything assuming you want only integers on the space [-MAX_NUMBER, MAX_NUMBER]. Dunno if it qualifies on what you want...

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Do you mean:

    Code:
    int product(int x, int y)
    {
        return x * y;
    }
    
    int find_x(int result, int y)
    {
        return result / y;
    }
    
    /* ... */
    
    int r;
    int x;
    
    r = product(5, 2); /* r is 10 */
    x = find_x(r, 2);   /* 10 / 2 is 5 */
    You can simply transpose x * y = r

    x * y = r
    x = r/y

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    30
    C_ntua - Well, I meant that what I WANT to do is something like function(x,2)=2, solve for x - but I know you can't do that. It looks like what you have is what I want to do, but I don't know if it's practical, because in the more complicated function, I want to do it with a lot of function "answers", and the equivalents of x and y aren't integers, so -MAX_NUMBER to MAX_NUMBER would have to go over a crazy amount of small decimals, and do it a lot of times...

    And zacs7 - kind of the same problem. Like I said before, my real function is difficult to solve for x, not anywhere near as simple as dividing both sides by y. :S

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    More information then? IMO this is maths not programming really.

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    30
    Do you mean more information on the function? I don't really think a specific discussion of the function would help, because I definitely understand the math concepts, it's just that it would be complicated to write out and solve and depends on more variables, so I was looking for a way to make the computer do it instead. But I don't even know that it's possible in c, since what I want to do is assign a value to the function.
    I was hoping there would be a way to backsolve the equation for x without actually doing it..., then give it the value for the "answer" (if answer is function(x,y)), and make it give me x.

    I guess what I'm saying is, I WANT it to be programming, not math, but if it has to be math, then I'll go back to it on my own...

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Still not sure what you mean? But you could always use pointers.
    Code:
    int do_something(int * x, int y)
    {
         int prod;
         
         if(x == NULL)
             return 0;
    
         prod = *x * y;
    
         *x = /* I dunno? */
         return /* something */
    }
    
    /* ... */
    int x = 5, y = 10, result;
    
    result = do_something(&x, y);
    /* x will be "I dunno?" */
    Maybe getting closer?

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    30
    Hmm...I'm not sure if that would work or not. I might be confused.

    Let me check and see if you understand what I'm asking (I'll do a better example).
    Let's say I'm doing this:
    I have a function like function(x,y)=5*x^3*y+x^2*y^2+x*y^4+17=answer.
    I pick a certain x and y and compute answer, let's say it's 5.
    Now, for kicks, I want to know what values for x and y would give an answer of 5.1.
    I'll say that y=1, but I want to know x.
    So I have
    y=1;
    5*x^3*y+x^2*y^2+x*y^4+17=5.1;

    Can I make it tell me what x is?

    ...without solving for x myself, as with a function like this, would be difficult.

    Now, I know I can't just set the function(x,y) to a value, but is there some way to get the computer to put the function in terms of x=something? I don't have to know what it is, I just want to know the final value for x if I enter values for y and answer.


    And if that was clear before, could you explain more what you were suggesting with pointers, because I don't completely follow, so I'm not sure if it solves the problem or not.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Can I make it tell me what x is?

    ...without solving for x myself, as with a function like this, would be difficult.

    Now, I know I can't just set the function(x,y) to a value, but is there some way to get the computer to put the function in terms of x=something? I don't have to know what it is, I just want to know the final value for x if I enter values for y and answer.
    You could program a numerical method for estimation.
    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

  10. #10
    Registered User
    Join Date
    May 2008
    Posts
    30
    Sorry, could you elaborate a little more?

    I haven't actually done something like that before, but it seems like those methods apply mainly when you don't know the function, but you have points...I have the function.

    ...Am I off track?
    Last edited by zdream8; 06-30-2008 at 12:39 AM.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Search the Web for "numerical method". Various algorithms and formulae are available online.
    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

  12. #12
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    There are no way to solve every equation dude, because not everything CAN be solved.
    If you tell me:
    x^2 = 4
    I can tell you the answer.
    If you tell me:
    sin(x) +x = 1
    probably i won't be able

    A program can do what a human can do. It will do faster the evaluations but it will use the same method as a human.

    So in order to solve an equation you do two things.
    1) Estimate the result. Use arithmetic methods. That is what programs usually do. There are a lot of arithmetic methods, depends on what you want to do. You get an estimation of the result, a really good one, and you can solve every equation. But this is a different story.
    2) Find the exact result. This can be done the same way a human would do it. Nothing less, nothing more.

    So in order to solve x^2 = 4 you would have to do this:
    Parse the expression. Save the data in specific structs. Read the operators and do accordingly (this is the 3rd time I say this in this week).
    So lets say you want a 3-thing solver. 2 things and an operator that equals to another thing.
    You read the operator. Lets say it is a ^. Then:
    check for values and variables. If both are variables check if they are the same. If they are the same then change 2nd variable and operator accordingly.
    x^x = x^2 --> 2nd variable = 2
    x+x = x*2 --> 2nd variable = 2, operator = *
    If one is variable do the appropriate method.
    If ^ for example do sqrt(result / second_variable). The result is x.
    If it was 2^x you would do log<1st_variable>(result)

    So you get an idea on what you have to do. If you want something complicated I suggest starting from the 2 variable program. Then you will have to make a struct based on 2 variable evaluation (a tree). So you can evaluate every expression.

    There are such programs by the way. You can make your own.
    But it is not done automatically as you would suspect.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I haven't actually done something like that before, but it seems like those methods apply mainly when you don't know the function, but you have points...I have the function.
    No, many of them are only applicable when you have the function.
    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

  14. #14
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Don't think of C functions as mathematical formula. Similar, but not the same.

    You seem to be looking for inverses. "Given f(x,y)=z, z=3, y=2, what is x?" For some generic function f, this is not really possible in C. If f() is a formula, you can approximate an answer in the way laserlight suggested. If you know f(), the best thing is to program another function that calculates the inverse. (Here is where your algebra comes in handy...)

    x^2 = 4
    I can tell you the answer.
    I hope you tell me two answers.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  15. #15
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    No, I won't. Only one

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM