Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › C# loops population calculation - help.
New Posts  All Forums:Forum Nav:

C# loops population calculation - help.

post #1 of 6
Thread Starter 
hey guys i have this assignment where i have to calculate final population ill just copy and paste it here:

A bacteriologist determines that the approximate final population of bacteria present in a culture after time (in days) is given by the following formula:
FinalPopulation = InitialPopulation * e(GrowthRate * Time)
where InitialPopulation is the number present at the beginning of the observation period (Note: InitialPopulation does not change its value). Let the user input the InitialPopulation, which is the number of bacteria present at the beginning of the trial and the GrowthRate (as a percentage). Then compute the number of bacteria in the culture after each day for the first 10 days (Time will have values 1 through 10). Do this in a loop so the user can see the results in a table (properly format your table with column headers and a title). The output table should have headings for Day and Number of Bacteria Present (on that day).

e=2.71828

and here is what i have so far. ( ive been working on this for almost 2 days)
i tried following this thread "C# Loops sending me for a loop... Could use some help " and this is what i have.

//declare variables
double FinalPopulation, InitialPopulation;
double e = 2.71828;
decimal growthRate;


while (i <= Interval)
{ Console.Write("Enter the Initial Population:");
InitialPopulation = double.Parse(Console.ReadLine());
Console.Write("Enter the Growth Rate: ");
growthRate = decimal.Parse(Console.ReadLine());

// FinalPopulation = Math.Log(InitialPopulation * e) * (Math.Pow(growthRate * DateTime)); }





//keep the console window open
Console.ReadLine();
post #2 of 6
You will have to set the interval somehow, to change i...

“For” loop will be useful here. Try to get acquainted with it.

You don't need to enter initial population for every day you calculate, so maybe putting population in this loop will not be a very good idea.

Also, you can simplify the equation (in a way).

So… here's homework:
  1. Break down your future program into steps. Define the user experience. After this step, you must know exactly what your user is going through start to finish. All the prompts, inputs, outputs.
  2. Define the architecture of your program based on the first step. Hint: it may be a single-run script or a state-driven endless loop, or other things.
  3. get some code together based on the work done.
Workstation mk II
(16 items)
 
ASUS A73S
(16 items)
 
 
CPUMotherboardGraphicsGraphics
Intel Core i7-4790K Gigabyte G1.Sniper 5 GTX 1080 8GB GB GTX 1080 8GB GB 
RAMHard DriveHard DriveCooling
G.Skill Ares 16GB 2133 dual channel kit Crucial 893 GB SSD 3x HGST 3TB SATA3 H110i v2 
OSMonitorKeyboardPower
Windows 10 Acer XB271HK FILCO Majestouch 2 EVGA Supernova 1300 80 gold 
CaseMouseMouse PadAudio
Corsair 900D Cougar 700M Mionix Sargas 400 Asus Xonar u7 
CPUMotherboardGraphicsRAM
Core i7 2670qm ASUSTeK K73SD GeForce 610M 8 GB Samsung 
Hard DriveOptical DriveCoolingOS
750 GB Hitachi HTS54somenumbers Matshita DVD-RAM UJ8B0AW Laptop cooling — snakey heatpipe, some heatsink... Windows 8 pro 
MonitorMonitorKeyboardPower
Some 17" 1600×900 glossy panel, not too shabby. LG Flatron W2343T Most keys are perfect square. A few elongated r... The AC/DC adapter makes a high-pitched sighing ... 
CaseMouseMouse PadAudio
Not slim. Nexus silent mouse Custom handmade Built in, some realtek crap. 
CPUMotherboardGraphicsRAM
Pentium® III-S 1.13 GHz Compaq ProLiant ML330 G2 ATI Rage XL/XC 2 GB Kingston 
Hard DriveHard DriveOptical DriveCooling
A couple 500GB SCSI II disks in raid 1. 120GB SCSI II disk HL-DT-ST DVD-RAM Stock. It’s noisy. 
OSPowerCaseAudio
Windows XP Pro SP 3 Some PSU I pulled out from under my bed. Compaq. Very genuine case. Some Legacy audio. 
OtherOther
Realtek RTL8169/8110 Gigabit ethernet NIC AMI MegaIDE raid controller 
  hide details  
Reply
Workstation mk II
(16 items)
 
ASUS A73S
(16 items)
 
 
CPUMotherboardGraphicsGraphics
Intel Core i7-4790K Gigabyte G1.Sniper 5 GTX 1080 8GB GB GTX 1080 8GB GB 
RAMHard DriveHard DriveCooling
G.Skill Ares 16GB 2133 dual channel kit Crucial 893 GB SSD 3x HGST 3TB SATA3 H110i v2 
OSMonitorKeyboardPower
Windows 10 Acer XB271HK FILCO Majestouch 2 EVGA Supernova 1300 80 gold 
CaseMouseMouse PadAudio
Corsair 900D Cougar 700M Mionix Sargas 400 Asus Xonar u7 
CPUMotherboardGraphicsRAM
Core i7 2670qm ASUSTeK K73SD GeForce 610M 8 GB Samsung 
Hard DriveOptical DriveCoolingOS
750 GB Hitachi HTS54somenumbers Matshita DVD-RAM UJ8B0AW Laptop cooling — snakey heatpipe, some heatsink... Windows 8 pro 
MonitorMonitorKeyboardPower
Some 17" 1600×900 glossy panel, not too shabby. LG Flatron W2343T Most keys are perfect square. A few elongated r... The AC/DC adapter makes a high-pitched sighing ... 
CaseMouseMouse PadAudio
Not slim. Nexus silent mouse Custom handmade Built in, some realtek crap. 
CPUMotherboardGraphicsRAM
Pentium® III-S 1.13 GHz Compaq ProLiant ML330 G2 ATI Rage XL/XC 2 GB Kingston 
Hard DriveHard DriveOptical DriveCooling
A couple 500GB SCSI II disks in raid 1. 120GB SCSI II disk HL-DT-ST DVD-RAM Stock. It’s noisy. 
OSPowerCaseAudio
Windows XP Pro SP 3 Some PSU I pulled out from under my bed. Compaq. Very genuine case. Some Legacy audio. 
OtherOther
Realtek RTL8169/8110 Gigabit ethernet NIC AMI MegaIDE raid controller 
  hide details  
Reply
post #3 of 6
Thread Starter 
I know what to output since i broke down the problems. but the problem is i dont know how to code it.
I want to calcualte the final population within 10 days using loop. and output 2 columns using "the space code" between days and final population like this:

day population
1 (example: 1000000)
2 200000
3 3000000
etc...

thats why i have the writelines asking for initial population and growth rate from the user so they can calculate by themselves .
post #4 of 6
Quote:
Originally Posted by peoplespower View Post

I know what to output since i broke down the problems. but the problem is i dont know how to code it.
I want to calcualte the final population within 10 days using loop. and output 2 columns using "the space code" between days and final population like this:

day population
1 (example: 1000000)
2 200000
3 3000000
etc...

thats why i have the writelines asking for initial population and growth rate from the user so they can calculate by themselves .

That's a good start, but you need to be much more specific. The vagueness is what drives your problem, once you get really-really specific, you will be able to see the logic of your program much better.

For example, here’s a breakdown of a very simple script:

****Guid generator v0.1****
  1. Display message to user: “Welcome to Guid generator.”
  2. Display prompt to user: “How many guids do you want to create? ”
  3. User types their input on the same line after the space following the question mark.
  4. Wait for user input. Receive input as integer. Let this integer be “x”.
  5. Display message: “Here are your fresh guids. Thank you and please come again.”
  6. Display “x” new lines with a new guid on each.
  7. End program.

So now we need four steps:
  1. Display initial message
  2. Receive input
  3. Calculate guids
  4. Display guids

The two last steps are of particular interest:
Two obvious solutions would be either to separate calculation from display or display in the process of calculation. Each has its own merits. If you keep displaying each new guid one by one, your code will be more laconic and the user will keep getting constant feedback. If you collect all guids into an array first, you'll be able to check up on whether you have generated two identical guids. There is a fat chance of that happening: it is likely with modern technology that you will need to exhaust all energy in the known universe in order to produce enough guids for two to be identical, but in case you do produce two identical ones, you would very much want to know, because you’d likely be the first person to do that.

I choose real-time display and break down the last two points of my program into the following logic:
Code:
int NumberOfGuids = int.Parse( Console.ReadLine() );

Console.WriteLine("Here are your fresh guids. Thank you and please come again.");
for (int i = 0; i < NumberOfGuids; i++)
{
    Console.WriteLine(new Guid().ToString());
}

Same result can be achieved with a do{}…while() loop.
Workstation mk II
(16 items)
 
ASUS A73S
(16 items)
 
 
CPUMotherboardGraphicsGraphics
Intel Core i7-4790K Gigabyte G1.Sniper 5 GTX 1080 8GB GB GTX 1080 8GB GB 
RAMHard DriveHard DriveCooling
G.Skill Ares 16GB 2133 dual channel kit Crucial 893 GB SSD 3x HGST 3TB SATA3 H110i v2 
OSMonitorKeyboardPower
Windows 10 Acer XB271HK FILCO Majestouch 2 EVGA Supernova 1300 80 gold 
CaseMouseMouse PadAudio
Corsair 900D Cougar 700M Mionix Sargas 400 Asus Xonar u7 
CPUMotherboardGraphicsRAM
Core i7 2670qm ASUSTeK K73SD GeForce 610M 8 GB Samsung 
Hard DriveOptical DriveCoolingOS
750 GB Hitachi HTS54somenumbers Matshita DVD-RAM UJ8B0AW Laptop cooling — snakey heatpipe, some heatsink... Windows 8 pro 
MonitorMonitorKeyboardPower
Some 17" 1600×900 glossy panel, not too shabby. LG Flatron W2343T Most keys are perfect square. A few elongated r... The AC/DC adapter makes a high-pitched sighing ... 
CaseMouseMouse PadAudio
Not slim. Nexus silent mouse Custom handmade Built in, some realtek crap. 
CPUMotherboardGraphicsRAM
Pentium® III-S 1.13 GHz Compaq ProLiant ML330 G2 ATI Rage XL/XC 2 GB Kingston 
Hard DriveHard DriveOptical DriveCooling
A couple 500GB SCSI II disks in raid 1. 120GB SCSI II disk HL-DT-ST DVD-RAM Stock. It’s noisy. 
OSPowerCaseAudio
Windows XP Pro SP 3 Some PSU I pulled out from under my bed. Compaq. Very genuine case. Some Legacy audio. 
OtherOther
Realtek RTL8169/8110 Gigabit ethernet NIC AMI MegaIDE raid controller 
  hide details  
Reply
Workstation mk II
(16 items)
 
ASUS A73S
(16 items)
 
 
CPUMotherboardGraphicsGraphics
Intel Core i7-4790K Gigabyte G1.Sniper 5 GTX 1080 8GB GB GTX 1080 8GB GB 
RAMHard DriveHard DriveCooling
G.Skill Ares 16GB 2133 dual channel kit Crucial 893 GB SSD 3x HGST 3TB SATA3 H110i v2 
OSMonitorKeyboardPower
Windows 10 Acer XB271HK FILCO Majestouch 2 EVGA Supernova 1300 80 gold 
CaseMouseMouse PadAudio
Corsair 900D Cougar 700M Mionix Sargas 400 Asus Xonar u7 
CPUMotherboardGraphicsRAM
Core i7 2670qm ASUSTeK K73SD GeForce 610M 8 GB Samsung 
Hard DriveOptical DriveCoolingOS
750 GB Hitachi HTS54somenumbers Matshita DVD-RAM UJ8B0AW Laptop cooling — snakey heatpipe, some heatsink... Windows 8 pro 
MonitorMonitorKeyboardPower
Some 17" 1600×900 glossy panel, not too shabby. LG Flatron W2343T Most keys are perfect square. A few elongated r... The AC/DC adapter makes a high-pitched sighing ... 
CaseMouseMouse PadAudio
Not slim. Nexus silent mouse Custom handmade Built in, some realtek crap. 
CPUMotherboardGraphicsRAM
Pentium® III-S 1.13 GHz Compaq ProLiant ML330 G2 ATI Rage XL/XC 2 GB Kingston 
Hard DriveHard DriveOptical DriveCooling
A couple 500GB SCSI II disks in raid 1. 120GB SCSI II disk HL-DT-ST DVD-RAM Stock. It’s noisy. 
OSPowerCaseAudio
Windows XP Pro SP 3 Some PSU I pulled out from under my bed. Compaq. Very genuine case. Some Legacy audio. 
OtherOther
Realtek RTL8169/8110 Gigabit ethernet NIC AMI MegaIDE raid controller 
  hide details  
Reply
post #5 of 6
What you need is a loop.

A loop is a programming structure that lets you do the same things over and over again. The power of loops is that they can count.

For example:
Code:
for (int i = 0; i < 100; i++)
{
    Console.WriteLine(i);
}

This code will output all of the numbers between 0-99 (inclusive).

The key here is:

i is a variable you declare for the loop (by convention it is often named i). You can set i to any number you like. It is just a starting point.

Whatever a loop does is defined in the curly braces. If you add numbers, call a function, do whatever, all of that will happen ever time the code goes through the loop. In this case, the code prints a number to the screen.

'i' gets compared to a number ever time the loop, 'loops'. In my code, i is compared to 100. If 'i' is less than 100, it keeps doing whatever the loop says. As soon as 'i' is not less than 100, it stops doing what is in the loop.

i changes its value every time the loop executes. That is what
Code:
i++
does. It means, add 1 to i. i starts at 0 when the loop starts, and then it increments to 1,2,3 ect every time the instructions in the loop exit.

So those are loops.

Your code is looking well on the right track. You know how to set up the problem, which is good. With a loop, if you can solve the problem once (caculate after one day) you can easily solve it 10 times the same way.

So the definition of your problem is what you said:
Quote:
FinalPopulation = InitialPopulation * e(GrowthRate * Time)

You don't know FinalPopulation. You get intial population and growth rate from the user and save it in a variable. Excellent. So you could do something like this
Code:
FinalPopulation = InitialPopulation * e(GrowthRate * 1)
FinalPopulation = InitialPopulation * e(GrowthRate * 2)
FinalPopulation = InitialPopulation * e(GrowthRate * 3)
FinalPopulation = InitialPopulation * e(GrowthRate * 4)
FinalPopulation = InitialPopulation * e(GrowthRate * 5)
FinalPopulation = InitialPopulation * e(GrowthRate * 6)
FinalPopulation = InitialPopulation * e(GrowthRate * 7)
FinalPopulation = InitialPopulation * e(GrowthRate * 8)
FinalPopulation = InitialPopulation * e(GrowthRate * 9)
FinalPopulation = InitialPopulation * e(GrowthRate * 10)

but what if your instructor asks you to calculate it 100 times? Or 100000 thousand times?

What you want to do is count.

1. Create a loop
2. use a loop variable in your calculation.
Code:
FinalPopulation = InitialPopulation * e(GrowthRate * i)

You are so close thumb.gif
Edited by mothergoose729 - 10/9/15 at 5:37pm
post #6 of 6
Thread Starter 
i actually got it and finished it. but my teacher decided to screw me over. he ddint mark my work becAuaw he doesnt wanna do markings anymore even tho there was 5 mins more before the class ends. anyway doesnt mayter anymore what a waste of $600 course fee.. but i thank you of course for your help really thank!! cheers.
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › C# loops population calculation - help.