CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2001
    Posts
    1

    How to search an array

    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?


  2. #2
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: How to search an array


    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
    [email protected]

    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
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

  3. #3
    Join Date
    Jun 2001
    Location
    Sri Lanka
    Posts
    272

    Re: How to search an array


    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





    If u don't know how to Rate an answer, then Rate my answer to learn, If u know, then practice it

  4. #4
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: How to search an array

    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
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured