Struggling to code in VB a simple population model - vb.net

I am trying to develop a population model in Visual Basics 2010. However, I cant do the math nor the code!! Suppose over 5 generations population growth of a fly look like this
GENERATION JUVENILES ADULTS SENILES TOTAL
0 10 10 10 30
1 20 10 10 40
2 20 20 10 50
3 40 20 20 80
4 40 40 20 100
5 80 40 40 160
In the model, Juvelines have a survival rate of 1 i.e. all juveniles survive into Adulthood. Adults have a survival rate of 1 also i.e. all adults survive into Seniles. However, Seniles have a survival rate of 0 i.e. the current population all die. Once completed, I will be able to vary the survival rates for each type of insect, but for now 1,1,0 works fine
The second most important assumption here is that the birth rate of juveniles is double the current number of adults:
New number of juveniles = Current number of adults * birth rate
My question is how do a represent this model as visual basic code?
This is all I have so far
'Assign Values of Textbox to Public Variables
BirthRate = txtBirthRate.Text
GenerationNum = txtGenerations.Text
PopulationJuveniles = txtPJ.Text
PopulationAdults = txtPA.Text
PopulationSeniles = txtPS.Text
SurvivalJuveniles = txtSJ.Text
SurvivalAdults = txtSA.Text
SurvivalSeniles = txtSS.Text
For Counter As Integer = 1 To GenerationNum
'Calculate population of adults
NewJuveniles = PopulationAdults * BirthRate
NewAdults = PopulationJuveniles * SurvivalJuveniles 'the juveniles have matured
NewSeniles = PopulationAdults * SurvivalAdults 'the adults have aged
Next

The following method calculates the population for each generation. Note that it includes code to validate the numbers entered in the TextBoxes (otherwise a bad number would cause an exception). It also displays the numbers for each generation in a ListBox.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Birthrate As Double, GenerationNum As Integer
Dim PopulationJuveniles, PopulationAdults, PopulationSeniles As Double
Dim SurvivalJuveniles, SurvivalAdults, SurvivalSeniles As Double
Dim NewJuveniles, NewAdults, NewSeniles As Double
If Not Double.TryParse(txtBirthRate.Text, Birthrate) _
OrElse Integer.TryParse(txtGenerations.Text, GenerationNum) Then
MessageBox.Show("Enter valid numbers for birthrate and generations")
Exit Sub
End If
If Not Double.TryParse(txtPJ.Text, PopulationJuveniles) _
OrElse Double.TryParse(txtPA.Text, PopulationAdults) _
OrElse Double.TryParse(txtPS.Text, PopulationSeniles) Then
MessageBox.Show("Enter valid numbers for populations")
Exit Sub
End If
If Not Double.TryParse(txtSJ.Text, SurvivalJuveniles) _
OrElse Double.TryParse(txtSA.Text, SurvivalAdults) _
OrElse Double.TryParse(txtSS.Text, SurvivalSeniles) Then
MessageBox.Show("Enter valid numbers for survival")
Exit Sub
End If
ListBox1.Items.Clear()
For Counter As Integer = 1 To GenerationNum
NewJuveniles = PopulationAdults * Birthrate
NewAdults = PopulationJuveniles * SurvivalJuveniles 'the juveniles have matured
NewSeniles = PopulationAdults * SurvivalAdults 'Add + PopulationSeniles * SurvivalSeniles if SurvivalSeniles can ever be >0
PopulationSeniles = NewSeniles
PopulationAdults = NewAdults
PopulationJuveniles = NewJuveniles
ListBox1.Items.Add(PopulationJuveniles.ToString & ", " & PopulationAdults.ToString & ", " & PopulationSeniles.ToString)
Next
End Sub

Related

I HAVE BEEN OVER THIS GOD KNOWS HOW MANY TIMES, WHAT IS WRONG WITH IT? (TICKET MACHINE) NO ERRORS ON VS), i think the top few line may not work

I have been looking over this for the last couple of days and personally I am not to keen on VB, the tab is loading up but I am not getting an answer?
Private Sub Button1_click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Age As Double
Dim seatgrading As Double
Dim Cardaddition As Single
Dim Memberdiscount As Single
Dim installments As Double
Dim totalcost As Double
Dim eachpayment As Integer
Dim total As Single
Dim price As Single
Dim Subtotal As Single
Age = CBOAge.Text
If RBFootball.Checked = True And Age = rbChild.Checked Then
price = 275
ElseIf RBFootball.Checked = True And Age = rbAdult.Checked Then
price = 450
ElseIf RBFootball.Checked = True And Age = rbOAP.Checked Then
price = 295
End If
If RBRugby.Checked = True And Age = rbChild.Checked Then
price = 85
ElseIf RBRugby.Checked = True And Age = rbAdult.Checked Then
price = 175
ElseIf RBRugby.Checked = True And Age = rbOAP.Checked Then
price = 105
End If
' Seat Grades
If RBG1.Checked = True Then
seatgrading = 150
ElseIf RBG2.Checked = True Then
seatgrading = 120
ElseIf RBG3.Checked = True Then
seatgrading = 87.5
End If
total = price + seatgrading
MemberDiscount = installments
If RBBronze.Checked = True Then
MemberDiscount = total * 0.08
ElseIf RBSilver.Checked = True Then
MemberDiscount = total * 0.09
ElseIf RBGold.Checked = True Then
`Cardaddition` = total * 0.025
End If
If RBCard.Checked = True Then
Cardaddition = Subtotal = 0.025
End If
If installments = True Then
installments = total * 0.0375
total = totalcost + installments
eachpayment = totalcost / 12
For count = 1 To 12
installments = installments & "payment" & "is" & Format(eachpayment, "currency") & vbCrLf
Next
End If
total = Subtotal - MemberDiscount + Cardaddition
total = Format(totalcost, "currency")
End Sub
This answer is perhaps more of a way to show you how to get to the code you need than a complete answer.
When dealing with money, it is best to use the Decimal type instead of Single or Double, otherwise the pennies can go wrong after a calculation or two.
You can have an If statement inside another If statement, which can sometimes make for less typing and/or easier reading.
I can't see a need for the age variable because there are already radiobuttons for child/adult/OAP.
The way of calculating the final total price was not clear to me, so I moved some things around into what may or may not be the correct order.
Perhaps the installments variable is meant to be a checkbox for if the payments are going to be taken monthly - I assumed that is the case.
I couldn't see where the results are being presented to the user, so I used MessageBox.Show - I'm sure you will be able to adapt it to how you want to show the total etc.
Private Sub Button1_click(sender As Object, e As EventArgs) Handles Button1.Click
' What I think the variable types are:
' Dim rbChild, rbAdult, rbOAP As New RadioButton
' Dim rbFootball, rbRugby As New RadioButton
' Dim rbG1, rbG2, rbG3 As New RadioButton
' Dim rbBronze, rbSilver, rbGold As New RadioButton
' Dim cbCard As New CheckBox
' Dim cbInstallments As New CheckBox
Dim price As Decimal
If rbFootball.Checked Then
If rbChild.Checked Then
price = 275
ElseIf rbAdult.Checked Then
price = 450
ElseIf rbOAP.Checked Then
price = 295
End If
End If
If rbRugby.Checked Then
If rbChild.Checked Then
price = 85
ElseIf rbAdult.Checked Then
price = 175
ElseIf rbOAP.Checked Then
price = 105
End If
End If
Dim seatgrading As Decimal
If rbG1.Checked Then
seatgrading = 150
ElseIf rbG2.Checked Then
seatgrading = 120
ElseIf rbG3.Checked Then
seatgrading = 87.5D
End If
Dim subtotal As Decimal = price + seatgrading
Dim memberDiscount As Decimal
If rbBronze.Checked Then
memberDiscount = subtotal * 0.08D
ElseIf rbSilver.Checked Then
memberDiscount = subtotal * 0.09D
ElseIf rbGold.Checked Then
memberDiscount = subtotal * 0.025D
End If
Dim cardSurcharge As Decimal
If cbCard.Checked Then
cardSurcharge = subtotal * 0.025D
End If
Dim total As Decimal = subtotal - memberDiscount + cardSurcharge
If cbInstallments.Checked Then
Dim installmentSurcharge = total * 0.0375D
total = total + installmentSurcharge
Dim eachpayment As Decimal = total / 12
MessageBox.Show("Monthly payment is " & eachpayment.ToString("C"))
End If
MessageBox.Show("Total payment is " & total.ToString("C"))
End Sub

Does this code not work because it will take a long time or is it broken?

I am currently trying to generate random numbers until a user defined amount of identical numbers in a row appears. The numbers range from 1 to 10.
When I want 8 identical numbers in a row it takes between 3 and 5 seconds. It goes through about 5 million random numbers. When I want 9 in a row I have left it for over 45 minutes without any luck.
I thought it would only take about 10 times the amount of time as it did for 8 since 1/10 to the power of 9 is only ten times bigger than to the power of 8.
Is it a problem with my code or have I messed up the maths?
MY CODE:
Sub Main()
Console.WriteLine("how many identical numbers in a row do you want")
Dim many As Integer = Console.ReadLine
Dim storage(many - 1) As Integer
Dim complete As Boolean = False
Dim part As Integer = 0
Dim count As Integer = 0
Randomize()
While complete = False
count += 1
storage(part) = Int(Rnd() * 10) + 1
' Console.WriteLine(storage(part))
part += 1
If part = many Then
part = 0
End If
If storage.Min = storage.Max Then
complete = True
End If
End While
Console.WriteLine("===========")
Console.WriteLine(count.ToString("N"))
Console.WriteLine("===========")
For i = 0 To many - 1
Console.WriteLine(storage(i))
Next
Console.ReadLine()
End Sub
There is more than one possibility: it could be that the VB Rnd function is written so that it can never work, or it could be that it really does simply take a long time sometimes. You would have to do many trials and average them out to find out if your expected ratio of 1:10 works as expected.
Incidentally, you don't need to keep the last ten numbers. You can just keep a count of how many times the next random number equals the previous number. Also I suggest trying a different RNG (random number generator), for example the .NET one:
Module Module1
Dim rand As New Random()
Sub Main()
Console.Write("How many identical numbers in a row do you want: ")
Dim howMany As Integer = CInt(Console.ReadLine)
Dim count As Long = 0
Dim previous = -1
Dim soFar = 0
Dim n = 0
While soFar < howMany
count += 1
n = rand.Next(1, 11)
'Console.Write(n.ToString() & " ")
If n = previous Then
soFar += 1
Else
'If previous >= 0 Then
' Console.WriteLine(" X")
' Console.Write(n.ToString() & " ")
'End If
previous = n
soFar = 1
End If
End While
Console.WriteLine()
Console.WriteLine("Tries taken: " & count.ToString())
Console.ReadLine()
End Sub
End Module

Lottery Program - Visual Basic

Have to create a lottery program, getting the random numbers and such easily enough. However I'm stumped. Essentially I have 2 buttons. One to display a checked list box with numbers 1-100, along with the 5 lotto numbers. I have a 2nd button that checks 2 things, to make sure that more than 5 numbers are not checked matching numbers. I'm lost on how to check for a match between the selected numbers between the RNG numbers.
Public Class Form1
Private Sub displayBtn_Click(sender As Object, e As EventArgs) Handles displayBtn.Click
Dim lottoNumbers(5) As Integer
Dim counter As Integer = 0
Dim number As Integer
Dim randomGenerator As New Random(Now.Millisecond)
'This will randomly select 5 unique numbers'
Do While counter < 5
number = randomGenerator.Next(0, 98)
If Array.IndexOf(lottoNumbers, number) = -1 Then
lottoNumbers(counter) = number
counter += 1
End If
Loop
'Display the lottery numbers in the label.'
Label1.Text = String.Empty
Array.Sort(lottoNumbers)
For Each num As Integer In lottoNumbers
Label2.Text = "Lottery Numbers"
Label1.Text &= CStr(num) & " "
Next num
For x = 0 To 98
CheckedListBox1.Items.Add(x + 1)
Next
End Sub
Private Sub checkBtn_Click(sender As Object, e As EventArgs) Handles checkBtn.Click
Dim count As Integer = 0
Dim x As Integer
'Checks to see if user checked more than 5'
For x = 0 To CheckedListBox1.Items.Count - 1
If (CheckedListBox1.CheckedItems.Count > 5) Then
MessageBox.Show("You cannot select more than 5 numbers.")
Return
Else
If (CheckedListBox1.GetItemChecked(x) = True) Then
count = count + 1
ListBox1.Items.Add(CheckedListBox1.Items(x))
End If
End If
Next
End Sub

Limiting the range of values allowed in multiple textboxes

For my current assignment, I've been asked to create a form to allow customers to enter desired quantities of shoes in a textbox and then total the cost in a second textbox. However, the customer cannot purchase more than 3 of any one particular shoe. I attempted to do the first two shoes in the code below but am running into issues once I added the code for the second model.
Private Sub btnCompute_Click(sender As Object, e As EventArgs) Handles btnCompute.Click
Dim qntyClassy, qntyHigh As Double
txtQntyClassy.Text = qntyClassy
txtQntyHigh.Text = qntyHigh
If 0 < Int(txtQntyClassy.Text) And Int(txtQntyClassy.Text) < 4 Then
txtTotalClassy.Text = FormatCurrency(txtQntyClassy.Text * 43)
txtQntyClassy.Text = Int(txtQntyClassy.Text)
Else
MsgBox("Enter a quantity no greater than 3.")
End If
If 0 < Int(txtQntyHigh.Text) And Int(txtQntyHigh.Text) < 4 Then
txtTotalHigh.Text = FormatCurrency(txtQntyHigh.Text * 48.95)
txtQntyHigh.Text = Int(txtQntyHigh.Text)
Else
MsgBox("Enter a quantity no greater than 3.")
End If
End Sub
End Class

"Make sure you are not dividing by zero" error Visual Basic express

I am using Visual Basic express 2010.I want to create a programm that is asking for number and stops when 0 is given.Then I want to check all that values to find the minimum value the maximum value the average and the sum.I have that code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim total As Integer
total = 0
Dim average As Integer
average = 0
Dim loops As Integer
loops = 0
Dim max As Integer
max = 0
Dim min As Integer
min = Val(InputBox("Give me the first number"))
Dim number As Integer
number = Val(InputBox("Give me a number"))
Do Until number = 0
loops = loops + 1
If number < min Then
min = number
ElseIf number > max Then
max = number
End If
total = number + total
Loop
average = total / loops
MsgBox(total)
End Sub
End Class
When i hit F5 it brings up the screen.After the first 2 inputboxes the program crashes.Any ideas?
Hey take a look at this code.You had the problem because the number was not changing.
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim total As Integer
total = 0
Dim average As Integer
average = 0
Dim loops As Integer
loops = 0
Dim max As Integer
max = 0
Dim min As Integer
min = Val(InputBox("Give me the first number"))
Dim number As Integer
number = Val(InputBox("Give me a number"))
Do Until number = 0
loops = loops + 1
If number < min Then
min = number
ElseIf number > max Then
max = number
End If
total = number + total
number = Val(InputBox("Give me a number"))
Loop
average = total / loops
MsgBox(total)
End Sub
End Class
Take a look at your loop:
Do Until number = 0
loops = loops + 1
If number < min Then
min = number
ElseIf number > max Then
max = number
End If
total = number + total
Loop
It will continue until number is 0, but number is never modified. So if it's not already 0, it will never be 0. Thus, the loop continues indefinitely. In that loop you do this:
loops = loops + 1
Since loops is an Integer, it has a fairly finite range. Once it reaches the value of MaxInt, you can't add to it anymore. So the next time you add to it, you see an exception.
You'll need to modify number in the loop in order for it to ever finish.
Note that the "divide by zero" part isn't actually part of the error message you're seeing. It's just a suggestion that Visual Studio is showing you, since dividing by zero is a common source of this error. It's just not the source of the error this time.