![]() |
|
Need large amount of help for C Programming classwork - Printable Version +- Southperry.net (https://www.southperry.net) +-- Forum: Social (https://www.southperry.net/forumdisplay.php?fid=14) +--- Forum: Rubik's Cube (https://www.southperry.net/forumdisplay.php?fid=58) +--- Thread: Need large amount of help for C Programming classwork (/showthread.php?tid=16778) |
Need large amount of help for C Programming classwork - Sivrat - 2009-09-24 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: Quote: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: 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) Need large amount of help for C Programming classwork - Fiel - 2009-09-24 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: Code: Day | Number of BacteriaSo now that we know what we need, let's generate some pseudo-code for it. Code: MAIN:Need large amount of help for C Programming classwork - Spaz - 2009-09-24 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. Code: double *numBacteriaOnDay = malloc(sizeof(double) * numDays); /* an integer type would make more sense, but the assignment says to use a double, so... */Use printf while specifying the amount of padding to print a chart. Need large amount of help for C Programming classwork - Fiel - 2009-09-24 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. ![]() in OutputDecision, I was thinking... Code: if numBactAfter <= 0:Need large amount of help for C Programming classwork - Spaz - 2009-09-24 Fiel Wrote: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.Obviously you'd do a sanity check on the input before calling malloc. Need large amount of help for C Programming classwork - Sivrat - 2009-09-24 ok, well, don't fully know the terms you are using since im pretty noob at programming. this is what ive got Code: int main(void) {it keeps returning just - (numBactDie) and dont know why Need large amount of help for C Programming classwork - Jormungandr - 2009-09-24 [COLOR="Teal"]... Why did I not ask for help on my C++ class hw here?[/COLOR] Need large amount of help for C Programming classwork - Dusk - 2009-09-24 Why are you setting numBact after the print statement? Need large amount of help for C Programming classwork - Sivrat - 2009-09-24 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. Need large amount of help for C Programming classwork - Sivrat - 2009-09-24 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) Need large amount of help for C Programming classwork - Noah - 2009-09-26 Sivrat Wrote: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. That's the basic. Let's expand it to see the pattern: This expands into the following: Which is equal to Now, looking at the last part, a finite geometric series can be rewritten as the following: Thus, you can rewrite the recursive function into this function instead: Now, we can answer your question way easier: Stay steady means that the derivative will equal to 0: Gotten the derivative. The derivative is 0: 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. ![]() 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: Code: int numBact2 = growthRate * numBact - numBactDie;And provide them with the upper information if you feel they're saying that you don't prove it completely. Noah |