Objective: write a completely inneficient recursion function that will copy two arrays and one that will compare two arrays.
I'm given an algorithm I have to use, hell the prof even told us what the first call should look like. The array data is read in from some file, it's all ints. Say my first array has 7 values before I reach an (we'll call it) array input termination character, sounds fancy enough, in the input file. I write a null as the next item in the array, unless the last item brought the num. of items to the max, which in this case is 50. Now here's the algorithm for copying that array into some other array (paraphrased): Your copy function should recursively copy half the range of the range given from the original array into the new array. An example of the first call for an array that contains 7 values would read something like this, copy(x, y, 0, 6).
Here's the rub, if you send the first and last indexes for what you want to copy, and you continue to divide in half you'll either come up with a continuous loop or you'll lose either the first or last index in the copy, depending on your stop case (the case where the function returns, doing nothing else first).
Here is what I have so far, to show you what I mean above about not being able to obtain the copy of the first or last values, as well as another idea that ends up with an infinite loop. Btw, I've been programming for much longer than I've been taking this b.s. course, so don't use kid gloves in answering this post please. ty
Idea 1:
void copy (int old[], int new[], int start, int end) {
if(start+1 == end)
new[start] = old[start]; // This way I'll never be able to copy the last
// member of old into new. If I did
// new[end] = start[end] I would be able to,
// but then I would never copy the first member
else {
copy(old, new, start, start+(end/2));
copy(old, new, start+(end/2), start);
}
}
I do realize that in Idea 1, if I send the first index to copy and one more than the last index to copy it will work as I want it to (ie. for an array with 7 items in it, I would do copy(x, y, 0, 7) and that would copy 0 - 6), but the class being as F#!@^*$ annoying as it is, the T.A. says, "No, don't do that." So I tried something else, but this doesn't work at all. I'm just throwing it out to stir ideas.
Idea 2:
It's exactly the same except instead of start+1 == end in the if statement, use start == end. It doesn't work, the second copy call is continuous.
Once I have the copy or compare function the other is easy, they use the same algorithm. Any help would be greatly appreciated. Thanks in advance
-e