943,640 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 10801
  • VB.NET RSS
Nov 7th, 2008
0

2D array to 1D array vice versa??

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
3Dees is offline Offline
23 posts
since Sep 2008
Nov 8th, 2008
0

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

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?
Reputation Points: 27
Solved Threads: 29
Posting Whiz
timothybard is offline Offline
317 posts
since Mar 2007
Nov 9th, 2008
0

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

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
3Dees is offline Offline
23 posts
since Sep 2008
Nov 9th, 2008
0

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

Reputation Points: 10
Solved Threads: 0
Newbie Poster
3Dees is offline Offline
23 posts
since Sep 2008
Nov 9th, 2008
1

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

Do you really need a 1d array. Why not just put the info into the listbox. i.e.
VB.NET Syntax (Toggle Plain Text)
  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.
Reputation Points: 84
Solved Threads: 58
Posting Pro in Training
waynespangler is offline Offline
461 posts
since Dec 2002
Nov 9th, 2008
0

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

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
3Dees is offline Offline
23 posts
since Sep 2008
Nov 10th, 2008
0

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

Hi again,
Using the above code how could i get the listbox to obtain the corresponding 2D array index.
Thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
3Dees is offline Offline
23 posts
since Sep 2008
Feb 13th, 2010
0
Re: 2D array to 1D array vice versa??
Can the same algorithm be used in C++?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nainakriti is offline Offline
1 posts
since Feb 2010
Feb 13th, 2010
0
Re: 2D array to 1D array vice versa??
>Can the same algorithm be used in C++?

Yes. If you have any question, start your own thread.

Thread closed.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in VB.NET Forum Timeline: how to read the text file to datagridview
Next Thread in VB.NET Forum Timeline: Datagrid in VB.net





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC