Splitting arrays

Hey
I'm trying to write a function on splitting an array. So far it doesn't seem to work and doesn't output the arrays properly. Note that I'm a complete beginner, so the code might look bad to you. But if you can give me any tips on how to improve it, I'd be thanksfull.

Anyways, here is the code:

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
int split (int a[])
{
	int x=1;
	int * list1;
	int * list2;
	list1 = new int [n/2];
	if (n % 2 == 0)
	{ 
		list2 = new int [n/2];
		for (int i=0;i<n/2;i++)
		{
			list2[i]=a[x+n/2];
			x++;
		}
	}
	else
	{
		list2 = new int [(n/2)+1];
		for (int i=1;i<n/2+1;i++)
		{
			x=0;
			list2[i]=a[x+n/2+1];
			x++;
		}
	}
	for (x=0;x<n/2;x++);
	{
		list1[x] = a[x];
	}
	for (x=0;x<n/2;x++);
	{
		cout << list1[x] << " " << list2[x] << endl;
	}
	return 0;

}

Last edited on
Problem still there...
Line 6 : variable n uninitialized

You also have to be careful if n is an odd number or 1
Well, simplyfy the whole thing:

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
#include <iostream>

void copy_list(int to[], int from[], int size)
{
	for(int i = 0; i < size; ++i)
	{
		to[i] = from[i];
	}
}
void print_list(int list[], int size)
{
	for(int i = 0; i < size; ++i)
	{
		std::cout << list[i];
 	}
}

int split (int a[], int n)
{
	int size1 = n / 2;
	int size2 = n - size1;
	int * list1 = new int[size1];
	int * list2 = new int[size2];

	copy_list(list1, a, size1);
	copy_list(list2, a + size1, size2);

	print_list(list1, size1);
	std::cout << " ";
	print_list(list2, size2);
	std::cout << std::endl;

	delete[] list2;
	delete[] list1;
	return 0;

}

int main()
{
	int l[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

	split(l, sizeof(l) / sizeof(*l));

}
Last edited on
Thank you a lot for your simplification. I hope you don't mind me asking few questions?

1. the copy_list function, how come you don't have to pass by reference? It automatically changes the values of the array you use as arguments (here list1, list2). I thought for it to do that it needs to be passed by reference?

2. When you call copy_list with list2, you use a + size1 as argument. I realize this is so the array starts at a[n/2], but you are essentially adding an integer to an array. How is that possible?

3. In your main function, you use sizeof(l) / sizeof(*l) as argument, what does *l mean? Why not just use sizeof(l)?

4. Why do you use std::cout instead of just cout? What does the std do? Or is this because you didn't put using namespace std;?


Again I thank you for your time.
Regards
1. If you have declared a 1 dimensional array the variable (here 'l') can be treated as the pointer to the first element in that array

2. Pointers are a kind of index to the memory. In fact they're simple numeric values and you can apply numeric calculation to them.

3.
- what does *l mean? It derefences 'l'. '*l' is of type 'int' instead of 'int *'.
- Why not just use sizeof(l)? That would result the number of bytes used for that array but I want the number of elements which is 9. The number of bytes is 36 (9 * 4 (if sizeof(int) is 4)).

4. With 'using namespace std;' you can indeed omit the 'std::' before the functions like cout. I don't do this because I want to avoid name conflicts (imagine there's alread a 'split()' function within the std namespace
1. But you are using list1 as argument, which is a dynamic array, not l? I still don't quite understand.

2. Ah yes, that's clear now. Pointers seem like a powerful but complex tool.

3. Aha! I thought sizeof(l) would give the actual number of elements. So l is an array, but a pointer as well, if I remember the tutorial correctly. Sizeof(l) would give me 36, and sizeof(*l) would give me 4, due to l pointing to l[0] and *l would be actually l[0]. That's an integer and the size of it would be 4. Did I understand correctly?

4. Thank you for clearing that up, the name conflict wouldn't have occurred to me.
'list1' is not a dynamic array. The only difference between l and list1 is just the location (stack / heap). 'copy_list()' and 'print_list()' expects a pointer to the first element of an array. That is true for l and list1/list2.
Ah ok. Now I understand. So that's only possible due to it being an array and a pointer? So if they were variables and not an array, you'd need to pass by reference to achieve the same effect? (effect as in the data in the variables being directly manipulated, with no return value)
Yes to manipulate the content you must pass an object as a pointer or reference.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void Foo(int *x)
{
  (*x) = 100; // Dereference so that the content and not the pointer becomes 100
}
void Bar(int &x) // Using reference
{
  x = 200; // No dereference necessary
}

...
{
  int x = 0;
  Foo(&x); // Now x is 100
  Bar(x); // Now x is 200
}
Note that manipulating the content is somewhat problematic since the caller might not expect it (esp. using reference since it looks like passing it by value)
Alright, it's clear to me now. Thank you for your time, I appreciate it.
Topic archived. No new replies allowed.