1. ## [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

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()
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. ## 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()
End Sub

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. ## Re: Arrays

Where's the code for generating the output?

4. ## Re: Arrays

Code:
```Dim m As Integer
Dim search(1 To 21) As Integer
Dim y As Integer

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()
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. ## 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

6. ## Re: Arrays

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

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()
End Sub

7. ## 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

8. ## 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

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()
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

9. ## 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

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()
End Sub```

#### 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