Rats multiplying...

pnklphnts
09-05-2003, 06:45 PM
I have been assigned a problem that says that 2 rats are on an island and they have 6 kids every 40 days then those kids after 120 days start having there own kids every thirty days. THen after a year how many rats are there. So i decided to write a program to slove it and impress my teacher. I finished my program but it doesnt seem to work. There is no error messege but when it finishs there are still only two rats. Here is my program:

'// In a Module
Public Type RatType
CreationDate As Long
LastBabyDate As Long
End Type
Public Rat() As RatType
Public Days As Long
'// On Form
Private Sub Form_Load()
ReDim Rat(1 To 2) '// Establish Origanal 2
For I = 1 To 2
Rat(I).CreationDate = -120 '// We want them to start having babies instantly
Next
Days = 365 '// set the number of days
End Sub


Private Sub Calculate_Click()
For D = 1 To Days
For R = 1 To LBound(Rat)
If Rat(R).CreationDate = (D + 120) Then '// If They just matured to baby age then...
Rat(R).LastBabyDate = D '// Set there last baby date
ReDim Preserve Rat(LBound(Rat) To UBound(Rat) + 3) '// We only make 3 New babies to account for the other rat
For I = LBound(Rat) To UBound(Rat) + 3
Rat(I).CreationDate = D
Next
ElseIf Rat(R).CreationDate > (D + 120) Then '// If they've matured before this day
If Rat(R).LastBabyDate = D + 40 Then '// if its time for them to have another group of babies
ReDim Preserve Rat(LBound(Rat) To UBound(Rat) + 3) '// We only make 3 New babies to account for the other rat
For I = LBound(Rat) To UBound(Rat) + 3
Rat(I).CreationDate = D
Next
Rat(R).LastBabyDate = D
End If
End If
Next
Day.Caption = D
Mice.Caption = R
Next
Form1.Caption = "Done"
End Sub

I cannot figure out what is messing up. Thanks for your help.

Deadalus
09-05-2003, 07:13 PM
You could always say that one rat decided to have a sex change. (Sorry, just had to make that joke.)

Did you try stepping through that code in debug mode? My guess is that it isn't even close to working like you think it does. The second loop is from 1 to 1 and neither of the conditions in the main if block will ever be true.

You can use VB tags to display VB code in the forum.

Corrected mistake.

pnklphnts
09-05-2003, 07:22 PM
how would I step through it in debug mode (im new to this debuging stuff)?

Deadalus
09-05-2003, 07:35 PM
You can begin by putting in one or more breakpoints (clicking on the left bar in the IDE). When execution stops on a line with a breakpoint, you can use F5 to run to the next breakpoint or F8 to go to the next executed line.

Deadalus
09-05-2003, 08:38 PM
You could begin by uncomplicating your setup and just work with an array of rat ages, which is really all information you need. The first two array items you initialise at 120, all new ones will start at 0. For every day, you check for every rat if its age > 119 and divisible by 40 (rats(R) Mod 40 = 0). If yes, you add three to a NewRats counter. For every rat, you add 1 to its age. After you've looped through the rats you redimension your array, adding the NewRats, and start again for the next day.

lebb
09-05-2003, 10:17 PM
how would I step through it in debug mode (im new to this debuging stuff)?
Do a search for Thinker's tutorial on debugging in Tutor's Corner.

pnklphnts
09-06-2003, 12:29 PM
I changed some of it so now it looks like this:

Private Sub Calculate_Click()
For D = 1 To 365
For R = 1 To LBound(Rat)
If Rat(R).CreationDate = (D - 120) Then '// If They just matured to baby age then...
Rat(R).LastBabyDate = D '// Set there last baby date
ReDim Preserve Rat(LBound(Rat) To UBound(Rat) + 3) '// We only make 3 New babies to account for the other rat
For I = LBound(Rat) To UBound(Rat) + 3
'// WHEN IT GETS HERE IT SAYS SUB SCRIPT OUT OF RANGE
Rat(I).CreationDate = D
Next
ElseIf Rat(R).CreationDate < (D - 120) Then '// If they've matured before this day
If Rat(R).LastBabyDate = D - 40 Then '// if its time for them to have another group of babies
ReDim Preserve Rat(LBound(Rat) To UBound(Rat) + 3) '// We only make 3 New babies to account for the other rat
For I = LBound(Rat) To UBound(Rat) + 3
Rat(I).CreationDate = D
Next
Rat(R).LastBabyDate = D
End If
End If
Next
Day.Caption = D
Mice.Caption = R
Next
Form1.Caption = "Done"
End Sub

as ive marked it says subscript out of range there. So it gives me an error messege there. I geuss I am adding to the array wrong...

alp0001
09-06-2003, 01:11 PM
Try getting rid of the +3 in the loop statement (I'm not sure why it is there anyway).

passel
09-06-2003, 01:26 PM
Yes, as alp0001 says.
The ReDim is increasing the number of Rats in the array, so the Ubound
function returns the new upper limit (includes the 3 new rats), in your
loop.

Your adding +3 again causes the loop to try and go beyound the end of
your array.

Same thing in your second loop.

I also see that you changed the birth rate. In your original post you
said each child would have offspring every 30 days, not 40, like the
original parents.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum