dcsimg
Results 1 to 9 of 9

Thread: [RESOLVED] Arrays

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    55

    Resolved [RESOLVED] Arrays

    Hello all, I need some help in a question i've been trying to solve:

    For this problem, the user enters a number n (1<n<21) to indicate how many numbers he/she has, then he/she enters the n numbers. The program must then ask for an additional number. The output of the program will indicate whether the last number is contained in the n numbers. Your output should resemble what is provided in the example below. All of the numbers that will be entered are integers.
    I'm using an array and input boxes, this is where i am in code

    Code:
    Dim m As Integer
    Dim search(1 To 21) As Integer
    Dim y As Integer
    
    Private Sub addnumbers()
    For m = 1 To Text1.Text
    search(m) = InputBox("enter your numbers", "Number Entry")
    Next
    y = InputBox("Enter a final Number", "Number Entry")
    End Sub
    
    Private Sub Command1_Click()
    addnumbers
    End Sub
    The output is always the same to no matter what input it got

    This is an example of how the output should be

    Input:
    5
    1
    2
    15
    10
    5
    10

    Output:
    The number 10 appears in the 5 numbers.

  2. #2
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Smile Re: Arrays

    so what you want really ?
    Code:
    Option Explicit
    Dim m As Integer
    Dim search(1 To 21) As Integer
    Dim y As Integer
    Private Sub Command1_Click()
    addnumbers
    End Sub
    
    Private Sub addnumbers()
    For m = 1 To Val(Text1.Text)
    search(m) = InputBox("enter your numbers", "Number Entry")
    Next
    y = InputBox("Enter a final Number", "Number Entry")
    End Sub

  3. #3
    Fanatic Member
    Join Date
    Mar 2009
    Posts
    804

    Re: Arrays

    Where's the code for generating the output?

  4. #4

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    55

    Re: Arrays

    Code:
    Dim m As Integer
    Dim search(1 To 21) As Integer
    Dim y As Integer
    
    Private Sub addnumbers()
    For m = 1 To Text1.Text
    search(m) = InputBox("enter your numbers", "Number Entry")
    Next
    y = InputBox("Enter a final Number", "Number Entry")
    
    
    
    End Sub
    
    Private Sub Command1_Click()
    addnumbers
    If Int(search(y)) Then
    MsgBox "The number appears"
    Else
    MsgBox "the number doesn't appear"
    End If
    
    End Sub
    Sorry i forgot to attach code of the output

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,993

    Re: Arrays

    You'll need to loop (For:Next) thru your array items to see if Y is one of the array items.

    - As is, if someone entered 100 for their final number, you'd get an array bounds error
    - No point using Int() when what is in the parentheses is already an integer. Search() is an Integer array
    - Error checking.
    -- If someone enters a huge number or non-numeric value in your inputboxes, type-mismatch error occurs
    -- If someone enters number larger than 21 or non-numeric number in text1, errors will occur
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    55

    Re: Arrays

    I tried this, still the same thing
    Dim m As Integer
    Dim search(1 To 21) As Integer
    Dim y As Integer

    Private Sub addnumbers()
    For m = 1 To Text1.Text
    search(m) = InputBox("enter your numbers", "Number Entry")
    If Int(search(y)) Then
    MsgBox "The number appears"
    Else
    MsgBox "the number doesn't appear"
    End If
    Next
    y = InputBox("Enter a final Number", "Number Entry")



    End Sub

    Private Sub Command1_Click()
    addnumbers
    End Sub

  7. #7
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,993

    Re: Arrays

    You can't check if the final number is in the array before the array is filled out. You should have 2 loops: 1) to fill the array initially & get final number and 2) to loop thru the filled array looking for that final number
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  8. #8
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: Arrays

    Aster

    First a suggestion .. indents help in readability.
    (I also took the liberty to make a few editorial
    changes for clarity.)

    Code:
    Dim m As Integer
    Dim aaSearch(1 To 21) As Integer
    Dim y As Integer
    
    Private Sub AddNumbers()
        For m = 1 To Text1.Text
            aaSearch(m) = InputBox("enter your numbers", "Number Entry")
            If Int(aaSearch(y)) Then
                MsgBox "The number appears"
            Else
                MsgBox "the number doesn't appear"
            End If
        Next
        y = InputBox("Enter a final Number", "Number Entry")
    End Sub
    
    Private Sub Command1_Click()
        AddNumbers
    End Sub
    Now, some thoughts...
    • Your loop facilitates the populating of the array aaSearch.
    • You then facilitate the assignment of the value y.
    • What seems to be lacking is another loop that counts "hits" on the value of y that occur in the array aaSearch.


    EDIT:

    Now that I look at things again, this line pops out at me....
    • First, if this is to be your test for "hits", it occurs before y has even been assigned.
    • Second, as written, it really does not make any sense.

    Can you take it from here?

    EDIT-2:

    Arrgh .. LaVolpe snuck one in on me.

    Spoo
    Last edited by Spoo; Apr 11th, 2012 at 12:46 PM.

  9. #9

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    55

    Re: Arrays

    the suggested code above didn't work out properly because it starts searching for the y before the user actually enters the y.
    I tried this out and it worked perfectly well:
    Code:
    Dim m As Integer
    Dim aaSearch(1 To 21) As Integer
    Dim y As Integer
    
    Private Sub AddNumbers()
        For m = 1 To Text1.Text
            aaSearch(m) = InputBox("enter your numbers", "Number Entry")
          
        Next
        y = InputBox("Enter a final Number", "Number Entry")
          If Int(aaSearch(y)) Then
                MsgBox "The number appears"
            Else
                MsgBox "the number doesn't appear"
            End If
    End Sub
    
    Private Sub Command1_Click()
        AddNumbers
    End Sub
    Thanks for your help everyone

Tags for this Thread

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width