Need help merging two arrays

I need help merging two arrays together and displaying them together in the end. However, I do not know how to start the merge function. If someone could help me or guide me on how to program the function, I would greatly appreciate it!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95

#include <iostream>
#include <assert.h>

using namespace std;

struct Array {
	int* array; // point to the dynamically allocated array
	int capacity; // the capacity of the array
	int length; // the number of elements in the array
};

/* Initializes the array structure with the given numbers. The array's capacity can be set to double the length of numbers array.
	@param array: the array struct to be initialized
	@param numbers: the array of values to be stored in the array struct
	@param numbers_len: how many numbers are there in the array numbers
	precondition: "numbers" has been filled with "numbers_len" number of ints
	post condition: array.length == numbers_len
	                array.array[0] == numbers[0],...,
                   array. capacity = 2*numbers_len
*/

void InitArray (Array & array, int numbers[], int numbers_len);

/* Displays the content of an int array, both the array and the length of array will be passed as parameters to the function
	@param array: gives the array to be displayed
*/

void DisplayArray (const Array & array);

/* Merge the contents of two arrays into one
	@param array1: the target array
	@param array2: the source array
	@precondition: array1 and array2 have been set up
	@post condition: array1.length = array1
*/

void MergeArray (Array & array1, const Array & array2);

int main()
{
	int numbers1[5] = {12, 23, 34, 56, 78};
	int numbers2[11] = {89, 7, 14, 22, 98, 101, 112, 34, 11, 5, 99};

	Array NumArray1, NumArray2;

	InitArray (NumArray1, numbers1, 5);
	InitArray (NumArray2, numbers2, 11);

	cout << "***** NumArray1: \n";
	DisplayArray(NumArray1);

	cout << "***** NumArray2: \n";
	DisplayArray(NumArray2);

	MergeArray (NumArray1, NumArray2);

	cout << "***** After merge:\n";
	cout << "***** NumArray1:\n";
	DisplayArray(NumArray1);

}

void InitArray (Array & a, int numbers[], int numbers_len)
{

	a.length = numbers_len;
	a.capacity = 2*numbers_len;
	a.array = new int[numbers_len];
	for (int i=0; i < numbers_len; i++)
	{
		a.array[i] = numbers[i];
	}
}



void DisplayArray (const Array & a)
{
	for (int i=0; i < a.length; i++)
	{
		cout << a.array[i] << " ";
	}
	cout << endl;

}



void MergeArray (Array & array1, const Array & array2)
{


}
So if I understand correctly, you're going to append array2 to the end of array1?

(1) Using this method, you probably want to store the pre-merge length of array1 in some temp variable.
int temp = array1.length;

(1) You'd have to figure out the new value of "length" by adding the length variables of array1 and array2.

(2) From there you should consider using the realloc function like so:
array1.array = (int*)realloc( array1.array, sizeof(int) * array1.length );
http://www.cplusplus.com/reference/cstdlib/realloc/

(3) Then it's just copy all the contents of array2 to the newly resized array1, starting from index temp.
Last edited on
Topic archived. No new replies allowed.