Splitting arrays randomly

My question is how should I manipulate this function to randomly split the array randomNumbers [] that it is taking in. array1 and array2 are the resulting arrays from the split. Note I have done my research on this topic and I am still confused.

void split_up_array(int randomNumbers [], int arraySize, int *& array1, int *& array2, int & size1, int & size2)

{

// Split up array
array1 = new int[size1];
array2 = new int[size2];

for (int count = 0; count < arraySize; count++)
{
array1[count] = randomNumbers[count];
array2[count] = randomNumbers[count];

}

It looks like array1, array2, size1 and size2 are all output parameters. That means you must compute size1 and size2.

Off hand, and not too efficient, I'd do this:
- make a copy of randomNumbers.
- shuffle the copy
- Create array1 and array2 to be half the size of randomNumbers.
- copy the first size1 numbers from the shuffled copy to array1
- copy the rest of the numbers from the shuffled copy to array2

Just wondering what do mean by shuffle? I have updated my code:

void split_up_array(int randomNumbers [], int arraySize, int *& array1, int *& array2, int & size1, int & size2)
{

// Split up array
array1 = new int[arraySize / 2];
array2 = new int[arraySize / 2];

for (int count = 0; count < arraySize; count++)
{
randomNumbers[count];

}
Last edited on
assuming you want both arrays to be "legal".. that is, not size 0, ...

get a number from 1 to the size of the array -1. That is, if the orig array is size 10, you want from 1 to 9. That is rand %9 +1, and 9 is size -1, right?

now copy the orig array into the slice.. say you get 3. so for 0,1,2 copy into a new array that has a size of 3.

now copy the rest. starting at position 3 in the original array, copy 7 more into array 2, size of 7.

that is, if the array is
{1,2,3,4,5,6,7,8,9,10}
you will get
{1,2,3}
and
{4,5,6,7,8,9,10}


Thank you I think I figured it now.
Topic archived. No new replies allowed.