PDA

View Full Version : Need large amount of help for C Programming classwork

Sivrat
2009-09-24, 06:39 PM
so yeah, I'm pretty nib at programming and am falling behind in class somewhat. The teacher gave us 3 assignments to do that were due 2 days ago(today is last day to turn in). I got the first 2 done within a few hours, but now im more or less stumped on how to go about the next part. The last problems states:

One common task that the biology students must undertake is cultivating specimens of bacteria. Typically, each day, the bacteria multiply at some given rate. (For example, if there are b bacteria at the beginning of the day, there will be 1.1b bacteria at the end of the day.) But, some of these bacteria, a constant number, also die. Thus, the formula for figuring out how many bacteria are alive on day n+1 is based on how much bacteria are alive on day n in the following manner:

f(n+1) = bf(n) – d

where f(n) represents the number of bacteria alive at the beginning of day n, b represents the growth rate (a real number greater than 1), and d represents the number of bacteria that die each day.

The biology department would like you to write a program that takes in the following information:

1) Number of bacteria on day 1
2) Growth rate
3) Number of bacteria that die each day
4) Number of days the bacteria will grow

And outputs a chart to the screen that shows how many bacteria will be alive on each day.

Note: If the number of bacteria alive after replication is less than d, the number that are supposed to die each day, then all remaining bacteria die. (You can’t have a negative number of bacteria.)

Furthermore, in all cases, there is one of three outcomes:

1) The bacteria continue to indefinitely grow (well, in real life the lab will just kill them)
2) A steady state is achieved with the same number of bacteria each day
3) The number of bacteria alive will eventually get down to 0.

You should also output which of these scenarios will happen.

Input Specification
1. The number of bacteria of day 1 will be a positive integer.
2. The growth rate will be a real number greater than 1 and less than 10.
3. The number of bacteria that die each day will be a positive integer.
4. The number of days the bacteria will grow will be a positive integer greater than 2 and less than 100.

Output Specification
Output a chart with the following column headers:
Day Number of Bacteria

Each following line should have the following format:

d X

where d represents the day number of growing the bacteria, and X represents the number of bacteria alive at the beginning of that day. (You’ll need to store the number of bacteria in a double, but print it out as an integer by casting the variable. If necessary, ask your instructor how to do this.)

After the chart, output a blank line and then output one of the following statements:

The bacteria will eventually die out.
The bacteria will live in a steady state forever.
The bacteria will grow without bound.

depending on which of the three scenarios is accurate.

and so far I'm pretty stumped, dont even know how to make charts in C, any help would be appreciated as today is last day i can still get credit for this (wouldn't ask others to do work for me normally)

Fiel
2009-09-24, 07: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:

Day | Number of Bacteria
1 | 32
2 | 64
3 | 128
4 | 256

So now that we know what we need, let's generate some pseudo-code for it.

MAIN:
double numBact
double growthRate
double numBactAfter
int numBactDie
int numDays

Fetch Inputs for (numBact, growthRate, numBactDie, numDays)
numBactAfter = RecursionFunction(numBact, growthRate, numBactDie, numDays, 1), return numBact
OutputDecision(numBact, numBactAfter)

END MAIN

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

Spaz
2009-09-24, 07:37 PM
I'm going to assume you know how to read the input.

While recursion is one possible solution, I think iteration would be simpler.

The algorithm for finding out how many bacteria on each day is simple. If you haven't covered malloc yet, just use a sufficiently large array. I've left out error checking and such.
double *numBacteriaOnDay = malloc(sizeof(double) * numDays); /* an integer type would make more sense, but the assignment says to use a double, so... */

numBacteriaOnDay[0] = numBacteriaOnStartingDay;
for(int i = 1; i < numDays; i++)
{
if(numBacteriaOnDay[i - 1] < d)
{
numBacteriaOnDay[i] = 0;
}
else
{
numBacteriaOnDay[i] = /* I'm sure you can figure out what goes here, I'm lazy */;
}
}

free(numBacteriaOnDay);
The growth function appears to be monotonic. If it grows from day to the next, it will grow infinitely. If it shrinks from one day to the next, it will continue to shrink to 0. Use that fact to determine if it grow forever, shrinks to 0, or stays the same.

Use printf while specifying the amount of padding to print a chart.

Fiel
2009-09-24, 07:42 PM
Don't use malloc. That can easily lead to pseudo-dynamic allocation (using the user input to create the size of the array without using malloc. I remember if I did that on any of my C assignments I received a 0 without the ability to contest it). Of course, the choice is up to you guys. :chin:

in OutputDecision, I was thinking...

if numBactAfter <= 0:
"Will die out"
else if 0 < numBactAfter < numBact
else
"will grow without bounds"

Spaz
2009-09-24, 08:08 PM
Don't use malloc. That can easily lead to pseudo-dynamic allocation (using the user input to create the size of the array without using malloc. I remember if I did that on any of my C assignments I received a 0 without the ability to contest it). Of course, the choice is up to you guys. :chin:
Obviously you'd do a sanity check on the input before calling malloc.

Sivrat
2009-09-24, 08:16 PM
ok, well, don't fully know the terms you are using since im pretty noob at programming.

this is what ive got

int main(void) {

double numBact;
double growthRate;
double numBactAfter;
int numBactDie;
int numDays;
int stop;
int dayPrint = 0;

printf("How many bacteria are there on day one?\n");
scanf("%f", &numBact);
printf("What is their growth rate?\n");
scanf("%f", &growthRate);
printf("How many bacteria die each day?\n");
scanf("%d", &numBactDie);
printf("How many days will you grow the bacteria?\n");
scanf("%d", &numDays);
printf("Day | Number of Bacteria\n");

while (dayPrint < numDays){
dayPrint = dayPrint + 1;
printf("%d | %f\n", dayPrint, numBact);
numBact = growthRate * numBact - numBactDie;
}

return (0);
}

it keeps returning just - (numBactDie) and dont know why

Jormungandr
2009-09-24, 08:18 PM
...

Why did I not ask for help on my C++ class hw here?

Dusk
2009-09-24, 08:22 PM
Why are you setting numBact after the print statement?

Sivrat
2009-09-24, 08:53 PM
ok, got the chart working right, now just to do the part of the 3 options (will die, stay steady, grow without bounds). How do i do that? Thinking that fiel's psuedocode before was made to determine if last output was lower then first, or if its same or if its higher, but i can't think of how to do that.

Sivrat
2009-09-24, 09:04 PM
actually just got it, ty everyone for your help, did it with 2 hours before deadline (well, deadline for 25% off since its late anyway)

Noah
2009-09-26, 05:21 AM
ok, got the chart working right, now just to do the part of the 3 options (will die, stay steady, grow without bounds). How do i do that? Thinking that fiel's psuedocode before was made to determine if last output was lower then first, or if its same or if its higher, but i can't think of how to do that.

You got to prove why your answers, or at least I would do so.

http://latex.codecogs.com/gif.latex?f(n)%20=%20\left\{\begin{matrix}%20c_1%2 0&,%20n%20=%201\\%20b%20\times%20f(n%20-%201)%20-%20d%20&,%20n%20%3E%201%20\end{matrix}\right.

That's the basic. Let's expand it to see the pattern:

http://latex.codecogs.com/gif.latex?\\%20f(n)%20=%20bf(n%20-%201)%20-%20d\\%20f(n)%20=%20b(bf(n%20-%202)%20-%20d)%20-%20d\\%20f(n)%20=%20b^2f(n%20-%202)%20-%20bd%20-%20d\\%20f(n)%20=%20b^2(bf(n%20-%203)%20-%20d)%20-%20bd%20-%20d\\%20f(n)%20=%20b^3f(n%20-%203)%20-%20b^2d%20-%20bd%20-%20d\\%20f(n)%20=%20b^3f(n%20-%203)%20-%20\sum_{k%20=%200}^{2}db^k

This expands into the following:
http://latex.codecogs.com/gif.latex?f(n)%20=%20b^{n-1}f(n%20-%20n%20+%201)%20-%20d\sum_{k%20=%200}^{n%20-%202}b^k

Which is equal to

http://latex.codecogs.com/gif.latex?f(n)%20=%20b^{n-1}c_1%20-%20d\sum_{k%20=%200}^{n%20-%202}b^k

Now, looking at the last part, a finite geometric series can be rewritten as the following:

http://latex.codecogs.com/gif.latex?d\sum_{k=%200}^{n%20-%202}b^k%20=%20\frac{d(b^{n-1}%20-%201)}{b-%201}

Thus, you can rewrite the recursive function into this function instead:

http://latex.codecogs.com/gif.latex?f(n)%20=%20c_1b^{n-1}%20-%20\frac{d(b^{n-1}%20-%201)}{b%20-%201}

Stay steady means that the derivative will equal to 0:

http://latex.codecogs.com/gif.latex?\\f(n)%20=%20c_1b^{n-1}%20+%20\frac%20d{b-1}%20-%20\frac%20{db^{n-1}}{b-1}\\\\%20{f(n)}%27%20=%20\frac{\mathrm{d}%20{(c_1b ^{n-1}})}{\mathrm{d}%20n}%20+%20\frac{\mathrm{d}%20{(\ frac%20d{b-1})}}{\mathrm{d}%20n}%20-%20\frac{\mathrm{d}%20\frac{db^{n-1}}{b-1}}{\mathrm{d}%20n}\\\\%20{f(n)}%27%20=%20\frac{\m athrm{d}%20{(c_1b^{n-1}})}{\mathrm{d}%20n}%20-%20\frac{\mathrm{d}%20\frac{db^{n-1}}{b-1}}{\mathrm{d}%20n}\\\\%20{f(n)}%27%20=%20c_1b^{n-1}\ln%20b%20-%20\frac{db^{n-1}\ln%20b}{b-%201}

Gotten the derivative. The derivative is 0:

http://latex.codecogs.com/gif.latex?\\%20{f(n)}%27%20=%20c_1b^{n-1}\ln%20b%20-%20\frac{db^{n-1}\ln%20b}{b-%201}%20\\\\%20{f(n)}%27%20=%200%20\\\\%200%20=%20 c_1b^{n-1}\ln%20b%20-%20\frac{db^{n-1}\ln%20b}{b-%201}%20\\\\%20c_1b^{n-1}\ln%20b%20=%20\frac{db^{n-1}\ln%20b}{b-%201}%20\\\\%20\textup{Multiply%20by%20}b^{n-1}\ln{b}\textup{:}\\\\%20c_1%20=%20\frac{d}{b-%201}%20\\\\

So as long the equation above holds, then for all n, f(n)' equals 0. Therefore, the amount of bacterias will live in a steady state for all n: The amount of bacterias will always be the same (c1).

That also means that if the amount of bacterias increase, the derivative is positive. But since we've already found out that the derivative is 0 only when the equation above holds, then the derivative will always be positive, and the amount of bacterias will increase forever.

Same yields if the population decreases: The derivative is negative. But the derivative can't be zero, and the derivative has to be continous. Therefore, the derivative will always be negative, and if the amount of bacterias decrease, the derivative is zero, and the bacterias will after a given amount of time (f(n) = 0 solves that question) die out.

Sorry to interrupt a programming-code-discussion like that, but it was an interesting question, so I analyzed it. :ohno:

Edit: There's no reason for anyone to make an array for this. That would be overkill and unneeded. Just use numBact = growthRate * numBact - numBactDie; as you mentioned and make a check on the end like this:

int numBact2 = growthRate * numBact - numBactDie;
if (numBact2 < numBact)
printf("The bacterias will eventually die out");
else if (numBact2 > numBact)
printf("The bacterias will grow without bound.");
else
printf("The bacterias will live in a steady state forever.");

And provide them with the upper information if you feel they're saying that you don't prove it completely.

Noah