 |
| VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1). |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

April 30th, 2008, 01:48 PM
|
|
Authorized User
|
|
Join Date: Apr 2008
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Help with an array
I need to be able to display only the distinct elements of a dynamic array. For example, if the array consists of:
1,2,2,3,4,5,5,5,5,6,7,8,9,9,9,9
then the output in my text box should be:
1,2,3,4,5,6,7,8,9
I can not figure out the code for this. I know how to count the total number of distinct array elements, but I don't know how to display each distinct element. Can anyone help?
|
|

April 30th, 2008, 01:57 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
Hi there.. If you can count them, then you can also show them. Just store the number you are counting, every time you add 1 to your counter and you are done...
HTH
Gonzalo
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
|
|

April 30th, 2008, 02:24 PM
|
|
Authorized User
|
|
Join Date: Apr 2008
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi, thank you for the help. This is the code I have for my counting function:
Code:
Private Function countDistinct(ByVal a() As Integer)
Dim count, i As Integer
For i = LBound(a) To (UBound(a) - 1)
If a(i) = a((i) + 1) Then
count = count
Else
count = count + 1
End If
Next
Return (count + 1)
End Function
The answer to this seems really simple, but I've been staring at this for hours and can't figure it out. How do I store the numbers I am counting in a variable without actually adding the numbers together?
|
|

April 30th, 2008, 03:32 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
|
|
You are only going to return how many distinct values you have. Each time you find a distinct value, you incr. your count.
One way to do this, that is a bit memory wasteful, is to create an integer array that has data about your Array Under Test (AUT).
Each time you hit a distinct element value in the AUT, resize the Data Array (DA) to be large enough to have an element of the same nuber as the value of the element in the AUT, and increment that DA element's value.
So:
Code:
Dim i as integer
Dim DA() As Integer
ReDim DA(0)
For i = LBound(a) To UBound(a)
If UBound(DA) < a(i) Then ReDim Preserve DA(0 To a(i))
DA(a(i)) += 1
Next i
Having done this, you can travel over DA, and add the index value of any element that is non-zero:
Code:
For i = LBound(DA) to UBound(DA)
Code:
If DA(i) > 0 Then Form.TxtBox.Text = "," & i
Next i
This should get you started. Zero might give you troubles...
You can also use collections to do this. It is slower, but makes more efficient use of memory. Also, this code is basically VB6. There might be better methods in .NETâs array object(s).
|
|

April 30th, 2008, 08:17 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
Do you realize that your code will only work with an ordered array (and in ascending order)?.
HTH
Gonzalo
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
|
|

April 30th, 2008, 09:46 PM
|
|
Authorized User
|
|
Join Date: Apr 2008
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Yes, I have a function that orders the array in ascending order before I attempt to display the distinct elements. I missed that in the original code I posted.
|
|

April 30th, 2008, 10:11 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
Hey, Hold down your horses.. you are ordering the array before doing this???
Then why not make 2 arrays, 1 ordered and the other ordered too, but with no repetition and at the same time counting the items???
you are doing the work twice...
HTH
Gonzalo
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
|
|
 |