generalized recursive for loop

This is a discussion on generalized recursive for loop within the C Programming forums, part of the General Programming Boards category; Hello, I'm trying to create a generalized recursive for loop function. I need some way to convert a string into ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    26

    generalized recursive for loop

    Hello,

    I'm trying to create a generalized recursive for loop function. I need some way to convert a string into runnable code (preferably through a macro). Here is what I have so far:

    Code:
    #include<stdio.h>
    
    void recursive_for(int,int,char*);
    
    int main(int argc, char* argv)
    {
            int start=0;
            int end=9;
            char* codeString = "printf(\"Hello World %d!\n\",start);";
            recursive_for(start,end,codeString);
            return 0;
    }
    
    void recursive_for(int start, int end, char* codeString)
    {
            int midpt = start + (end-start)/2;
            if(start == end)
                   to_code(codeString);
            else
            {
                    recursive_for(start,midpt,codeString);
                    recursive_for(midpt+1,end,codeString);
            }
    }
    I need either a function or a macro for to_code that will make the string, codeString, into runnable code. This would give me the following output:

    Code:
    Hello World 0!
    Hello World 1!
    Hello World 2!
    Hello World 3!
    Hello World 4!
    Hello World 5!
    Hello World 6!
    Hello World 7!
    Hello World 8!
    Hello World 9!
    Is this possible? The closest I have found is a macro that will convert code to a string, but I need the exact opposite.

    Thanks in advance

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,701
    You can use function pointers for something similar. Knowing "why", or what you want to accomplish in the end would help us steer you in the correct direction.
    Code:
    #include<stdio.h>
    
    void recursive_for(int, int, void(*)(int));
    void my_code(int n) {printf("Hello World &#37;d\n", n);}
    
    int main(void)
    {
        recursive_for(0, 9, &my_code);
        return 0;
    }
    
    void recursive_for(int start, int end, void(*f)(int))
    {
        int midpt = start + (end - start) / 2;
        if (start == end)
            f(start);
        else
        {
            recursive_for(start, midpt, f);
            recursive_for(midpt+1, end, f);
        }
    }
    gg

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    26
    Thanks for the response, although I'm not sure that will work for my purposes.

    My purpose for doing this is i'm writing some multi-threaded code using Cilk. I plan on each function call here spawning a new process so the inner code can be done in parallel. I'm just don't want to write an individual recursive for loop for every single time I want to parallelize it.

    my cilk code would look something like this:
    Code:
    #include<stdio.h>
    #include<cilk.h>
    
    cilk void recursive_for(int,int,char*);
    
    cilk int main(int argc, char* argv)
    {
            int start=0;
            int end=9;
            char* codeString = "printf(\"Hello World &#37;d!\n\",start);";
            spawn recursive_for(start,end,codeString);
            sync;
            return 0;
    }
    
    cilk void recursive_for(int start, int end, char* codeString)
    {
            int midpt = start + (end-start)/2;
            if(start == end)
                   to_code(codeString);
            else
            {
                    spawn recursive_for(start,midpt,codeString);
                    spawn recursive_for(midpt+1,end,codeString);
                    sync;
            }
    }
    Since I am going to be re-using this function many times throughout my program, I don't want to create a separate function with the internal code for each call, but rather just send the code to it in string format. I'm open to suggestions.

    EDIT: Also note that the code inside the for loop could be anything from one line to several lines long. There will probably also be nested for loops i will want to parallize.
    Last edited by moddinati; 12-18-2008 at 04:51 PM. Reason: add information

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,701
    >> I don't want to create a separate function with the internal code for each call, but rather just send the code to it in string form
    So you would rather put the code in quotes instead of a function block? Well - one way works and the other one doesn't

    You would have to make recursive_for a macro itself to "pass" code to it (not in a string). But it's recursive so it can't be a macro.

    >> The closest I have found is a macro that will convert code to a string
    And there's no "de-string" macro operator.

    Function pointers are how you would make a generic "recursive_for" in C.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. recursive function
    By technosavvy in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 05:42 AM
  2. difference between recursive and iterative
    By Micko in forum C Programming
    Replies: 33
    Last Post: 07-06-2004, 10:34 PM
  3. Algorithm help (Changing from Recursive to Non Recursive)
    By Thantos in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2004, 08:27 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 09:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21