Re: triangular array- how to transform twodimensional array into singledimesional array
- From: "Bukefal" <percec@xxxxxxxxxxxxxxxxx>
- Date: Mon, 9 Jul 2007 13:07:22 +0200
Steve, I'm very gratefull, this is amazing, new approach to my problem (I
have to say, a little above my level of reasoning and coding).
Now, this question 's torturing me:
In my application I have to display pairs one by one, and halfs of pair in
it's own label (to enable voting(clicking of the user to make choice):
Dim intCount As Integer
Private Sub CommandX_Click()
intCount = intCount + 1
If intCount = 1
Label1.Caption = first word of the 1stPair
Label2.Caption = second word of the 1stPair
End If
Etc etc. to the end of the list of pairs.
Is it possible to break list of pairs into the single pairs and also every
pair into the parts of it?
I was'nt able to do it from your fantastic code that makes full list of
pairs:
For k = 0 To NumPairs - 1
List2.AddItem mWords(mPairs(k, 0)) & " : " & mWords(mPairs(k, 1))
Next k
Please, can you add just 'a little something' upon this topic?
"Steve Gerrard" <mynamehere@xxxxxxxxxxx> wrote in message
news:DYCdnYfhNLAsqgzbnZ2dnUVZ_rOpnZ2d@xxxxxxxxxxxxxx
"Bukefal" <percec@xxxxxxxxxxxxxxxxx> wrote in message
news:f6qf1e$h1a$1@xxxxxxxxxxxxxxxxxxxxxx
This makes groups: (1,0) (2,0) (2,1) (3,0) (3,1) (3,2),
exactly what I need.
But something is wrong in my way of thinking, because I can't know (and
display) what is, for example, group 5, what pair make this group. I have
to display:
Instead of a matrix r x c, think of a list with two columns:
1, 0
2, 0
2, 1
3, 0
3, 1
3, 2
This is an array of the form (0 to NumPairs, 0 to 1), where the above
would be
pair(0,0) = 1 pair(0,1) = 0
pair(1,0) = 2 pair(1,1) = 0
pair(2,0) = 2 pair(2,1) = 1
pair(3,0) = 3 pair(3,1) = 0
pair(4,0) = 3 pair(4,1) = 1
pair(5,0) = 3 pair(5,1) = 2
- - - - -
Here is an example that might help.
Command1 builds the list of words, then loads them into List1.
Command2 builds the array of pairs, similiar to above. It loads them into
List2 by using the stored indexes to retrieve the words from the first
array.
The number of pairs is calculated using
NumPairs = (WordCnt - 1) / 2 * WordCnt
which is more or less that old trick of Gauss for adding up consecutive
numbers, in this case from 0 to 99.
The nested loops for building the pair list takes each word, and pairs it
with each word that comes before it in the list. This gets all the pairs
once, without repeats.
The line
List2.AddItem mWords(mPairs(k, 0)) & " : " & mWords(mPairs(k, 1))
gets the two indexes from pair k, and uses them to get the two words from
the word list.
' - - - - -
Option Explicit
Private mWords() As String
Private WordCnt As Long
Private mPairs() As Long
' - - - - -
Private Sub Command1_Click()
Dim n As Long
WordCnt = 100
' build word list
ReDim mWords(0 To WordCnt - 1)
For n = 0 To WordCnt - 1
mWords(n) = "Word#" & Format(n, "00")
Next n
'display words in List1
List1.Clear
For n = 0 To WordCnt - 1
List1.AddItem mWords(n)
Next n
Me.Caption = "List 1: " & List1.ListCount
End Sub
' - - - - -
Private Sub Command2_Click()
Dim NumPairs As Long
Dim n As Long
Dim j As Long
Dim k As Long
' calc num of pairs
NumPairs = (WordCnt - 1) / 2 * WordCnt
ReDim mPairs(0 To NumPairs - 1, 0 To 1)
' build pair list
k = 0
For n = 0 To WordCnt - 1
For j = 0 To n - 1
mPairs(k, 0) = n
mPairs(k, 1) = j
k = k + 1
Next j
Next n
' display pairs in List2
List2.Clear
For k = 0 To NumPairs - 1
List2.AddItem mWords(mPairs(k, 0)) & " : " & mWords(mPairs(k, 1))
Next k
Me.Caption = "List 2: " & List2.ListCount
End Sub
.
- Follow-Ups:
- References:
- triangular array- how to transform twodimensional array into singledimesional array
- From: Bukefal
- Re: triangular array- how to transform twodimensional array into singledimesional array
- From: Stan Weiss
- Re: triangular array- how to transform twodimensional array into singledimesional array
- From: Bukefal
- Re: triangular array- how to transform twodimensional array into singledimesional array
- From: Steve Gerrard
- triangular array- how to transform twodimensional array into singledimesional array
- Prev by Date: Data Source Problem regarding MYSQL
- Next by Date: Re: triangular array- how to transform twodimensional array into singledimesional array
- Previous by thread: Re: triangular array- how to transform twodimensional array into singledimesional array
- Next by thread: Re: triangular array- how to transform twodimensional array into singledimesional array
- Index(es):
Relevant Pages
|
Loading