2009-09-24, 08:00 PM
Looks like a recursion problem.
EDIT:
Okay, the first thing you need to do is glean out all of the inputs here that you'll need from the user and their types
1) Number of bacteria on day 1 - Double (Although you can't have half of a bacteria, you will need exact measurements with multiplication)
2) Growth rate - Double (the problem itself tells you the type)
3) Number of bacteria that die each day - Integer
4) Number of days the bacteria will grow - Integer
The clue for this problem that it's a recursion is that it neatly sets up one formula which requires the results from calculations of previous formulas (the test case), and a series of possible outcomes that ends the loop (base cases). You could do this as a while or for loop, but using recursion makes this problem much more elegant.
When the problem says "Create a chart", they just mean a list of values with two columns, like this:
So now that we know what we need, let's generate some pseudo-code for it.
EDIT:
Okay, the first thing you need to do is glean out all of the inputs here that you'll need from the user and their types
1) Number of bacteria on day 1 - Double (Although you can't have half of a bacteria, you will need exact measurements with multiplication)
2) Growth rate - Double (the problem itself tells you the type)
3) Number of bacteria that die each day - Integer
4) Number of days the bacteria will grow - Integer
The clue for this problem that it's a recursion is that it neatly sets up one formula which requires the results from calculations of previous formulas (the test case), and a series of possible outcomes that ends the loop (base cases). You could do this as a while or for loop, but using recursion makes this problem much more elegant.
When the problem says "Create a chart", they just mean a list of values with two columns, like this:
Code:
Day | Number of Bacteria
1 | 32
2 | 64
3 | 128
4 | 256So now that we know what we need, let's generate some pseudo-code for it.
Code:
MAIN:
double numBact
double growthRate
double numBactAfter
int numBactDie
int numDays
Fetch Inputs for (numBact, growthRate, numBactDie, numDays)
OutputHeader()
numBactAfter = RecursionFunction(numBact, growthRate, numBactDie, numDays, 1), return numBact
OutputDecision(numBact, numBactAfter)
END MAIN
void OutputHeader()
print "Day | Number of Bacteria"
double RecursionFunction(numBact, growthRate, numBactDie, numDays, int curDay)
IF CURDAY <= NUMDAYS //This is called your "test case"
//Output for the current day
OutputRow(curDay, (int) numBact) //Typecasting, make sure you know how to do it
numBact = (growthRate * numBact) – numBactDie;
//numBact now holds how many we have for day two.
//time for recursion!
return RecursionFunction(numBact, growthRate, numBactDie, numDays, curDay + 1)
ELSE //Return case
return numBact
void OutputRow(curDay, int numBact) //Now an int because of typecasting!
print "%d | %d", curDay, numBact
void OutputDecision(numBact, numBactAfter)
print "\n" //This is the newline they wanted
//I'll leave you to figure out the rest