Hello , sometime ago I started a small project centred around some crazy mutating breeding rabbits( sounds silly but I have learned an awful lot ,especially about lists). Anyway , as each year passes the number of rabbits grow - not exponentially but very quickly all the same. The next part of the exercise states that I have to modify the code such that 1 year in a rabbits life equals 2 seconds real time. Needless to say I have absolutely no idea how to this! ,especially as there is a large degree or randomness in the number of rabbits that get produced each year. Though it's pretty safe to say that the program can always beat this time i.e. A rabbit year will always pass by faster than 2 seconds real time.

Rabbit year + some calculated time = 2 seconds realtime

Or, as i suspect there is a more elegant way of doing this?
Anybody got any idea where I even start on this next part of the exercise?

Make a function that blocks until year_seconds seconds have passed, where year_seconds is a variable you have flow control of, such that you can increase or decrease it at different phases of the evolution simulator you are creating.

Naturally, the process will typically finish in microseconds, so if you want to slow it down to 2 seconds, you need to actually force the program to wait.

Good luck!

I would actually imagine they are assuming the computations take 0 time. So you just need to make a function wait(2) that pauses for 2 seconds.

very simple:

int last_time_ms = some_time_function();
for(int year=0;year<MAX_YEAR;++year) {
  //perform your task..
  int current_time_ms = some_time_function();
  sleep(2000 - (current_time_ms - last_time_ms));
  last_time_ms = some_time_function();
};

that's it. If you are looking for a good time function, I would suggest Boost Date-Time library, which would turn into:

#include <boost/thread.hpp>
#include <boost/posix_time.hpp>
//...
boost::posix_time::ptime last_time_ms = boost::posix_time::microsecond_clock();
for(int year=0;year<MAX_YEAR;++year) {
  //perform your task..
  boost::posix_time::ptime current_time_ms = boost::posix_time::microsecond_clock();
  boost::this_thread::sleep(boost::posix_time::seconds(2) - (current_time_ms - last_time_ms));
  last_time_ms = boost::posix_time::microsecond_clock();
};
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.