Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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 1yname(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 indexEnd Sub
Private Sub calculate(ByRef capacity() As Single, transferrate() As Long, transferspeed() As Single)
Dim index As Integerindex = index
For index = 0 To 1
transferspeed(index) = capacity(index) * 1024 * (8 / transferrate(index))
Next indexEnd Sub
Private Sub fastestdrive(ByRef transferspeed() As Single, ByVal fastest As Integer)
Dim index As Integer
Dim total1 As Integerindex = 0
total1 = transferspeed(index)
For index = 0 To 1
If transferspeed(index) < total1 Then
fastest = index
End If
Next indexEnd Sub
Any ideas??
Thanks in advance.
being mad really does rockbeing mad really does rock!!!

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 indexthat 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 iffinally, 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.

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 SubPrivate 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 VariantCall 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 Integerindex = 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 Integerindex = 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 Integertotal1 = transferspeed(0)
For index = 0 To 2
If transferspeed(index) < total1 Then
fastest = index
total1 = transferspeed(index)
End If
Next indexEnd 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.PrintEnd Sub
thanks very much.
being mad really does rock!!!

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

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.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |