merge function

Hi guys,

I wrote this merge function for two sub-arrays of equal size(so the resulting array size is an even number). Although it works if size(of resulting array)<=6 but for any bigger number it crashes...

More specifically if I have:

int c[]={1,3,5,6};
int b[]={2,7,9,11};
int a[]={0,0,0,0,0,0,0,0};

merge(a,b,c,8)

it works...

If however I have:

int c[]={1,3,5,6};
int b[]={2,7,9,11};
int* a=new int(8);
merge(a,b,c,8);

it crashes (unless the size is <=6)...


Any ideas?
Thanks
P

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
   void merge(int* a, int* b, int* c, int size)
{
	int k(0),i(0),j(0);
	while(k<size)
	{
		if(i>=size/2)
		{
			a[k]=c[j];
			k++;
			j++;
		}
		else if (j>=size/2)
		{
			a[k]=b[i];
			k++;
			i++;
		}
		else
		{
			if(b[i]<=c[j])
			{
				a[k]=b[i];
				k++;
				i++;
			}
			if (c[j]<b[i])
			{
				a[k]=c[j];
				k++;
				j++;
			}
		}
	}
}
Last edited on
int* a=new int(8); means have a point to an integer with the value 8.

int* a=new int[8]; means have a point to an integer array of size 8.
Oh man some times it's the most stupid thing! Thanks a lot!
Topic archived. No new replies allowed.