# Using Arrays Homework Help

Page 1 of 1

## 9 Replies - 686 Views - Last Post: 13 July 2014 - 06:05 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=350271&amp;s=5bdaef1a7c2ca3e4301d499f7f805914&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Wernir

Reputation: 0
• Posts: 20
• Joined: 28-June 14

# Using Arrays Homework Help

Posted 13 July 2014 - 06:37 AM

This assignment deals with arrays. The user inputs a charge account number to see if it is stored in the array. The message box indicates wether or not it is in the array.
The problem I'm having is that no matter what number I enter the message comes back as false. Again this is Homework and I just need to be pointed in the right direction. Thanks.

```Option Explicit On
Option Strict On

Public Class Form1

' Class level Declarations.
Const intMax_Subscript As Integer = 17
Dim ValidNumbers(intMax_Subscript) As Integer

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click

' Closes the form.
Me.Close()
End Sub

' Initialize array.
InitArrays()
End Sub

Private Sub InitArrays()

'Intialize Arrays.
ValidNumbers(0) = 5658845
ValidNumbers(1) = 8080152
ValidNumbers(2) = 1005231
ValidNumbers(3) = 4520125
ValidNumbers(4) = 4562555
ValidNumbers(5) = 6545231
ValidNumbers(6) = 7895122
ValidNumbers(7) = 5552012
ValidNumbers(8) = 3852085
ValidNumbers(9) = 8777541
ValidNumbers(10) = 5050552
ValidNumbers(11) = 7576651
ValidNumbers(12) = 8451277
ValidNumbers(13) = 7825877
ValidNumbers(14) = 7881200
ValidNumbers(15) = 1302850
ValidNumbers(16) = 1250255
ValidNumbers(17) = 4581002

End Sub

Private Sub btnValidate_Click(sender As Object, e As EventArgs) Handles btnValidate.Click
Dim intUserNumber As Integer
Dim found As Boolean
Dim count As Integer
Dim position As Integer

If Not Integer.TryParse(txtUserNumber.Text, intUserNumber) Then
MessageBox.Show("Your Account Numbers Must All Be Integers.")
End If

found = False
count = 0

Do While found = False And count < ValidNumbers.Length
If intUserNumber = ValidNumbers(intMax_Subscript) Then
found = True
position = count

End If
count += 1
Loop

If found = False Then
MessageBox.Show("Sorry The Account Number is Invalid.")
If found = True Then
MessageBox.Show("The Account Number is Valid.")
End If
End If

End Sub
End Class

```

Is This A Good Question/Topic? 0

## Replies To: Using Arrays Homework Help

### #2 jimzcoder

• D.I.C Regular

Reputation: 57
• Posts: 349
• Joined: 14-November 12

## Re: Using Arrays Homework Help

Posted 13 July 2014 - 06:42 AM

check this line 9 where you declared
``` Const intMax_Subscript As Integer = 17
'then as you initialized your array
ValidNumbers(0) = 5658845
34
ValidNumbers(1) = 8080152
35
ValidNumbers(2) = 1005231
36
ValidNumbers(3) = 4520125
37
ValidNumbers(4) = 4562555
38
ValidNumbers(5) = 6545231
39
ValidNumbers(6) = 7895122
40
ValidNumbers(7) = 5552012
41
ValidNumbers(8) = 3852085
42
ValidNumbers(9) = 8777541
43
ValidNumbers(10) = 5050552
44
ValidNumbers(11) = 7576651
45
ValidNumbers(12) = 8451277
46
ValidNumbers(13) = 7825877
47
ValidNumbers(14) = 7881200
48
ValidNumbers(15) = 1302850
49
ValidNumbers(16) = 1250255
50
ValidNumbers(17) = 4581002 'this is suppose to throw an overflow or OutOfBounce exception since this exceeds the length of your array.

'this code line here

Do While found = False And count < ValidNumbers.Length
74           'you are suppose to use the variable count instead of intMax_Subscript constant
'this will always compare the last value of your array no matter how many times you loop
'If intUserNumber = ValidNumbers(intMax_Subscript) Then 'You must change this into:
75
If intUserNumber = ValidNumbers(count) Then
found = True
76
position = count
77              'you can exit the loop immediately after it founds a match because you are only looking for a single match case in here
Exit Do 'can do this

78
End If
79
count += 1
80
Loop

```

Hope it makes sense.. good luck

This post has been edited by jimzcoder: 13 July 2014 - 07:43 AM

### #3 Wernir

Reputation: 0
• Posts: 20
• Joined: 28-June 14

## Re: Using Arrays Homework Help

Posted 13 July 2014 - 11:50 AM

Its just not making sense to me.

### #4 andrewsw

Reputation: 5463
• Posts: 21,473
• Joined: 12-December 12

## Re: Using Arrays Homework Help

Posted 13 July 2014 - 12:05 PM

jimzcoder is incorrect about the out-of-bounds error. He is suggesting that ValidNumbers(16) is the last element of the array, but it is ValidNumbers(17). When the array is declared 17 is the upper-bound of the array, not the size (which it is in most other languages).

This post has been edited by andrewsw: 13 July 2014 - 12:07 PM

### #5 Wernir

Reputation: 0
• Posts: 20
• Joined: 28-June 14

## Re: Using Arrays Homework Help

Posted 13 July 2014 - 12:07 PM

I still dont really understand why my loop isnt working though?

### #6 andrewsw

Reputation: 5463
• Posts: 21,473
• Joined: 12-December 12

## Re: Using Arrays Homework Help

Posted 13 July 2014 - 12:27 PM

One main problem is:
```    If found = False Then
MessageBox.Show("Sorry The Account Number is Invalid.")
If found = True Then
MessageBox.Show("The Account Number is Valid.")
End If
End If
```

The True message will never appear, because it is nested within the test for False. You need to use ElseIf:
```        If found = False Then
MessageBox.Show("Sorry The Account Number is Invalid.")
ElseIf found = True Then
MessageBox.Show("The Account Number is Valid.")
End If
```

although you can just use Else, because if it isn't False then it must be True.

jimzcoder has already described in his comments the other main error, in this line:
```If intUserNumber = ValidNumbers(intMax_Subscript) Then
```

You are constantly checking the last element of the array, rather than the elements 0,1,2,3...

This post has been edited by andrewsw: 13 July 2014 - 12:28 PM

### #7 Wernir

Reputation: 0
• Posts: 20
• Joined: 28-June 14

## Re: Using Arrays Homework Help

Posted 13 July 2014 - 12:27 PM

Thanks I overlooked that. It makes sense to me but Im still having trouble with the loop. I'm writing it as the book states and it isnt working. When I asked the teacher the answer is that the loop isnt searching the entire array. I cannot find any examples of how it is supposed to look.

### #8 andrewsw

Reputation: 5463
• Posts: 21,473
• Joined: 12-December 12

## Re: Using Arrays Homework Help

Posted 13 July 2014 - 12:31 PM

Quote

I cannot find any examples of how it is supposed to look.

That is a curious statement. You need to understand, and work out, why it is not working, and correct it.

This post has been edited by andrewsw: 13 July 2014 - 12:32 PM

### #9 Wernir

Reputation: 0
• Posts: 20
• Joined: 28-June 14

## Re: Using Arrays Homework Help

Posted 13 July 2014 - 12:40 PM

I'm so frustrated I'm done for now. Im just stuck and you are right I dont understand why its wrong. Im new to coding and it isnt making that much sense to me.

### #10 jimzcoder

• D.I.C Regular

Reputation: 57
• Posts: 349
• Joined: 14-November 12

## Re: Using Arrays Homework Help

Posted 13 July 2014 - 06:05 PM

oopss. i stand corrected about the array size. thanks andrewsw
just thought it would be the same as c# and i was wrong.
apologies.

Page 1 of 1

 .related ul{list-style-type:circle;font-size:12px;font-weight:bold;}.related li{margin-bottom:5px;background-position:left 7px!important;margin-left:-35px;}.related h2{font-size:18px;font-weight:bold;}.related a{color:blue;}