Lid |
|
Ik heb een vraag betreffende een waarde te koppelen aan een index.
Hieronder staat de code maar mag niet veranderd worden.
Om de berekening te stellen zou ik een "do loop" gebruiken en een "if then else".
Ik zie alleen niet in hoe ik de index moet laten zoeken(optellen).
Er mag alleen tussen de blok zelf toegevoegt worden.
Kan iemand hierbij helpen?
'Vul onderstaande code aan met een binary search, aan de code die je meekrijgt mag je NIETS veranderen :
'Module GequoteerdeOefening03
' Sub Main()
' ' Configure source collection :
' ' 0 1 2 3 4 5 6 7 8 9
' Dim zipcodes() As String = {"1000", "Brussel", "2000", "Antwerpen", "3000", "Leuven", "8000", "Brugge", "9000", "Gent"}
' Dim zipcodesCount As Integer = 5
' '
' ' Assemble test searchvalues collection :
' Dim searchValues() As String = {"0500", "1000", "2000", "3000", "4000", "8000", "9000", "9500"}
' ' Search for every testvalue :
' For searchValueIndex = 0 To 7
' ' Set the searchvalue :
' Dim searchValue As String = searchValues(searchValueIndex)
' '
' ' Perform a binary search for searchvalue in zipcodes(), set variable found to true if searchvalue is found,
' ' when searchvalue is found make sure variable index is set to position of searchvalue in zipcodes() :
' Dim index As Integer
' Dim found As Boolean = False
' ...
' '
' ' Output :
' If found Then
' Console.WriteLine(searchValue & " is zipcode of " & zipcodes(index + 1))
' Else
' Console.WriteLine(searchValue & " not found")
' End If
' Next
' '
' Console.ReadLine()
' End Sub
' End Module
'Programmaverloop :
'0500 not found
'1000 is zipcode of Brussel
'2000 is zipcode of Antwerpen
'3000 is zipcode of Leuven
'4000 not found
'8000 is zipcode of Brugge
'9000 is zipcode of Gent
'9500 not found
'Opmerking :
'Ook Strings kan je met de operatoren <, >, <=, >=, = en <> vergelijken, zo zal de Boolean expressie "abc" < "def" evalueren naar True.
'Vul onderstaande code aan met een binary search, aan de code die je meekrijgt mag je NIETS veranderen : 'Module GequoteerdeOefening03 ' Sub Main() ' ' Configure source collection : ' ' 0 1 2 3 4 5 6 7 8 9 ' Dim zipcodes() As String = {"1000", "Brussel", "2000", "Antwerpen", "3000", "Leuven", "8000", "Brugge", "9000", "Gent"} ' Dim zipcodesCount As Integer = 5 ' ' ' ' Assemble test searchvalues collection : ' Dim searchValues() As String = {"0500", "1000", "2000", "3000", "4000", "8000", "9000", "9500"} ' ' Search for every testvalue : ' For searchValueIndex = 0 To 7 ' ' Set the searchvalue : ' Dim searchValue As String = searchValues(searchValueIndex) ' ' ' ' Perform a binary search for searchvalue in zipcodes(), set variable found to true if searchvalue is found, ' ' when searchvalue is found make sure variable index is set to position of searchvalue in zipcodes() : ' Dim index As Integer ' Dim found As Boolean = False ' ... ' ' ' ' Output : ' If found Then ' Console.WriteLine(searchValue & " is zipcode of " & zipcodes(index + 1)) ' Else ' Console.WriteLine(searchValue & " not found") ' End If ' Next ' ' ' Console.ReadLine() ' End Sub ' End Module 'Programmaverloop : '0500 not found '1000 is zipcode of Brussel '2000 is zipcode of Antwerpen '3000 is zipcode of Leuven '4000 not found '8000 is zipcode of Brugge '9000 is zipcode of Gent '9500 not found 'Opmerking : 'Ook Strings kan je met de operatoren <, >, <=, >=, = en <> vergelijken, zo zal de Boolean expressie "abc" < "def" evalueren naar True.
Koen schreef: Code tags ;)
|