Visual Basic Foro
Programación => Visual Basic 6 => Mensaje iniciado por: Bazooka en Agosto 03, 2012, 08:58:38 pm
-
Hola estoy enroscado como una serpiente tratando de hacer una boludez pero parece que hay ciertas cosas que cuestan mas que otras y ami esta es una la cuestión es que necesito crear una funcion que me devuelva el indice de la matriz que contenga el mayor valor. Por ahi hay alguna funcion o API que no conozco y me ayuda o algún colega!!
Un ejemplo
Private Sub Command1_Click()
Dim MiArray(5) As Byte
MiArray(0) = 12
MiArray(1) = 4
MiArray(2) = 89
MiArray(3) = 42
MiArray(4) = 78
MiArray(5) = 2
Index = DevuelveIndiceMayor(MiArray)
End Sub
Private Function DevuelveIndiceMayor(Mat() As Byte) As Byte
'??
End Function
-
Private Function DevuelveIndiceMayor(Mat() As Byte) As Byte
Dim i as Integer, j as Integer, max as Byte
j = ubound(mat)
for i = 0 to j
if mat(i) > max Then max = mat(i)
next
DevuelveIndiceMayor = max
End Function
Algo así buscas? :P o de ultima si necesitas el indice, devuelves i puro :P (usando otra variable)
-
Muchas gracias Raul, no se si les pasa que a veces tienen tan saturada la mente que no pueden resolver algo simple! Justamente me desperte con una funcion en la cabeza muy similar a la que me encontre en este foro!!
Aca la posteo como quedo finalmente devolviendo el indice que es lo que necesitaba!
Private Function DevuelveIndiceMayor(Mat() As Byte) As Byte
Dim i As Integer, j As Integer, max As Byte, Index As Byte
j = UBound(Mat)
For i = 0 To j
If Mat(i) > max Then max = Mat(i): Index = i
Next
DevuelveIndiceMayor = Index
End Function
-
sino me equivoco, te seria mas conveniente que la variable i sea =LBound(Mat), solo por si no es Zero