|
-
Jul 17th, 2006, 12:58 AM
#1
Thread Starter
Hyperactive Member
-
Jul 17th, 2006, 01:07 AM
#2
Re: Array problem.
Don't use multidimensional arrays for that. The Framework provides classes specifically for this type of thing. You would create a new Dictionary(Of Integer, Char) and then add the number/letter pairs to it. The idea of a Dictionary is that you specify a key and it returms the corresponding value.
VB Code:
Dim myDictionary As New Dictionary(Of Integer, String)
myDictionary.Add(-90, "A"c)
myDictionary.Add(-84, "C"c)
'...
myDictionary.Add(84, "Z"c)
MessageBox.Show(String.Concat(myDictionary(-8), _
myDictionary(-64), _
myDictionary(32), _
myDictionary(32), _
myDictionary(-90), _
myDictionary(-48), _
myDictionary(-64)) 'Displays "MESSAGE".
-
Jul 17th, 2006, 01:11 AM
#3
Thread Starter
Hyperactive Member
Re: Array problem.
Thanks. Let me give it a go.
BTW. Why did you change your Avatar.
-
Jul 17th, 2006, 01:29 AM
#4
Thread Starter
Hyperactive Member
-
Jul 17th, 2006, 01:30 AM
#5
Re: Array problem.
 Originally Posted by maps
Why did you change your Avatar.
Coz that baby panda is soooooooooo cute! He's cuter at full size but he's still cute at 64x64.
-
Jul 17th, 2006, 01:43 AM
#6
Thread Starter
Hyperactive Member
Re: [RESOLVED] Array problem.
By the way...if i choose a value between two numbers, i would like to get a letter corresponding to the number that starts the range. For example if i choose 2, i would like to bring up an answer "N" and if i choose -10, i would like to bring up an answer "M".
How do i do this?
-
Jul 17th, 2006, 02:49 AM
#7
Re: [RESOLVED] Array problem.
Gee, that's not a very important detail. There's nothing built into arrays or collections to do something like that. You probably should add items to the dictionary for all those intermediate values as well, so no matter what number you choose it will return a letter. The keys must be unique but the values don't have to be. The other alternative would be to just check every key to find the closest value if you don't get an exact match.
-
Jul 17th, 2006, 03:27 AM
#8
Thread Starter
Hyperactive Member
Re: [RESOLVED] Array problem.
JMC, thanks. For the first suggestion, as you see the dictionery would contain too much as you see the values span from -90 to +84. I would rather go for the alternative approach but still, it wouldnt give what i need since for a value of 7 will give me 8 since its the closest, yet for a value of 7 should give me N.
Any work around will be highly appreciated.
-
Jul 17th, 2006, 04:05 AM
#9
Re: [RESOLVED] Array problem.
 Originally Posted by maps
I would rather go for the alternative approach but still, it wouldnt give what i need since for a value of 7 will give me 8 since its the closest, yet for a value of 7 should give me N.
You what? You look for the closest key and then get the value that corresponds to that key. 8 is the closest key to 7 and the value that corresponds to 8 is N.
-
Jul 17th, 2006, 04:38 AM
#10
Thread Starter
Hyperactive Member
Re: [RESOLVED] Array problem.
8 is P.
And anything between 8 and 16 should also be P. Anyway, how do i go ahead to get the closest value in anycase.
-
Jul 17th, 2006, 04:44 AM
#11
Re: [RESOLVED] Array problem.
OK, think outside the square a bit. When I say the "closest value", it's up to you what that means. If you want the closest value that's less than the specified value then use that. This is your requirement so you set the rules.
-
Jul 17th, 2006, 04:51 AM
#12
Thread Starter
Hyperactive Member
Re: [RESOLVED] Array problem.
Ok.Thanks. How do i check every Key To get the closest match?
-
Jul 17th, 2006, 05:37 AM
#13
Re: [RESOLVED] Array problem.
You have been pointed in the right direction. Time to do some leg work yourself. Read the documentation for the Dictionary class and its member listing. Get an understanding of how it works and what members it has. Then you can answer that question yourself. Remember that square.
-
Jul 17th, 2006, 05:52 AM
#14
Thread Starter
Hyperactive Member
Re: [RESOLVED] Array problem.
Ha ha...it depends on what is IN the Square.
-
Jul 17th, 2006, 06:09 AM
#15
Thread Starter
Hyperactive Member
Re: [RESOLVED] Array problem.
Plzz Help. i cant see anything, i cant get going.
-
Jul 17th, 2006, 06:55 AM
#16
Re: [RESOLVED] Array problem.
You can't see anything? You can't see that the Dictionary class has a property named Keys that is described thusly: Gets a collection containing the keys in the Dictionary. If you can't see that then you haven't looked.
-
Jul 17th, 2006, 07:21 AM
#17
Thread Starter
Hyperactive Member
Re: [RESOLVED] Array problem.
i Have seen it. But how can i use it? How can i reference myDictionary here?
-
Jul 17th, 2006, 08:05 AM
#18
Re: [RESOLVED] Array problem.
Keys is just a collection. If you have a collection of numbers that doesn't contain 7 and you want to find the number closest to 7 that it does contain, what do you suppose you should do? The help topic for the Dictionary.Keys property even has a code example that uses a loop to get each key. Really...
-
Jul 17th, 2006, 08:48 AM
#19
Thread Starter
Hyperactive Member
Re: [RESOLVED] Array problem.
Thanks JMC, this is the furthest i have gone. I just dont know how to get a value close to a given value.
VB Code:
Dim strvalue As String = ""
Dim keyColl As Dictionary(Of Integer, String).KeyCollection = _
myDictionery.Keys
If myDictionery.TryGetValue(2, strvalue) Then
MsgBox("Value Found")
Else
MsgBox("Value Not Found")
For Each i As Integer In keyColl
MsgBox(i)
Next i
Exit Sub
End If
-
Jul 17th, 2006, 11:56 PM
#20
Thread Starter
Hyperactive Member
Re: [RESOLVED] Array problem.
I still need help. Anyone please.
I just need to know how to get the closest number contained in a collection that is closest to a specified number that is not in the collection.
-
Jul 18th, 2006, 12:24 AM
#21
Re: [NOT YET RESOLVED] Array problem.
I really think that you're trying to make this difficult when it's extremely easy. The keys are just a series of numbers. How would find the number in a series that was closest to a target number? You'd just make sure they were in numeric order and then check each one until you found the two that were on either side of the target. You can then choose either of those two, depending on whether you want the absolute closest, the closest without going over, the closest without going under, the closest in the direction of zero or whatever. You've got your series of numbers so just use a loop to test each one until you find the two the enclose the target. I suggested a Dictionary before I knew about this requirement. Given that you need to keys in order you should probably use a SortedList instead, which is otherwise almost the same a s a Dictionary. You could have sorted the keys from a Dictionary easily enough but if the collection will do it for you you might as well let it.
Can you tell me what the index of the number closest to but not greater than 15 is from the following series:
1,3,6,8,9,12,13,17,19,23
If you can then you know how to solve your current issue.
-
Jul 18th, 2006, 02:26 AM
#22
Thread Starter
Hyperactive Member
Re: [NOT YET RESOLVED] Array problem.
 Originally Posted by jmcilhinney
the index of the number closest to 15 from the following series:
1,3,6,8,9,12,13,17,19,23
If you can then you know how to solve your current issue.
JMC..now this is my problem.
This is what i have so far.
VB Code:
Private mySortedList As New SortedList(Of Integer, String)
mySortedList.Clear()
With mySortedList
.Add(-90, "A"c)
.Add(-84, "C"c)
.Add(-72, "D"c)
.Add(-64, "E"c)
.Add(-56, "F"c)
.Add(-48, "G"c)
.Add(-40, "H"c)
.Add(-32, "J"c)
.Add(-24, "K"c)
.Add(-16, "L"c)
.Add(-8, "M"c)
.Add(0, "N"c)
.Add(8, "P"c)
.Add(16, "Q"c)
.Add(24, "R"c)
.Add(32, "S"c)
.Add(40, "T"c)
.Add(48, "U"c)
.Add(56, "V"c)
.Add(64, "W"c)
.Add(72, "X"c)
.Add(84, "Z"c)
End With
For Each key As String In mySortedList.Keys
If mySortedList.Keys(mySortedList.IndexOfKey(key)) < 16 Then
'Show value "P" as the answer.
'mySortedList(key)
End If
Next
Last edited by maps; Jul 18th, 2006 at 02:35 AM.
-
Jul 18th, 2006, 03:02 AM
#23
Re: [NOT YET RESOLVED] Array problem.
*sighs heavily*
VB Code:
Dim mySortedList As New SortedList(Of Integer, Char)
With mySortedList
.Add(-90, "A"c)
.Add(-84, "C"c)
.Add(-72, "D"c)
.Add(-64, "E"c)
.Add(-56, "F"c)
.Add(-48, "G"c)
.Add(-40, "H"c)
.Add(-32, "J"c)
.Add(-24, "K"c)
.Add(-16, "L"c)
.Add(-8, "M"c)
.Add(0, "N"c)
.Add(8, "P"c)
.Add(16, "Q"c)
.Add(24, "R"c)
.Add(32, "S"c)
.Add(40, "T"c)
.Add(48, "U"c)
.Add(56, "V"c)
.Add(64, "W"c)
.Add(72, "X"c)
.Add(84, "Z"c)
End With
Dim targetKey As Integer = 44 'Find the key closest to but not greater than 44.
Dim lastKey As Integer
For Each key As Integer In mySortedList.Keys
If key > target Then
'This key is greater than the target so the previous key is the one we want.
Exit For
Else
'This key is still less than the target.
lastValue = key
End If
Next key
MessageBox.Show(String.Format("The key closest to but not greater than {0} is {1}.", _
target, _
lastValue))
MessageBox.Show(String.Format("The value that corresponds to {0}, and therefore also {1}, is {2}.", _
lastValue, _
target, _
mySortedList(lastValue)))
You could speed that up using a binary search but if straight iteration was such a chore let's not even go there.
Last edited by jmcilhinney; Jul 18th, 2006 at 03:08 AM.
-
Jul 18th, 2006, 03:13 AM
#24
Thread Starter
Hyperactive Member
Re: [NOT YET RESOLVED] Array problem.
Yah...i know that was a bitter pill for you. But Thanks A Million, you got some ignorance out of me.
Last edited by maps; Jul 18th, 2006 at 03:31 AM.
-
Jul 19th, 2006, 05:09 AM
#25
Thread Starter
Hyperactive Member
Re: [NOT YET RESOLVED] Array problem.
JMC....i have been going through how to implement a binarysearch. I came up with this code which uses a binarysearch from a one dimentional array.
i am just curious how we can use this kind of search over a sortedlist.
VB Code:
Dim myNumbers() As Integer = {0, 2, 4, 6, 8, 10, 12}
Array.Sort(myNumbers)
Dim i As Integer
Dim target As Integer = 7
Dim NextNo As Integer
For Each i In myNumbers
If i > target Then
NextNo = i
Exit For
Else
End If
Next i
MessageBox.Show(myNumbers.GetValue(Array.BinarySearch(myNumbers, NextNo)))
Last edited by maps; Jul 19th, 2006 at 07:23 AM.
-
Jul 19th, 2006, 07:11 AM
#26
Re: [RESOLVED] Array problem.
The Keys and Values properties are just collections themselves that can be treated in most cases as though they were 1D arrays, so basically you'd do it exactly the same way. Once you have the key you want you index the SortedList by that key to get the corresponding value.
-
Jul 19th, 2006, 08:24 AM
#27
Thread Starter
Hyperactive Member
Re: [RESOLVED] Array problem.
You mean like this! It doesnt work.
VB Code:
Dim mySortedList As New SortedList(Of Integer, Char)
With mySortedList
.Add(-90, "A"c)
.Add(-84, "C"c)
.Add(-72, "D"c)
.Add(-64, "E"c)
.Add(-56, "F"c)
.Add(-48, "G"c)
.Add(-40, "H"c)
.Add(-32, "J"c)
.Add(-24, "K"c)
.Add(-16, "L"c)
.Add(-8, "M"c)
.Add(0, "N"c)
.Add(8, "P"c)
.Add(16, "Q"c)
.Add(24, "R"c)
.Add(32, "S"c)
.Add(40, "T"c)
.Add(48, "U"c)
.Add(56, "V"c)
.Add(64, "W"c)
.Add(72, "X"c)
.Add(84, "Z"c)
End With
Dim targetKey As Integer = 44 'Find the key closest to but not greater than 44.
Dim lastKey As Integer
For Each key As Integer In mySortedList.Keys
If key > target Then
'This key is greater than the target so the previous key is the one we want.
Exit For
Else
'This key is still less than the target.
lastValue = key
End If
Next key
Messagebox.Show(mySortedList.Item(Array.BinarySearch(mySortedList.Keys,lastValue)))'<-----Unable to cast object of type KeyList to type system.array
-
Jul 19th, 2006, 08:36 AM
#28
Re: [RESOLVED] Array problem.
Ok, I didn't really read your previous post properly. It would stand to reason that you cannot use Array.BinarySearch to perform a binary search on something that is not an array. I just quickly read the words "implement a binarysearch" and I assumed that you had actually implemented a binary search yourself. If you want to implement a binary search you have two options. Firstly you could copy the contents of the Keys property to an array and then use Array.BinarySearch on that. After all this back and forth you obviously know how important it is to read the documentation, so I know that you have read about the CopyTo method of the Keys property. Your second option is to implement your own binary search. It's quite easy to do. You just keep cuttin the list in half. You start in the middle and then your new list becomes the half that contains the target. You then go to the middle of that list and your new list becomes the half of that that contains the target. You just keep cuttin the list in half until you either find the target or you end up with two items side by side that the target would fall between. Those would be the same last two items that you would have found the other way but generally you will get to them more quickly this way.
-
Jul 19th, 2006, 08:42 AM
#29
Thread Starter
Hyperactive Member
Re: [RESOLVED] Array problem.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|