9 Replies - 349 Views - Last Post: 13 July 2014 - 06:05 PM Rate Topic: -----

#1 Wernir  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • 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

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        ' 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  Icon User is online

  • D.I.C Regular

Reputation: 54
  • Posts: 323
  • 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

Was This Post Helpful? 1
  • +
  • -

#3 Wernir  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 28-June 14

Re: Using Arrays Homework Help

Posted 13 July 2014 - 11:50 AM

Its just not making sense to me.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3175
  • View blog
  • Posts: 10,625
  • 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).

However, he has included other comments which you should read.

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

Was This Post Helpful? 0
  • +
  • -

#5 Wernir  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • 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?
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3175
  • View blog
  • Posts: 10,625
  • 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

Was This Post Helpful? 1
  • +
  • -

#7 Wernir  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • 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.
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3175
  • View blog
  • Posts: 10,625
  • Joined: 12-December 12

Re: Using Arrays Homework Help

Posted 13 July 2014 - 12:31 PM

I added to my previous answer and your teacher is correct.

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

Was This Post Helpful? 0
  • +
  • -

#9 Wernir  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • 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.
Was This Post Helpful? 0
  • +
  • -

#10 jimzcoder  Icon User is online

  • D.I.C Regular

Reputation: 54
  • Posts: 323
  • 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. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1