12 Replies - 14464 Views - Last Post: 16 May 2007 - 02:17 PM Rate Topic: -----

#1 Sarge  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 06-May 07

Need Help! Merging Two 1D Arrays into One 1D Array

Posted 12 May 2007 - 07:48 AM

If I have 2 1D Arrays:

Dim intX() As Integer = {1, 3, 5, 7, 9, 11, 13, 15, 17}
Dim intY() As Integer = {2, 4, 6, 8, 9, 10, 12, 14, 16}
Dim intZ(17) As Long

and need to merge array intX with array intY to form a new array intZ, is this possible?????

Any Help would be appreciated, Thanx In Adavance!
Is This A Good Question/Topic? 0
  • +

Replies To: Need Help! Merging Two 1D Arrays into One 1D Array

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Need Help! Merging Two 1D Arrays into One 1D Array

Posted 12 May 2007 - 12:22 PM

There are a few ways to do it, mostly having to do with copying elements from both arrays into the third array, then sorting..are you using VB6 or VB.NET?
Was This Post Helpful? 0
  • +
  • -

#3 Sarge  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 06-May 07

Re: Need Help! Merging Two 1D Arrays into One 1D Array

Posted 12 May 2007 - 12:38 PM

Thanx for replying Amadeus, I am working with Visual Studio.Net 2003.

Any help is graetly needed!!
Was This Post Helpful? 0
  • +
  • -

#4 Orann  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-May 07

Re: Need Help! Merging Two 1D Arrays into One 1D Array

Posted 13 May 2007 - 03:49 AM

I guess it depends on what you actually want to happen.
You you want intZ to simply be all the values of intX followed by the values of intY (Which needs to be 18 in length by the way) you could do something like the following:


Dim intX() As Integer = {1, 3, 5, 7, 9, 11, 13, 15, 17}
		Dim intY() As Integer = {2, 4, 6, 8, 9, 10, 12, 14, 16}
		Dim intZ(17) As Long
		Dim count As Integer

		'for every value in Array "intX" create
		'an indentical value in Array "IntZ"
		For count = 0 To UBound(intX, 1)
			intZ(count) = intX(count)
		Next count

		'For Every value in Array "IntY" Create
		'an identical value in Array "intZ"
		'after all the values from IntX
		For count = 0 To UBound(intY, 1)
			intZ((count + UBound(intX, 1))) = intY(count)
		Next count



If this is not what you are after (What did you mean by "merge"?) then let me know and I'll see if I can help.
Was This Post Helpful? 0
  • +
  • -

#5 Sarge  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 06-May 07

Re: Need Help! Merging Two 1D Arrays into One 1D Array

Posted 13 May 2007 - 04:30 AM

Orann, thanx for the reply, what I am looking for is for:

Dim intX() As Integer = {1, 3, 5, 7, 9, 11, 13, 15, 17}
Dim intY() As Integer = {2, 4, 6, 8, 9, 10, 12, 14, 16, 18}
Dim intZ(19) As Long

intX and intY merge into intZ as follows with this output into massagebox:

intZ = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18

Any Ideas Is Greatly Appreciated
Was This Post Helpful? 0
  • +
  • -

#6 Orann  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-May 07

Re: Need Help! Merging Two 1D Arrays into One 1D Array

Posted 13 May 2007 - 05:21 AM

View PostSarge, on 13 May, 2007 - 04:30 AM, said:

Orann, thanx for the reply, what I am looking for is for:

Dim intX() As Integer = {1, 3, 5, 7, 9, 11, 13, 15, 17}
Dim intY() As Integer = {2, 4, 6, 8, 9, 10, 12, 14, 16, 18}
Dim intZ(19) As Long

intX and intY merge into intZ as follows with this output into massagebox:

intZ = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18

Any Ideas Is Greatly Appreciated


No problem. I was playing around with this code for a bit and ended up going in circles. I'll come back and try and help out again, but for now I'll leave you with what I got to go clear my head.

Dim intX() As Integer = {1, 3, 5, 7, 9, 11, 13, 15, 17}
		Dim intY() As Integer = {2, 4, 6, 8, 9, 10, 12, 14, 16, 18}
		Dim intZ(17) As Long
		Dim det As Boolean
		Dim count As Integer

		det = False
		For count = 0 To UBound(intZ, 1)
			'det true means the next item to be loaded is from inty
			If det = False Then
				If intZ(count) < 1 = False Then
					If intX(count) = intZ(count - 1) Then
					Else
						intZ(count) = intX(count)
						det = True

					End If
				Else
					intZ(count) = intX(count)
					det = True
				End If

			Else
				If intY(count) = intZ(count - 1) Then
				Else
					intZ(count) = intY(count)
					count = count + 1
					det = False
				End If
			End If

		Next count



Note; this does not work as it is, but it might be something to go off.
Was This Post Helpful? 0
  • +
  • -

#7 Sarge  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 06-May 07

Re: Need Help! Merging Two 1D Arrays into One 1D Array

Post icon  Posted 13 May 2007 - 07:53 AM

Here is what I am wanting to do merge intX and intY into intZ and show the result in a messagebox:


Dim intX() As String = {4, 9, 12, 15, 22, 33, 44, 66, 72, 84, 87, 92, 96, 98, 99}
Dim intY() As String = {6, 8, 12, 16, 24, 31, 68, 71, 73, 74, 81, 93, 94}
Dim intZ(27) As String

I am wanting to merge the two arrays intX and intY into intZ and place the output to a messagebox that has the two merge arrays as so:

4, 6, 8, 9, 12, 12, 15, 16, 22, 24, 31, 33, 44, 66...................etc.

so intX has 15 elements, intY has 13 element
Was This Post Helpful? 0
  • +
  • -

#8 Persistant  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 06-April 07

Re: Need Help! Merging Two 1D Arrays into One 1D Array

Posted 13 May 2007 - 08:50 AM

View PostSarge, on 13 May, 2007 - 07:53 AM, said:

Here is what I am wanting to do merge intX and intY into intZ and show the result in a messagebox:


Dim intX() As String = {4, 9, 12, 15, 22, 33, 44, 66, 72, 84, 87, 92, 96, 98, 99}
Dim intY() As String = {6, 8, 12, 16, 24, 31, 68, 71, 73, 74, 81, 93, 94}
Dim intZ(27) As String

I am wanting to merge the two arrays intX and intY into intZ and place the output to a messagebox that has the two merge arrays as so:

4, 6, 8, 9, 12, 12, 15, 16, 22, 24, 31, 33, 44, 66...................etc.

so intX has 15 elements, intY has 13 element


Checkout this link, I think it may be what you are wanting to do.
http://www.freevbcod...ode.asp?ID=2874
Was This Post Helpful? 0
  • +
  • -

#9 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Need Help! Merging Two 1D Arrays into One 1D Array

Posted 13 May 2007 - 10:05 AM

Topics merged for continuity.
Was This Post Helpful? 0
  • +
  • -

#10 Persistant  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 06-April 07

Re: Need Help! Merging Two 1D Arrays into One 1D Array

Posted 15 May 2007 - 03:14 PM

View PostPersistant, on 13 May, 2007 - 08:50 AM, said:

View PostSarge, on 13 May, 2007 - 07:53 AM, said:

Here is what I am wanting to do merge intX and intY into intZ and show the result in a messagebox:


Dim intX() As String = {4, 9, 12, 15, 22, 33, 44, 66, 72, 84, 87, 92, 96, 98, 99}
Dim intY() As String = {6, 8, 12, 16, 24, 31, 68, 71, 73, 74, 81, 93, 94}
Dim intZ(27) As String

I am wanting to merge the two arrays intX and intY into intZ and place the output to a messagebox that has the two merge arrays as so:

4, 6, 8, 9, 12, 12, 15, 16, 22, 24, 31, 33, 44, 66...................etc.

so intX has 15 elements, intY has 13 element


Checkout this link, I think it may be what you are wanting to do.
http://www.freevbcod...ode.asp?ID=2874


Try BrainMass.com they say they will not do your homework but will help you.
Was This Post Helpful? 0
  • +
  • -

#11 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: Need Help! Merging Two 1D Arrays into One 1D Array

Posted 15 May 2007 - 10:40 PM

if the 2 arrays are already sorted, created a merged array is a simple task, needing a simple loop and 3 integer variables.
-intZ should be an array who's length is the sum of intX and intY's length.
-create the 3 ints (i,j,k) with an initial value of 0.
-have a while loop which runs the length of the shortest array
-compare intX[i] < intY[j]
-if it's true, then intZ[k] = intX[i]; ++i, else intZ[k] = intY[j]; ++j, and for both cases ++k
-once you reach the end of one of the arrays, simply copy the rest of the elements from the other on to the end of intZ, starting at the k position.

This is pseudo code, but, should be clear enough if you understand loops and arrays well enough.
Was This Post Helpful? 0
  • +
  • -

#12 Sarge  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 06-May 07

Re: Need Help! Merging Two 1D Arrays into One 1D Array

Posted 16 May 2007 - 10:23 AM

William,

Thanx for the reply, I dont understand the while loops, here is what I have so far
 Dim intX() As Integer = {4, 9, 12, 15, 22, 33, 44, 66, 72, 84, 87, 92, 96, 98, 99}
	Dim intY() As Integer = {6, 8, 12, 16, 24, 31, 68, 71, 73, 74, 81, 93, 94}
	Dim intZ(27) As Integer






	Private Sub btnMerge_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMerge.Click

		Dim I As Integer

		For I = 0 To 14

			intZ(I) = intX(I)

		Next

		For I = 0 To 12

			intZ(I + 12) = intY(I)

			txtBox1.Text = txtBox1.Text & intZ(I) & intZ(I + 12)

		Next

	End Sub



this puts the two arrays into:


The output in the textbox is supposed to be:

4, 6, 8, 9, 12, 12, 15, 16, 22, 24, 31, 33, 44, 66, 68, 71, 72, 73, 74, 81, 84, 87, 92, 93, 94, 96, 98, 99

With the code listed above this is what the output is:

4, 6, 9, 8, 12, 12, 15, 16, 22, 24, 33, 31, 44, 68, 66, 71, 72, 73, 84, 74, 87, 81, 92, 93, 6, 94

As you can see there is a small problem, I would appreciate another set of eyes on it to help me see the mistake, ALSO!!!!! I know that it would be easier to use copy.array and sort it but, I cannot use any Array Methods with this. I will also upload the .zip with the project in it if you want to see how it acts.

Any Help Would Be Appreciated,
Was This Post Helpful? 0
  • +
  • -

#13 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Need Help! Merging Two 1D Arrays into One 1D Array

Posted 16 May 2007 - 02:17 PM

You will need to implement your own sort method and there are many different types.

You can find out about the different sorting algorithms at the following link.
Sorting Algorithms.

The code is in java, but converting it to VB.NET is not that difficult.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1