Merging Dynamically Allocated Arrays

I am having trouble with merging the two dynamically allocated arrays I have made according to my teacher's instructions. Can somebody please tell me how I can fix the MergeArray function? Also I have tried to deallocate the memory of the arrays at the end of the program to avoid a memory leak, but the function I have written seems to create a big runtime error, whch I don't understand. I have commented it out for now.




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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189

/* Lab5: practice using strut and dynamic arrays
 * Author: Luca Del Signore
 * Last modified on: October 10th, 2014
 * Known bug: N/A
 * Note: Please use assert to check for precondition
 */

#include <iostream>
#include <assert.h>
using namespace std;

// Define a struct type, each struct type variable has three
// member variables: array, capacity, and length. Togther these
// three variable represent a partially filled array.

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
		    // the array is filed from the 0th to the
		    // (length - 1)th element
};

/*
 Initialize the array structure with the given numbers.
 the array's capacity will be twice 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: a.length == numbers_len
                 a.array[0]==numbers[0],..., 
		 a.capacity = 2*numbers_len
 */
void InitArray (Array & a, 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 & a);

/*
 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 
 @postcondition: array1.length = array1.length+array2.length;
                 array1 contains its previous numbers, followed by numbers
		from array2 
 Note: if array1's capacity is not enough to hold all numbers, 
   you need to allocate a new int array, and copy old data over to the 
   new array... 
 */
void MergeArray (Array & a1, const Array & a2);



/*
 Deallocate the memory of both arrays after the end of the program.
 @param a1.array: first array to be deleted
 @param a2.array: second array to be deleted
 @precondition: a1.array and a2.array have been allocated
 @postcondition: both dynamic arrays are deallocated
 */
//void DeleteArrays (Array & a1, Array & a2);



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 = {NULL, 0, 0}; // Declare an Array object and initialize
				      // its member variables

	Array NumArray2 = {NULL, 0, 0}; // Declare another Array object and initialize
					// its member variables

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

	InitArray (NumArray1, numbers1, 5); // now initialize the two Array objects

	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);
	//DeleteArrays(NumArray1, NumArray2);

}


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

	a.length = numbers_len;
	a.capacity = (2 * numbers_len);	

	a.array = new int[a.capacity];

	for (int i = 0; i < a.capacity; i++)
	{
		a.array[i] = numbers[i];
 	}
	



}


void MergeArray (Array & a1, const Array & a2)
{
	int * IntPtr;
	int newCapacity;


	if (a1.length + a2.length > a1.capacity)
	// if a1.array is not big enough to hold all of the data
	{	
		a1.capacity = a1.length + a2.length + 10;
		

		newCapacity = a1.length + a2.length + 10;
		IntPtr = new int[a1.length + a2.length + 10];
		
		for (int i = 0; i < a1.length; i++)
			IntPtr[i] = a1.array[i];

		delete[] a1.array;

		*a1.array = *IntPtr;		

		a1.capacity = newCapacity;		

		// allocate a new dynamic int array that is big enough
		// so that you can make its size be a1.length+a2.length+10 
		
		// copy numbers from a1.array to this new array
		
		// free the current a1.array
		
		// make a1.array point to the new array's capacity

		// set a1.capacity to the new array's capacity
	}

	for (int i = a1.length; i < a1.length + a2.length; i++)
	{
		a1.array[i] = a2.array[i];			
	}

	a1.length = a1.length + a2.length;	
	// Todo: append a2.array's data into a1.array
	
	// Todo: update a1.length
}

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

/*
void DeleteArrays (Array & a1, Array & a2)
{
		delete[] a1.array;

		delete[] a2.array;
}
*/




This is the output I get:

***** NumArray1:
***** NumArray2:
***** NumArray1:
12
23
34
56
78
***** NumArray2:
89
7
14
22
98
101
112
34
11
5
99
***** After merge:
***** NumArray1:
12
0
34
56
78
101
112
34
11
5
99
0
24666176
0
22
11





Topic archived. No new replies allowed.