C#



Hi, New here, and would be very thankful for any help someone can give me.
The code I am working for is a population estimator. It has 3 text boxes, the first is two enter the starting population, the second is to enter the daily increase in that population in a percentage, and the third is the number of days to multiply that by.
My first issue is with the second text box. I don't know how to enter the data as a percent. As of now it is only entering as a whole number, so I when I right in text box one the number 2, in box2 the number 30, and in three the number 10, I get the 302. Not 21.209 like it should be.
Secondly, it all displays in a listbox, and its supposed to loop for every day you enter. I've got the loop to loop by days, but it just runs the same calculation, I want it make every entry build off the last.
Example:
It should say
2
2.6
3.38
4.394
5.7122
etc
I've entered in the code below.
Thanks for any help!
private void calculateButton_Click(object sender, EventArgs e)
{
int organisms;
int days;
int growth;
int population;
int count = 1;
if (int.TryParse(OrganismsTextbox.Text, out organisms))
{
if (int.TryParse(DailyTextbox.Text, out growth ))
{
if (int.TryParse(DaysTextbox.Text, out days))
do
{
population = organisms + growth * days;
resultsListbox.Items.Add("The population For day " + count + " is " + population.ToString("n1"));
count = count + 1;
}
while (count <= days);





You probably need to use double values rather than integers for your calculations, for greater accuracy. You also need to convert the number taken from the second textbox to a percentage; it's not 30 it is .30. You also need to change your calculation to calculate the growth in compound terms  I'm not sure of the actual formula.
One of these days I'm going to think of a really clever signature.





There are a couple of simple things to do  firstly as Richard said, you need to use floating point values instead of integers:
2 + 30% is 2.6, but as an integer it is 2 again!
The second is that you are looping through the days, so you don't need to include the number of days in each calculation  just use the result from the previous day.
The third is that when you want an increased value, you can't just multiply by the percentage  2 * 30% is 0.6, so your increase is 0.6, not your population. To increase your population, multiply but 1.0 + the percentage.
Finally, there is a better way to loop through the days: use a for loop instead of a while:
for (int day = 1; day <= days; day++)
{
... calculate here
}
So, my version would be:
resultsListbox.Items.Add(string.Format("The population for day {0} is {1}", day, organisms));
organisms *= 1.0 + percent;
with percent adjusted to 0.3 as Richard suggested.
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.





Thanks guys! Got it to work!





Glad to hear it!
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.







General News Suggestion Question Bug Answer Joke Rant Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.