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

Page 1 of 1

## 12 Replies - 17512 Views - Last Post: 16 May 2007 - 02:17 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=27964&amp;s=e4f0621dd892b8c996831665794840f5&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Sarge

Reputation: 0
• 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

• g+ + -o drink whiskey.cpp

Reputation: 250
• Posts: 13,507
• 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?

### #3 Sarge

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

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

Posted 12 May 2007 - 12:38 PM

Any help is graetly needed!!

### #4 Orann

Reputation: 0
• 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.

### #5 Sarge

Reputation: 0
• 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

### #6 Orann

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

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

Posted 13 May 2007 - 05:21 AM

Sarge, 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.

### #7 Sarge

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

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

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

### #8 Persistant

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

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

Posted 13 May 2007 - 08:50 AM

Sarge, 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

### #9 Jayman

• Student of Life

Reputation: 421
• 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.

### #10 Persistant

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

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

Posted 15 May 2007 - 03:14 PM

Persistant, on 13 May, 2007 - 08:50 AM, said:

Sarge, 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

### #11 William_Wilson

• lost in compilation

Reputation: 207
• Posts: 4,812
• 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.

### #12 Sarge

Reputation: 0
• 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,

### #13 Jayman

• Student of Life

Reputation: 421
• 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.