# Thread: Loops that iterate/alter code in a binary fashion?

1. ## Loops that iterate/alter code in a binary fashion?

Hi,

say I wanted to turn the following into a loop:

result = floor(a) + floor(a)
result = ceil(a) + floor(a)
result = floor(a) +ceil(a)
result = 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. 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. Originally Posted by tabstop 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. 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 = {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. 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 