|
|
 |
|
|
|
|
|
|
|
|
Visual Basic 2008 9.0 .NET Examples and Ebook
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| It is possible to create an array of which the elements are references to other arrays. This is for instance useful when a collection of collections needs to be managed.
Next example declares a array cities with two elements of type Array. These elements can now hold a reference to any type of array. cities(0) holds a reference to a one-dimensional String array (1). cities(1) holds a reference to a one-dimensional Integer array (2). |
| Module Example
Sub Main()
Dim names() As String = {"Brussels", "Antwerp", "Ghent"}
Dim zipCodes() As Integer = {1000, 2000, 0}
Dim cities(1) As Array
cities(0) = names
cities(1) = zipCodes
Console.WriteLine(names(1))
Console.WriteLine(cities(0)(1))
cities(1)(2) = 9000
Console.WriteLine(zipCodes(2))
Console.ReadLine()
End Sub
End Module Download Broncode |
| Output : Antwerp
Antwerp
9000 |
| As you can see on lines (3) and (4) expressions names and cities(0) both evaluate to the reference of the array instance containing the names of the cities. So expressions names(1) and cities(0)(1) both address the second element of the same array. |
Exercise
| Task :
Make a program containing 3 arrays : - a one-dimensional String array containing names of students - a one-dimensional Integer array containing scores of students - a jagged array, with 2 elements, pointing to the previous two arrays
Try for exercise only to use the jagged array to address the names and scores of the students, so avoid using the first 2 arrays.
Make it possible to add students and their scores. The program calculates and stores the averages score for each student. Store this average score in an extra row or column of the scores-array. |
| Output : Student Count ? 2
Topic Count ? 2
Name Student 1 ? John
John Score Topic 1 ? 4
John Score Topic 2 ? 6
Name Student 2 ? Paul
Paul Score Topic 1 ? 6
Paul Score Topic 2 ? 8
Name : John
Score Topic 1 : 4
Score Topic 2 : 6
Average Score : 5
Name : Paul
Score Topic 1 : 6
Score Topic 2 : 8
Average Score : 7 |
| Module ExerciseSolution
Sub Main()
Console.Write("Student Count ? ")
Dim studentCount As Integer = Console.ReadLine()
Console.Write("Topic Count ? ")
Dim topicCount As Integer = Console.ReadLine()
Console.WriteLine()
Dim studentUpperbound As Integer = studentCount - 1
Dim names(studentUpperbound) As String
Dim topicUpperbound As Integer = topicCount
Dim scores(studentUpperbound, topicUpperbound) As Double
Dim students(1) As Array
Const namesIndex As Integer = 0, scoresIndex As Integer = 1
students(namesIndex) = names
students(scoresIndex) = scores
Dim studentIndex, topicIndex As Integer
For studentIndex = 0 To studentUpperbound
Console.Write("Name Student " & (studentIndex + 1) & " ? ")
students(namesIndex)(studentIndex) = Console.ReadLine()
For topicIndex = 0 To topicUpperbound - 1
Console.Write(students(namesIndex)(studentIndex) & _
" Score Topic " & (topicIndex + 1) & " ? ")
students(scoresIndex)(studentIndex, topicIndex) = _
Console.ReadLine()
Next
Console.WriteLine()
Next
For studentIndex = 0 To studentUpperbound
For topicIndex = 0 To topicUpperbound - 1
students(scoresIndex)(studentIndex, topicUpperbound) += _
students(scoresIndex)(studentIndex, topicIndex)
Next
students(scoresIndex)(studentIndex, topicUpperbound) /= _
topicUpperbound
Next
Console.WriteLine()
For studentIndex = 0 To studentUpperbound
Console.WriteLine("Name : " & students(namesIndex)(studentIndex))
For topicIndex = 0 To topicUpperbound - 1
Console.WriteLine("Score Topic " & (topicIndex + 1) & " : " & _
students(scoresIndex)(studentIndex, topicIndex))
Next
Console.WriteLine("Average Score : " & _
students(scoresIndex)(studentIndex, topicUpperbound))
Console.WriteLine()
Next
Console.ReadLine()
End Sub
End Module Download Broncode |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|