Loops that iterate/alter code in a binary fashion?

This is a discussion on Loops that iterate/alter code in a binary fashion? within the C++ Programming forums, part of the General Programming Boards category; Hi, say I wanted to turn the following into a loop: result[0] = floor(a) + floor(a) result[1] = ceil(a) + ...

  1. #1
    1:1
    1:1 is offline
    Registered User
    Join Date
    Jan 2011
    Posts
    3

    Loops that iterate/alter code in a binary fashion?

    Hi,

    say I wanted to turn the following into a loop:

    result[0] = floor(a) + floor(a)
    result[1] = ceil(a) + floor(a)
    result[2] = floor(a) +ceil(a)
    result[3] = ceil(a) + ceil(a)

    See how if you imagine floor is 0 and ceil is 1 that the functions change in a binary fashion that follows with the resultX number (binary backwards)

    I am working with things that do this often with lots of variations and more complexity, usually 8 but the above example is only 4... Lots of typing!

    Is there a way I can set up a loop that can iterate the code itself in a way that follows the binary pattern of the current loop number ?

    I'm trying to save on typing here so I don't want a false economy of even more loop logic code just to make it work...

    Maybe there is a way ? I just dont know how to search for it - beginner here!

    Kind Regards

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You could set up an array of function pointers (or since we're in the C++ forum, a vector of function objects) and then call functionarray[i/2](a) + functionarray[i%2](a).

  3. #3
    1:1
    1:1 is offline
    Registered User
    Join Date
    Jan 2011
    Posts
    3
    Quote Originally Posted by tabstop View Post
    You could set up an array of function pointers (or since we're in the C++ forum, a vector of function objects) and then call functionarray[i/2](a) + functionarray[i%2](a).
    ah, ok - hmmm, to tell you the truth I'm a complete C++ beginner - we're just finally fed up with running MEL script in Maya and want to use the C++ API ...

    This is one of the first things I thought we'd get right - can you flesh out syntax involved in doing this for a beginner ?

    Help much appreciated !

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I would be very surprised if there is such a thing as doing this for a beginner. But the basic idea using your functions above would be
    Code:
    typedef double *function_pointer(double);
    function_pointer array_of_function_pointers[2] = {floor, ceil};
    for (int i=0; i < 4; ++i) {
        result[i] = array_of_function_pointers[i%2](a) + array_of_function_pointers[i/2](a);
    }

  5. #5
    1:1
    1:1 is offline
    Registered User
    Join Date
    Jan 2011
    Posts
    3
    Awesome,

    cheers mate! will have a crack at it soon

    looks simpler than MEL - so much farting around trying to reinvent the wheel going on there

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. binary code
    By Ken JS in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 08-29-2007, 04:06 PM
  2. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  3. making programms faster
    By deian in forum C Programming
    Replies: 23
    Last Post: 10-09-2004, 12:19 AM
  4. What is binary code?
    By DramaKing in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 12-04-2001, 10:46 AM
  5. Loops OR Functions!! HELP WITH THIS CODE!!!!!!!!!!
    By WIshIwasGooD in forum C++ Programming
    Replies: 4
    Last Post: 10-24-2001, 12:49 PM

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