// Lab Test 1
// CS 176 - Summer 2001
#include <cstdlib>
#include <iostream>
// The answer to the first design test gave two ways of implementing a model of
// the jackrabbit ecosystem: one based on computing populations each time
// seasons() or population() is calld, and the other based on pre-computing the
// population values and then looking them up. The implementation below uses
// the second choice.
class jackrabbit_ecosystem {
public:
jackrabbit_ecosystem(int start, int max) {
// Initialize a jackrabbit ecosystem to have a starting population of
// start rabbits and enough food to support max rabbits.
// Sanity checks.
if (max < 1) {
cerr << "Maximum value " << max << " is not positive.\n";
exit(EXIT_FAILURE);
}
if ((start < 1) || (start > max)) {
cerr << "Start value " << start << " is not in the range [0.."
<< max << "].\n";
exit(EXIT_FAILURE);
}
// Compute the populations up to the maximum, keeping track in max_season
// the breeding season in which the rabbit population is no longer less
// than the maximum. Does this code work correctly if start == max?
pop[0] = start;
pop[1] = start + 1;
max_season = 1;
while (pop[max_season] < max) {
max_season++;
pop[max_season] = pop[max_season - 2] + pop[max_season - 1];
}
}
int seasons(void) {
// Return the number of seasons until the rabbit population is no less
// than the maximum.
return max_season;
}
int population(int s) {
// Return the rabbit population after breeding season s.
if ((0 <= s) && (s < max_season))
return pop[s];
return 0;
}
private:
// The season at which the rabbit population is no longer less than the
// maximum rabbit population.
int max_season;
// if i is not less than 0 and less than max_season, then pop[i] is the
// size of the rabbit population after breeding season i. 1000 elements is
// big enough for pop because max - start <= 1000; that is, there will
// never be more than 1000 breeding seasons.
int pop[1000];
};
static const int start1 = 7;
static const int max1 = 128;
static const int start2 = 51;
static const int max2 = 1024;
static void q1(void) {
// Answer the first question: which jackrabbit ecosystem runs out of food
// first?
jackrabbit_ecosystem je1(start1, max1), je2(start2, max2);
if (je1.seasons() < je2.seasons())
cout << "first\n";
else
cout << "second\n";
}
static void q2(void) {
// Answer the second question: at which point does the rabbit population in
// the first ecosystem equal or excede the starting rabbit population in the
// second ecosystem?
jackrabbit_ecosystem je(start1, max1);
int i = 0;
while (je.population(i) < start2)
i++;
cout << i << "\n";
}
// That's it. What are the right answers?
//
// breeding ecosystem ecosystem
// season 1 2
//
// 0 7 51
// 1 8 52
// 2 15 103
// 3 23 155
// 4 38 258
// 5 61 413
// 6 91 671
// 7 152 1084
//
// They both run out of food at the same time (breeding season 7) and
// ecosystem 1 exceedes the initial population of ecosystem 2 at breeding
// season 5.
int main() {
q1();
q2();
}
syntax highlighted by Code2HTML, v. 0.9