2D array to 1D array vice versa??

Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2008
Posts: 23
Reputation: 3Dees is an unknown quantity at this point 
Solved Threads: 0
3Dees 3Dees is offline Offline
Newbie Poster

2D array to 1D array vice versa??

 
0
  #1
Nov 7th, 2008
Hey,
i am trying to convert a 2D array to a 1D array and also the reverse. Not sure if i am doing it right so does anyone know how it can be done. below is the code i have for 2D to 1D but now how do i do the reverse.

Dim Index As Integer
For j = 1 To array.GetLength(0)
For i = 0 To array.GetLength(0) - 1
Index = (i * rowNum) + j
Next i
Next j
Return Index
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 306
Reputation: timothybard is an unknown quantity at this point 
Solved Threads: 26
timothybard's Avatar
timothybard timothybard is offline Offline
Posting Whiz

Re: 2D array to 1D array vice versa??

 
0
  #2
Nov 8th, 2008
I don't see how this makes a 2D array in to a 1D array. All it does is loop through all the 'cells' of the array and stores a number as Index. What, by the way, is rowNum? It is neither declared nor set in the provided code.

I'm guessing what you will want to do is create a 1D array and store the values of the 2D array into the 1D array.

As far as creating a 2D array from a 1D array, you will have to provide additional details about the 1D array. Are all the items for the first row stored first and then the items for the second row? How do we know where the end of each row is?
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 23
Reputation: 3Dees is an unknown quantity at this point 
Solved Threads: 0
3Dees 3Dees is offline Offline
Newbie Poster

Re: 2D array to 1D array vice versa??

 
0
  #3
Nov 9th, 2008
Thanks for the reply Timo,
yes i basically want to find the coresponding index for a (row,col) in a 1D representation of 2D.
For e.g i a 3x3 array the element in positon (1,1) is 4 in a 1D array. I need to write funtions which can convert from 2D to 1D and next ID back to 2D because the 2D will be dislayed in a listbox as 1D. When i update an element in the listbox it should update the corresponding element stored in the index position in the matrix.

it is a bit much to explain hope you dont get lost.
here is how i did some calculation:

(rowIndex*no_Of_col)+colIndex
so in a 3x3 position (1,1)
1*3+1=4
The thing i now need to write a algorithm to change the index from 2D to 1D and one to be the opposite.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 23
Reputation: 3Dees is an unknown quantity at this point 
Solved Threads: 0
3Dees 3Dees is offline Offline
Newbie Poster

Re: 2D array to 1D array vice versa??

 
0
  #4
Nov 9th, 2008
Reply With Quote Quick reply to this message  
Join Date: Dec 2002
Posts: 461
Reputation: waynespangler is on a distinguished road 
Solved Threads: 56
waynespangler waynespangler is offline Offline
Posting Pro in Training

Re: 2D array to 1D array vice versa??

 
1
  #5
Nov 9th, 2008
Do you really need a 1d array. Why not just put the info into the listbox. i.e.
  1. Public Class Form1
  2. Dim ary(4, 3)
  3.  
  4. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  5. For col = 0 To 3
  6. For row = 0 To 4
  7. ListBox1.Items.Add(ary(row, col))
  8. Next
  9. Next
  10. End Sub
  11.  
  12. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  13. Dim index As Integer = 0
  14. For col As Integer = 0 To 3
  15. For row As Integer = 0 To 4
  16. ary(row, col) = index
  17. index += 1
  18. Next
  19. Next
  20.  
  21. End Sub
  22. End Class
Last edited by waynespangler; Nov 9th, 2008 at 1:38 pm.
Wayne

It is hard to understand how a cemetery can raise its burial rates and blame it on the cost of living.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 23
Reputation: 3Dees is an unknown quantity at this point 
Solved Threads: 0
3Dees 3Dees is offline Offline
Newbie Poster

Re: 2D array to 1D array vice versa??

 
0
  #6
Nov 9th, 2008
Thanks you,
i will have a look at you method and see if i could use it in any way. But i do need to display the 2D in a listbox as 1D array of strings. i also need to update the data in the listbox so i will need a reverse of the method. The object is to get the index of an element so if i could write a method which use the index of an element in the listbox and find is corresponding index in the 2D array and vice versa.

sometimes i am not even sure if i understand it my self. just started vb 2 months ago
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 23
Reputation: 3Dees is an unknown quantity at this point 
Solved Threads: 0
3Dees 3Dees is offline Offline
Newbie Poster

Re: 2D array to 1D array vice versa??

 
0
  #7
Nov 10th, 2008
Hi again,
Using the above code how could i get the listbox to obtain the corresponding 2D array index.
Thanks
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC