Computing.Net > Forums > Programming > vb6 - gettin maximum from an array

vb6 - gettin maximum from an array

Reply to Message Icon

Original Message
Name: beingmadrocks
Date: April 17, 2006 at 16:39:52 Pacific
Subject: vb6 - gettin maximum from an array
OS: xp
CPU/Ram: pentium5 512 ram i think
Model/Manufacturer: dell
Comment:

Hi
I'm having terrible problems with this one - been trying for weeks to sort it. I need to find out the maximum h/d, speed, transfer rate from an array but all my program does is print out the first record every time. I'd appreciate some help with this as it's driving me mad. The following is an excerpt.

For index = 0 To 1

yname(index) = InputBox("Please enter Hard Drive's name")

capacity(index) = InputBox("Please enter Hard Drive's capacity")

transferrate(index) = InputBox("Please enter Hard Drive's transfer rate")

Next index

End Sub

Private Sub calculate(ByRef capacity() As Single, transferrate() As Long, transferspeed() As Single)
Dim index As Integer

index = index

For index = 0 To 1

transferspeed(index) = capacity(index) * 1024 * (8 / transferrate(index))

Next index

End Sub
Private Sub fastestdrive(ByRef transferspeed() As Single, ByVal fastest As Integer)
Dim index As Integer
Dim total1 As Integer

index = 0

total1 = transferspeed(index)

For index = 0 To 1
If transferspeed(index) < total1 Then
fastest = index
End If
Next index

End Sub

Any ideas??

Thanks in advance.


being mad really does rock

being mad really does rock!!!


Report Offensive Message For Removal


Response Number 1
Name: Chi Happens
Date: April 18, 2006 at 06:04:24 Pacific
Subject: vb6 - gettin maximum from an array
Reply: (edit)

first off the fastest drive code is wrong. You are never readjusting the total1 variable. It looks like you are trying to do a simple sort but you are not doing it right.

try this:
total1 = transferspeed(0)

For index = 0 To 1
If transferspeed(index) < total1 Then
fastest = index
total1 = transferspeed(index)
End If
Next index

that will at least set the correct fastest speed (assuming that you might have more than 2) otherwise it is over engineered and would be better written as:
if transferspeed(0) < transferspeed(1) then
fastest = 0
elseif transferspeed(0) > transferspeed(1) then
fastest = 1
else
fastest = -1 ' they are same speed
end if

finally, you don't show the code that displays the drive(s) so I can't tell what's wrong with that (you say it only shows the first one)

Post more.

Chi

They mostly come at night...mostly.


Report Offensive Follow Up For Removal

Response Number 2
Name: beingmadrocks
Date: April 18, 2006 at 09:46:05 Pacific
Subject: vb6 - gettin maximum from an array
Reply: (edit)

Hi

Thanks for replying so quickly - I've put your code in the fastest speed bit & altered it so that there are 3 records to be found but it's still not finding the fastest - it keep showing the first hd. For each hardrive I enter name hd1, hd2, hd3 then 4.8 for capacity for each, then 231, 231,233 for transfer rate. The entire code is below.

Private Sub cmdEnd_Click()
End
End Sub

Private Sub cmdRun_Click()
Dim transferspeed(0 To 2) As Single
Dim transferrate(0 To 2) As Long
Dim yname(0 To 2) As String
Dim capacity(0 To 2) As Single
Dim fastest As Variant

Call properties(yname(), transferrate(), capacity())
Call calculate(capacity(), transferrate(), transferspeed())
Call fastestdrive(transferspeed(), fastest)
Call displayspeed(capacity(), transferspeed(), transferrate(), yname(), fastest)

End Sub

Private Sub properties(ByRef yname() As String, transferrate() As Long, capacity() As Single)
Dim index As Integer

index = 0

For index = 0 To 2

yname(index) = InputBox("Please enter Hard Drive's name")

capacity(index) = InputBox("Please enter Hard Drive's capacity")

transferrate(index) = InputBox("Please enter Hard Drive's transfer rate")

Next index

End Sub

Private Sub calculate(ByRef capacity() As Single, transferrate() As Long, transferspeed() As Single)
Dim index As Integer

index = 0

For index = 0 To 2

transferspeed(index) = capacity(index) * 1024 * (8 / transferrate(index))

Next index

End Sub
Private Sub fastestdrive(ByRef transferspeed() As Single, ByVal fastest As Variant)
Dim index As Integer
Dim total1 As Integer

total1 = transferspeed(0)

For index = 0 To 2
If transferspeed(index) < total1 Then
fastest = index
total1 = transferspeed(index)
End If
Next index

End Sub

Private Sub displayspeed(ByRef capacity() As Single, transferspeed() As Single, transferrate() As Long, yname() As String, fastest As Variant)

picDisplay.Print "The fastest Hard Drive is:"; yname(fastest); ""
picDisplay.Print "The capacity of the fastest Hard Drive is "; capacity(fastest); "Gb"
picDisplay.Print "The transfer rate of the fastest Hard Drive is "; transferrate(fastest); "mbps"
picDisplay.Print "The transfer time of the fastest Hard Drive is "; transferspeed(fastest); "seconds"
picDisplay.Print

End Sub

thanks very much.

being mad really does rock!!!


Report Offensive Follow Up For Removal

Response Number 3
Name: Michael J (by mjdamato)
Date: April 18, 2006 at 10:45:03 Pacific
Subject: vb6 - gettin maximum from an array
Reply: (edit)

I'm not familiar with VB6, but you problem could be due to variable scope. Is "fastest" treated as a global variable?

In any case, you just need to add some debugging code to see that your data is being properly handled. For instance you should have a prompt or alert which displays each of the three drives and all their variables. Is all the data the same as you entered, are the drive speeds calculated correctly for each? Don't assume that the problem is in the code which determines the fastest drive, the problem could be int he data that is passed to that function.

Then I would have an alert that displays the value for the variable "fastest" at the end of the routine which calculates the fastest drive and at the beginning of the routine which displays the fastest drive. Is it the same. This is basic debugging 101.

Michael J


Report Offensive Follow Up For Removal

Response Number 4
Name: Chi Happens
Date: April 25, 2006 at 11:07:39 Pacific
Subject: vb6 - gettin maximum from an array
Reply: (edit)

I think i see the problem. You are passing fastes byval it needs to be passed byref otherwuse it will always be zero when you return from the calc function.

Chi

They mostly come at night...mostly.


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: vb6 - gettin maximum from an array

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 2 Days.
Discuss in The Lounge