Click to See Complete Forum and Search --> : How to search an array
eyez01
October 8th, 2001, 06:35 AM
Hi,
I have just started programming in visual and I'm trying to create an array of 10 elements. A user will enter in 10 numbers and then I need to search through the array for numbers that are greater than 0 but I'm hitting loads of errors. Can someone help me?
Cakkie
October 8th, 2001, 06:54 AM
Dim myArray(9) as Long
Dim T as integer
for T=0 to 9
myArray(T) = Inputbox("Please give number " & T)
next T
for T=0 to 9
If myArray(T) > 0 then
Msgbox "Number " & T & " (" & myArray(T) & ") is greater than 0"
else
Msgbox "Number " & T & " (" & myArray(T) & ") is not greater than 0"
End If
next T
Tom Cannaerts
slisse@planetinternet.be
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
srinika
October 8th, 2001, 06:55 AM
Dim a(10) as Integer
Dim v as Variant
Dim i as Integer
for i = 0 to 10
v = InputBox("Put the Array Element(" & i & ")")
If v = "" then Exit for
a(i) = v
next i
for i = 0 to 10
If a(i) > 0 then
MsgBox a(i)
End If
next i
Cimperiali
October 8th, 2001, 07:11 AM
This may be a way:
'written here and not tested. So beware of bugs!
private sub command1_click()
dim aintNumber(9) as variant
dim i as integer
dim ret as integer
for i = 0 to 9
aintNumber(i)=inputbox("insert a number (0 to 32000)")
If aintnumber(i)="" or not isnumeric(aintnumber(i)) then
ret =msgbox("do you want to exit",vbyesno)
if ret=vbyes then
exit sub
else
aintnumber(i)=0
end if
end if
next
'find all numer greater than 0
'two strategies: put all numer greater than 0 in another array or
'use another array to mark positions of the numbers in first array
'choosen method: the second.
dim theIndexesArray() as integer
dim blnfisrt as boolean
blnfisrt= true
for i = 0 to 9
if aintNumber(i)> 0 then
if blnfirst then
blnfirst = false
redim theIndexesArray(0)
else
redim preserve theIndexesArray(ubound(theIndexesArray)+1)
end if
theIndexesArray(ubound(theIndexesArray))= i
end if
next
'show all numbers
dim strResult
strResult = "Position in first array of number greater than 0 = "
strValues = "Values of numbers greater than 0 = "
for i = 0 to ubound(theIndexesArray)
strResult=strResult & theIndexesArray(i) & "; "
strValues =strValues & aintNumber(theIndexesArray(i)) & "; "
next i
msgbox strResult & vbcrlf & strValues
end sub
Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Michael
and all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
The Rater
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.