|
-
Apr 11th, 2012, 06:16 AM
#1
Thread Starter
Member
[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.
-
Apr 11th, 2012, 07:00 AM
#2
Frenzied Member
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
-
Apr 11th, 2012, 07:02 AM
#3
Re: Arrays
Where's the code for generating the output?
-
Apr 11th, 2012, 07:06 AM
#4
Thread Starter
Member
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
-
Apr 11th, 2012, 10:54 AM
#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
-
Apr 11th, 2012, 12:21 PM
#6
Thread Starter
Member
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
-
Apr 11th, 2012, 12:37 PM
#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
-
Apr 11th, 2012, 12:38 PM
#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
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.
-
Apr 16th, 2012, 06:03 AM
#9
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|