gingikid gingikid - 1 month ago 10
C# Question

Loop to calculate organism growth

I am trying to make a loop that calculates number of organisms over time, but I am stuck on how to make the loop update. Do I need to put something outside the for loop to update the total organisms?

private void calculateButton_Click(object sender, EventArgs e)
{
//declare variables for number of days passed and population
double days;
double organisms;
double increaseDaily;
double total_organisms;

//declare the constants to be used
const int interval = 1;
const int start_days = 1;

//try parse to get amount of starting organisms
if (double.TryParse(organismTextBox.Text, out organisms))
{
//try parse to get the percent daily increase
if (double.TryParse(dailyIncreaseTextBox.Text, out increaseDaily))
{
//try parse to get the number of days passed
if (double.TryParse(daysMultiplyTextBox.Text, out days))
{

//for loop to count through the number of days
for (int i = 1; i <= days; i += interval)
{

//calculate the amount of organisms
total_organisms = (organisms * (increaseDaily / 100) + organisms);

//display the amount of organisms after an amount of time
listBox1.Items.Add("after " + i + " days, the amount of organisms is " + total_organisms);
}

Answer

Each loop, you are calculating the total_organisms as the sum of organisms plus some percent:

total_organisms = (organisms * (increaseDaily / 100) + organisms);

You are never changing the value of organisms, so total_organisms will be calculated as the same value each loop. You should just updated the value of organisms instead.

Also, you could reduce indentation in your code by changing each if statement to test for parse failure and bail out:

private void calculateButton_Click(object sender, EventArgs e)
{
    //declare variables for number of days passed and population 
    double days;
    double organisms;
    double increaseDaily;

    List<string> errors = new List<string>();
    //declare the constants to be used 
    const int interval = 1;
    const int start_days = 1;

    //try parse to get amount of starting organisms
    if (!double.TryParse(organismTextBox.Text, out organisms)) {
        errors.Add("Organisms must be a valid number");
    }
    //try parse to get the percent daily increase
    if (double.TryParse(dailyIncreaseTextBox.Text, out  increaseDaily)) {
        errors.Add("Daily increase must be a valid number");
    }
    //try parse to get the number of days passed
    if (double.TryParse(daysMultiplyTextBox.Text, out days)) {
        errors.Add("Number of days must be a valid number");
    }
    if (errors.Any()) {
        // Display errors to user here (depending on your UI)
        return;
    }
    //for loop to count through the number of days
    for (int i = 1; i <= days; i += interval) {
        //calculate the amount of organisms
        organisms = (organisms * (increaseDaily / 100) + organisms);

        //display the amount of organisms after an amount of time
        listBox1.Items.Add(
            "after " + i + " days, the amount of organisms is " + organisms);
   }
}