Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C++ use function's output as functions input n times

I’m sorry for the weird title. I don’t know how to word this.
If I have function func()
How do I do this:

func(func(func(func(func(x)))))

where it repeats N times?

I’m trying to implement Conway’s Game of Life. I have a function that takes a vector and outputs another vector, which is the next generation of the input vector. So generation 3’s vector would be func(func(func(x))).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The easy way, simply using for loop:

int x = some_initial_value;
for (int i = 0; i < NUMBER_OF_ITERATIONS; ++i)
{
    x = func(x);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading