Thread: pass extra data to a function

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    48

    Unhappy pass extra data to a function

    Hi folks,
    I'm working on a project hence in urgent need of guides. The problem might be clarified by using a simple example (if in need, I'll spell out the whole picture, however the following is the barebone):

    suppose I want to minimize a function f(a), the argument for the function is only 'a' from the optimizor's point of view. Hence the optimizor wants to find the 'a' that will minimize f().

    However, f(.) itself is not a simple function of 'a', it is derived out of function
    g(a,b) where b is a constant determined during the runtime (i.e., you cannot declare f(.) by specifying 'b' beforehand). Worse, 'b' might be an array of two dimension (think of importing data into the function g(.) and construct f(.)).

    I tried declaring b as an external, defining g(a,b), and then used g(.) in the definition of f(.), but failed. The linker said the 'b' is undeclared.

    Please anyone helps me out of this. I appreciate it very much...

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    Could you... put some real code to that because it's pretty damn confusing as you state it. And what the hell is the difference between f() and f(.) ?

    Edit: Also, what do you mean by "minimize" ? And what do you mean when you say a function is derived out of another.. ?

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    48

    the code

    I'm sorry; actually f() and f(.) are the same in my last thread.
    Here's the code for illustration:

    Code:
    #include "/*headfile, etc.*/"
    
    double g(double &x, double *b) {
    //b is to be a 2-dimension matrix
    //x is a vector
    ....
    }
    
    double f(double &x) {
    return g(x, &b);
    }
    
    
    int main() {
    
    double x[dim]; //dim to be the dimension of x vector;
    
    x=funmin(init_x, f); /* funmin() is a routine to minimize f() with the argument
                              vector init_x initialized for f()*/
    
    }
    Please let me know if there's any question...

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    48

    extern

    I forgot to add, as a matter of fact, I declare both x and b to be external in the program; this is a bad design but I have no idea how to circumvent it...

  5. #5
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happier about helping you

    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found on the code tag post at the top of every forum. I also suggest you take a look at the board guildlines if you have not done so already.

    This is a common mistake for those new to our forums, just remember to use [code] tags in the future and you'll get much more help.

    If this is your first time posting here the welcome, and if there's anything I can do or any questions I can answer about these forums, or anything else, please feel free and welcome to PM me.


    Good Luck with your program,

    Kermi3
    Lead Moderator
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    48

    thanks

    It's very kind of you for providing the info...

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    48

    linker error

    The linker gives the error message:
    Code:
    undefined reference to 'b'.
    I'm using Bloodshed Dev C++...

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    903
    What do you mean by minimize ? In four years of programming with C++ I've never seen such a term used anywhere in any book nor on any discussion board.

    Edit: You haven't even declared 'b'. I guess the error message is meaningful.

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Can you post a FULL WORKING EXAMPLE program (with comments, I/O and everything) to illustrate your problem? We really have no idea what you are talking about. I'm guessing it's rather trivial...

    By the way, double &x is not a "vector", it's a regular double passed by reference. And where are you supplying the dimensions for the "two-dimensional array" b?
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  10. #10
    Registered User
    Join Date
    Aug 2006
    Posts
    48
    I declare 'b' by using EXTERN keyword, which I omit from my original code:
    Code:
    extern double *b
    I'm thinking of a way to come up with a working example; my original code spans several files, not all of which are context-free...

  11. #11
    Registered User
    Join Date
    Aug 2006
    Posts
    12
    Quote Originally Posted by asmileguo
    I declare 'b' by using EXTERN keyword, which I omit from my original code:
    Code:
    extern double *b
    Quote Originally Posted by asmileguo
    The linker gives the error message:

    Code:
    undefined reference to 'b'.
    I think if you have an "extern double * b;" declaration, you should also have somewhere a definition like "double * b;" (on file level, not inside a function), and also initialize it before calling your functions.

    I hope it helps.

  12. #12
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Do you mean optimize?
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  13. #13
    Registered User
    Join Date
    Aug 2006
    Posts
    48

    yes

    I mean that by taking the minimum of a (mathmatical) function ( I realize it's slightly different from what we label as function in the programming...)

    Quote Originally Posted by Mario F.
    Do you mean optimize?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM