Array Splitting

Nov 7, 2011 at 1:33am
closed account (365X92yv)
I've got a game I'm trying to code. What I have is 3 arrays that are filled with numbers. What happens is I get a set(or deck) of numbers and store them into a source array. Then what I'm trying to do is take that array and split it into two player arrays. I'm having a hard time trying to code a way to split the source array evenly between the two player arrays. I was thinking you could use a for loop to loop through the total number of numbers in the source array and go back and forth, one number in each array. Any information to help?
Nov 7, 2011 at 1:34am
closed account (365X92yv)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Split the elements of a source array into two destination arrays
// The given size is the size of the destination arrays (they are the same size)
//
// WRITE CODE TO COMPLETE THIS FUNCTION
//
void splitArray(int dest_one[], int dest_two[], int source[], int size)
{
	int ctr = 0; // Counter that will go through the source[] array.
    int i; // A counter

    // Loop through the destination arrays.
    // Assign each element in a destination array the next element
    // in the source array. Use the ctr variable to keep track of the
    // current position in the source array.

}
Nov 7, 2011 at 2:36am
closed account (D80DSL3A)
Use a for loop in the function, like this:
for(ctr=0; ctr<size; ++ctr)
Use integer division within this loop to determine which dest[] gets assigned the value. What happens to the value ctr%2 as ctr increases?

ctr/2 could be used to find which element in the dest[] to assign.
Nov 7, 2011 at 4:00am
closed account (365X92yv)
I'm not sure if I understand what it is you suggested. Could you elaborate more into what that would do? Just to clarify so I can get more input, this is what would be wished to happen.

source array[] = {1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13}

The numbers in the source array will be randomed though. What I want is to take those numbers in source array and split them evenly between these two arrays.

dest_one[] = { (10 random numbers from source array)}
dest_two[] = { (10 more random numbers from source array)}

What would I need to do to get that to work?
Nov 7, 2011 at 7:11am
closed account (D80DSL3A)
You could copy the 1st half of the source[] into dest_one[] and the 2nd half into dest_two[]. That's one way.
I was following your idea:
I was thinking you could use a for loop to loop through the total number of numbers in the source array and go back and forth, one number in each array.

This way the assignments would alternate between dest_one[] and dest_two[].
Note that the quantity ctr%2 will alternate value = 0,1,0,1... as the loop goes from ctr=0 to ctr = size-1. This allows you to alternate assignments:
1
2
3
4
if( ctr%2 == 0 )
   // make one assignment
else
    // make the other assignment 

I'm trying to avoid giving too much away as it's your homework problem.
See also my hint from the 1st post about what value for the array index to use for dest_one[] and dest_two[] when making the assignment.
Nov 8, 2011 at 4:20am
closed account (365X92yv)
This is what I'm trying and its not working. Before I had the for loop using ctr like you had it before and it would take the first number in the source array and just loop it. It would only have one memory spot for the destination arrays. Any help?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void splitArray(int dest_one[], int dest_two[], int source[], int size)
{
	int ctr = 0; // Counter that will go through the source[] array.
    int i; // A counter

    // Loop through the destination arrays.
    // Assign each element in a destination array the next element
    // in the source array. Use the ctr variable to keep track of the
    // current position in the source array.
	for(ctr=0; ctr<size; ctr++)
	{
		if((ctr % 2) == 0)
		{
			dest_one[ctr] = source[ctr];
		}
		else
		{
			dest_two[ctr] = source[ctr];
		}
	}
}



What I'm TRYING to get it to do is to use the counter variable as a way to use a certain spot in the array to fill a spot in the destination arrays. I'm really against the wall to getting this done. I've got this and an even bigger assignment due in my programming class and 2 papers, one english, one ethics, that still needs to be done. I'd really appreciate some serious help on this. I can guarantee you that any help will not just be to get work done. I'm a computer engineering major.
Last edited on Nov 8, 2011 at 4:29am
Nov 8, 2011 at 5:55am
Think about what indices in dest_one[] and dest_two[] you need to fill.
Are they the same indices as the one you're using to read from source[]?
Nov 8, 2011 at 3:08pm
closed account (365X92yv)
I figured it out. Took me forever but when you get something figured out its an awesome feeling. Now that I got the two destination arrays to be filled how would I get rid of the 0's and slide all the filled values to the left?

What the arrays look like after the for loop:

source[] = {1,2,3,4,5,6,7,8,9,10}

dest_one[] = {1,0,3,0,5,0,7,0,9,0}
dest_two[] = {0,2,0,4,0,6,0,8,0,10}

How would I shift the 0's to the end and have the numbers all the way to the left?
Nov 8, 2011 at 4:49pm
I think it would probably be easier and perhaps more efficient to iterate through the values and store the non-zero values in a new array than to try and shift the zeroes.

Since if you try to shift the zeroes you'll most likely need some kind of sorting algorithm that would have to iterate over the array a few times before actually accomplishing the task. That's only my opinion though.

Edit:

Oh wait you can't do that, I just noticed you're actually passing the arrays as parameters. So the destination arrays should be the same ones.

Perhaps while splitting the source array at the same time you can also save the indices with zero values and indices with non-zero values into 2 different integer arrays. Then afterwards you can easily swap the values?
Last edited on Nov 8, 2011 at 5:30pm
Nov 8, 2011 at 8:15pm
closed account (D80DSL3A)
Another simpler method would be to use a different value for the index in:
dest_one[ctr] = source[ctr]; and the other one too.
Use an index on the left side which grows half as fast as ctr does.
HINT: I gave the answer in my 1st post!
Nov 9, 2011 at 4:32am
closed account (365X92yv)
I can get the dest arrays to get rid of the zeroes but then in some places of the zeroes I get duplicated values. What would you suggest I do then? Also this project is basically the card game War. Once I get the two hands I've got code to compare them and decide which one is higher. What I need help with is writing the function that gets rid of the loser's card, shifts all the cards down one spot. If I can get the idea of shifting and moving values in arrays then I can easily get both functions done. What you've told me in the first post and everything else just doesn't make sense. I keep getting overwhelmed I just need to get this done.
Nov 9, 2011 at 6:29am
Use an index on the left side which grows half as fast as ctr does.


If you still can't get it, I think what he means is to do something like:

dest_one[ctr/2] = source[ctr];

or

dest_one[ctr >> 1] = source[ctr]; //same as dividing ctr by 2.

which is actually very efficient cause it automatically inserts the number at the correct index so there won't be any zeroes in between.

Nov 9, 2011 at 8:41am
closed account (D80DSL3A)
I'm sorry that my posts made little sense to you hardlinecoded.
@Dacster dest_one[ctr/2] = source[ctr]; is exactly what I was hinting at.

I think this should work well:
1
2
3
4
5
6
7
8
9
10
void splitArray(int dest_one[], int dest_two[], int source[], int size)
{
	for(int ctr=0; ctr<size; ctr++)
	{
		if((ctr % 2) == 0)		
			dest_one[ctr/2] = source[ctr];		
		else		
			dest_two[ctr/2] = source[ctr];		
	}
}

Each dest[] array will get every other value in source[].
Nov 9, 2011 at 2:29pm
closed account (365X92yv)
ctr/2 will work for both arrays but have errors when they both run. In dest_one[] the last value in the array is a garbage value, i.e -259292868.245e15. Also, in dest_two[] it doesn't pick up the first number that should be put in the array. I've got some other coding I need done so I'll be going up to campus today and getting some help at the tutoring center. I appreciate all the help up to date. Its been helpful. Sorry for not picking up the hint early.
Nov 9, 2011 at 5:22pm
closed account (365X92yv)
Its solved. Thank god.
Topic archived. No new replies allowed.