For my class I am asked to complete a program: create an application that will predict the approximate sie of a population of organisms. the user should select or enter the starting number of organisms in a combo box, enter the average daily population increase (as a percentage) in a text box, and select or enter the number of days the organisms will be lef tot multiply in another combo box.

My issues are: In the list box my final results are lining up in two columns and the math wont turn out correctly. I also cant get the days(which is one of the columns) to go from 1 to the number of days the user selects. so my question is what went wrong with my code?

Public Class Form1

    Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
        Dim Intcount As Integer
        Dim intMultipied As Integer
        intMultipied = 0
        Dim intFinalCalc As Integer
        Dim intadded As Integer
        Dim daysmultiplied As String

        intadded = 1
        daysmultiplied = CBool(cboDaysToMultiply.SelectedItem)

        Intcount = 1
        For Intcount = 1 To cboDaysToMultiply.SelectedItem
            intMultipied = cboNumOfOrgan.SelectedItem * Intcount
            intadded = intMultipied * txtAveDailyInc.Text
            Intcount += 1

            intFinalCalc = intadded * cboDaysToMultiply.SelectedItem

            Dim strOut As String = String.Empty
            strOut &= "Day " & cboDaysToMultiply.SelectedItem
            lstFinalData.Items.Add(strOut)
            strOut &= "Approximate Population " & intFinalCalc.ToString

            lstFinalData.Items.Add(strOut)
        Next
    End Sub
End Class

Recommended Answers

All 2 Replies

You are over complicating the problem. You need three things as input

  1. the number of organisms in the original population
  2. the number of days to calculate growth
  3. the percentage growth per day

The user will likely enter the growth rate as a number from 1 to 100. You can convert that to a daily multiplier by dividing that by 100 and adding 1. For example, 20% becomes 1 + 20/100 or 1.2. Each time through the loop you multiply the current population by this number to get the next population size. So the only two statements you need in the loop are

population *= growthRate

and a line to add that result to the listBox.

Now I'm going to offer some unasked for advice. It is no longer encouraged to prefix variables with their type, as in intCount. It is also useless to name a variable with a name that does not indicate its function. Integers are, by definition, counting numbers so intCount is both redundant and uninformative. It is more important to name the variable it indicate its function. Generally, the type can be inferred from the function. NumDays, for example, will likely never be anything but an integer.

My suggestion would be to copy your inputs into local variables such as

population
growthRate
numDays

Of course, with appropriate error checking before converting from string to numeric. Then your loop looks like

For day As Integer = 1 To numDays
    population *= growthRate
    lstFinalData.Items.Add("end of day " & day & " population is " & population)
commented: Great explanation. +15

Cool thanks for your help. Now I just have to mess with my code a bit until I find out why my population is always zero.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.