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?
// 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.
}
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.
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.
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)}
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.
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?
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.
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?
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?
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!
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.
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.