PDA

View Full Version : Two Dimensional arrays


George Hallam
11-28-2008, 07:33 AM
Can anyone teach me how to do them, my VB book doesn't go that much in depth with them and i really need to learn how to do them for my end of year project :rolleyes:

Thank you
~ George :D

Paul Komski
11-28-2008, 10:45 AM
Download the demo example (http://www.freevbcode.com/ShowCode.asp?ID=2624). Unzip it and run Array.vbp which should open the demo in your VB6 software.

Experiment and view the code to see how it all functions. Post back if there is anything you don't understand.

Don't get freaked out by arrays - just develop your knowledge of them one step at a time. Basically they are just a special form of variable, whose elements are referenced by "index" values, each one of which always begins with zero.

The are commonly populated by on load events or for/next loops (typically two nested for/next loops for a two dimensional array) but each indexed entry can be given a value by merely stating, for example:
myfirstarray (1,2) = "bob"

Their data type can only be of one type such as string or integer etc but one can mix numeric and text values in string arrays and then deal with or convert the output at a later stage.

George Hallam
11-29-2008, 08:31 AM
Ok i have spent a few hours (5 :rolleyes:) today but i have finished the whole program

here it is

*note its quite big

Option Explicit
'The global area of the code, this means that everything difined in this section of the code counts for the whole_
'of the program
Dim students(1 To 20, 1 To 4) As String 'The two dimensional array to store the students names and grades
Dim index As Integer 'The index of that student it is on

Private Sub cmdaddstudent_Click()
Dim student_index As Integer
Dim row As Integer
Dim col As Integer
Dim student_to_add As String
If (txtstudentname.Text = "") Or (txtgrade1.Text = "") Or (txtgrade2.Text = "") Or (txtgrade3.Text = "") Or index = 20 Then
'This message box displays, what the user must do. It also has a title and a OK command [to close it]
MsgBox "Please fill out all four text boxes and check that you do not have more than 20 names", vbOKOnly, Title:="An error has occurred"
Else
index = index + 1 'adds one to the index that difines what student the program is on
student_index = 1 'Selects part 1 of 4 in the student index
students(index, student_index) = txtstudentname.Text 'adds the students name to part 1 of 4
student_index = 2 'Selects part 2 of 4 in the student index
students(index, student_index) = txtgrade1.Text 'adds the students 1st grade to part 2 of 4
student_index = 3 'Selects part 3 of 4 in the student index
students(index, student_index) = txtgrade2.Text 'adds the students 2st grade to part 3 of 4
student_index = 4 'Selects part 4 of 4 in the student index
students(index, student_index) = txtgrade3.Text 'adds the students 3st grade to part 4 of 4
txtstudentname.Text = ""
txtstudentname.SetFocus 'Sets focus to the students name so you can start again
txtgrade1.Text = "" 'Clears the text box
txtgrade2.Text = ""
txtgrade3.Text = ""
End If
lststudentlist.Clear 'Clears the list
For row = 1 To index 'The array going across from 1 to how over many item are in your array
student_to_add = ""
For col = 1 To 4 'The array going down from 1 to 4 [from student name to grade3]
student_to_add = student_to_add & students(row, col) & " " 'The loop goes from 1 to 4 adding each item to the varible
Next col
lststudentlist.AddItem student_to_add 'Adds the item to the list
Next row
cmdsearchstudent.Enabled = True 'sets the enable value to true when the command button is clicked making these command buttons usable
cmsstudentspassed.Enabled = True
cmdstudentsnotpassed.Enabled = True
cmdhighestaverage.Enabled = True
cmdhighestsingle.Enabled = True
End Sub

Private Sub cmdclearlist_Click()
lstfilterd.Clear 'Clears the list above it
End Sub

Private Sub cmdhighestaverage_Click()
Dim highest_average As Single
Dim name_of_best As String
Call calc_highest_average_mark(highest_average, name_of_best) 'Calls a procedure
'This message box displays, The highest average grade and the name of the student that got it. It also has a title and a OK command [to close it]
MsgBox "The highest average grade was " & highest_average & " by " & name_of_best, vbOKOnly, Title:="The highest average mark"
End Sub

Private Sub cmdhighestsingle_Click()
Dim highest As Integer
Dim name_of_best As String
Call calc_highest_mark(highest, name_of_best)
'This message box displays, The highest grade and the name of the student that got it. It also has a title and a OK command [to close it]
MsgBox "The highest grade was " & highest & " by " & name_of_best, vbOKOnly, Title:="The highest average mark"
End Sub

Private Sub cmdreset_Click()
'On the click of this command buttom it sets all the varibles to "0" and emptys all text boxes and clears all lists aswell as setting
'cirtain command buttoms properties to enabled = false
index = 0
lststudentlist.Clear
lstfilterd.Clear
cmdsearchstudent.Enabled = False
cmsstudentspassed.Enabled = False
cmdstudentsnotpassed.Enabled = False
cmdhighestaverage.Enabled = False
cmdhighestsingle.Enabled = False
cmdclearlist.Enabled = False
txtsearchstudent.Text = ""
txtstudentname.Text = ""
txtgrade1.Text = ""
txtgrade2.Text = ""
txtgrade3.Text = ""
End Sub

Private Sub cmdsearchstudent_Click()
Dim name As String
Dim row As Integer
Dim not_there As Integer
name = txtsearchstudent.Text
not_there = 0
For row = 1 To index 'Starts loop to find if the student is there
If students(row, 1) = name Then 'If the row number and the 1 of 4 [the students name] is equal to name then
'This message box displays, The name of the student, that they were found and there three grades. It also has a title and a OK command [to close it]
MsgBox "Your student " & name & " got the grades " & students(row, 2) & " " & students(row, 3) & " " & _
students(row, 4), vbOKOnly, Title:="Your student was found"
Else 'if it is not true add one to the not_there varible
not_there = not_there + 1
End If
Next row
If not_there = index Then 'if the name was not found then the times the loop has gone round should be equal to the index
'This message box displays, the name of the student and that they were not found. It also has a title and a OK command [to close it]
MsgBox "Your student " & name & " was not there please check spelling and capitals and try again", _
vbOKOnly, Title:="Your student was not found"
End If
End Sub

Private Sub cmdstudentsnotpassed_Click()
Dim average As Single
Dim row As Integer
Dim col As Integer
Dim name As String
lbllisttitle.Caption = ""
lbllisttitle.Caption = "These students did not pass" 'The caption caption changes depending on what command event you click
average = 0
For row = 1 To index 'The loop starts, from row 1 [of the 2D array] to the amount in the array
For col = 2 To 4 'Then another loop starts, from coloum 2 to 4 [of the 2D array]
average = average + students(row, col) 'goes through each of the 3 grades and adds then together
Next col
average = average / 3 'works out average
name = students(row, 1)
If average <= 50 Then 'If they got a mark less than 50 they do not pass
lstfilterd.AddItem students(row, 1) & " " & students(row, 2) & " " & students(row, 3) & " " & students(row, 4) 'adds their name to the list
End If
average = 0 'sets the average back to "0"
Next row
cmdclearlist.Enabled = True
End Sub

Private Sub cmsstudentspassed_Click()
Dim average As Single
Dim row As Integer
Dim col As Integer
Dim name As String
lbllisttitle.Caption = ""
lbllisttitle.Caption = "These students passed" 'The caption caption changes depending on what command event you click
average = 0
For row = 1 To index 'The loop starts, from row 1 [of the 2D array] to the amount in the array
For col = 2 To 4 'Then another loop starts, from coloum 2 to 4 [of the 2D array]
average = average + students(row, col) 'goes through each of the 3 grades and adds then together
Next col
average = average / 3 'works out average
name = students(row, 1)
If average >= 50 Then 'If they got a mark larger than 50 they pass
lstfilterd.AddItem students(row, 1) & " " & students(row, 2) & " " & students(row, 3) & " " & students(row, 4) 'adds their name to the list
End If
average = 0 'sets the average back to "0"
Next row
cmdclearlist.Enabled = True
End Sub

Private Sub Form_Load()
index = 0
End Sub

Public Sub calc_highest_average_mark(ByRef highest_average, ByRef name)
Dim row As Integer
Dim col As Integer
Dim current As Single
highest_average = 0 'Sets values of varibles to "0" or blank [""]
current = 0
name = ""
For row = 1 To index 'The loop starts, from row 1 [of the 2D array] to the amount in the array
For col = 2 To 4 'Then another loop starts, from coloum 2 to 4 [of the 2D array]
highest_average = highest_average + students(row, col) 'Adds the three grades that the student got together
Next col
If highest_average >= current Then 'If the grade they got was larger than the previous that becomes the new highest
current = highest_average
name = students(row, 1) 'Stores the name of the student who got this high grade
Else
current = current
End If
highest_average = 0 'High averages goes back to "0" before the loop starts again
Next row
current = current / 3 'Works out average
highest_average = current
End Sub

George Hallam
11-29-2008, 08:34 AM
continued...


Public Sub calc_highest_mark(ByRef highest, ByRef name)
'This procedure is a slight variation of "calc_highest_average_mark"
Dim row As Integer
Dim col As Integer
Dim current As Single
highest = 0 'Sets values of varibles to "0" or blank [""]
current = 0
name = ""
For row = 1 To index 'The loop starts, from row 1 [of the 2D array] to the amount in the array
For col = 2 To 4 'Then another loop starts, from coloum 2 to 4 [of the 2D array]
highest = students(row, col)
If highest >= current Then 'If the grade they got was larger than the previous that becomes the new highest
current = highest
name = students(row, 1) 'Stores the name of the student who got this high grade
Else
current = current
End If
Next col
highest = 0 'High goes back to "0" before the loop starts again
Next row
highest = current
End Sub


i attached the form to the end of that message, its clean (no virus etc) if you can have a look and see if you think i can make any minor improvements

:D

~George

Paul Komski
11-29-2008, 09:28 PM
I haven't scrutinised the code but the form seems to work so the proof of the pudding is in the eating I guess. Hope you are happy with the results of your hard work.

George Hallam
11-30-2008, 04:18 AM
:D ye i am and thank you,

but the program is only 60% of the project grade (which in turn is only 25% of the over all year grade) the other 40% is writing about what you have done, designing it and doing tests. That means for 3 pages of code i have to do 25-30 pages of writing :(

Paul Komski
11-30-2008, 04:22 AM
have to do 25-30 pages of writing
Don't be getting yourself an RSI (http://en.wikipedia.org/wiki/Repetitive_strain_injury)now!

;)

George Hallam
11-30-2008, 04:43 AM
haha what a good excuse not to do it :p