C++ Loop Challenge

I received a C++ challenge in the RIT Game Design & Development group on Facebook, so I decided to chart my iterative approach to solving it in hopes that I learn a few things and can share.

Here is the challenge I was given:

Program a counter that goes from 0 to 15, and then at 16 loops back to 0, going on forever. The counter itself is within a function, and it increments every time the function is called.
You are no longer allowed to use:
-Branching (which includes try catch, if statements, switches)
-The & operator
-Modulus (%)
-Overflowing a bitfield
-Overflowing any other data type
-Compare operators (==, < , etc) -Bit shifting to create any of the operators not allowed here (bit shifting in general is fine, but no doing & with bit shifting) -MOV instructions to create any of the operators not allowed here (you can recreate every instruction out of mov's) -Assembly instructions in general to create any of the operators not allowed here. -Looped linked lists. -Hardwiring a circuit explicitly for this purpose. -Overflowing a buffer with zero's to write over the parts of the counter that go above 16. -Using lookup tables -Unrolling the loop -Never-ending streams of interns -Webcam related number-recognition

There’s a lot to check off here, but I decided to start with a basic implementation first and then handle each restriction until all have been met. With that, here’s the basic first step:

“Program a counter that goes from 0 to 15, and then at 16 loops back to 0, going on forever.”

So I put that together and here’s what we have:

#include

int main()
{
int counter = 0;
do
{
++counter;
std::cout < < counter << std::endl; if (counter > 15)
{
counter = 0;
}
} while(counter != 0);//true);

return 0;
}

(see it at http://cpp.sh/2pjsh)
I of course modified my do/while loop so I could verify that it is counting up without it running forever and simply commented out the code that would do so.

Next we have “The counter itself is within a function, and it increments every time the function is called.”. This has two parts: (1) counter is defined within a function and (2) the counter increments every time the functioned is called. With slight adjustments, here’s what I came up with:


#include

int PerformCounter()
{
static int counter = 0;
return ++counter;
}

int main()
{
int counter = 0;
do
{
counter = PerformCounter();
std::cout < < counter << std::endl; if (counter > 15)
{
counter = 0;
}
} while(counter != 0);//true);

return 0;
}


(see it at http://cpp.sh/3thj3)

With this, we have our variable created within the PerformCounter function (there’s no rule about static or not!) and incremented in the function.

Now we start hitting the list of restrictions. The first one we have is “Branching (which includes try catch, if statements, switches)”. This is a bit harder! However, we have C++ on our side. Thinking of ideas around this, I came up with a crazy idea: lambdas!


#include
#include
#include

const int MAX_NUMBER = 16;
std::array, MAX_NUMBER+1> incrementers;

int PerformCounter()
{
static int counter = 0;
return incrementers[counter](counter);
}

int main()
{
// Construct the array of lambdas
incrementers.fill([](int &v)
{
return ++v;
});
incrementers.back() = [](int &v)
{
return v = 0;
};

// Perform the counting
int counter = 0;
do
{
counter = PerformCounter();
std::cout < < counter << std::endl; } while(counter != 0);//true); return 0; }

(see it at http://cpp.sh/3f2b)

My idea here was not only to have each number increment the same except the final, but also to allow the list to be dynamic (why not!?). Looking through the list, this fulfills all of the restrictions! Although, one might argue that this still conflicts with the "Lookup table" restriction. I might revisit this at a later time when I dig into std::enable_if and template metaprogramming a bit more.

What do you think? What would your solution be?

Comments are closed.