Try running through your code on paper, as though you were the computer. You'll see that you're not using the left variable for anything, except the +1 before recursing. And you'll recurse infinitely unless a and b are different. Which they almost always will be, unless you're comparing an array to itself. You need to index the arrays (using left) and check for bounding errors to make sure you don't go over the length of the arrays. ;)
Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
Why are you using 2 separate functions to perform the recursion?
Your comparison is likely to be what's causing the problem -- you can't just compare the starting memory addresses for 2 arrays to check if the contents are equal, you have to individually check each element.
A few other points:You need to make sure that the index value is not larger than the size of the array (I'm guessing that left is your index value). If it is, return immediately.
The method described only compares arrays of equal size. Hopefully this is what your teacher wants.
Recursion like this example isn't really going to be that useful. The problem here could have been solved with a loop, but regardless, you still have to do your assignments.
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
>I dont really understand what you mean by i need to index the arrays using left.
In other words:
if (a[ left ] == b[ left ]) {
// element matches
}
else {
// element doesn't math; return false
}
Arrgh, those stupid [ left ] tags keep parsing out!
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
i actually had a(left) == b(left) but when i originally posted it they went away sorry
Well, then it should just be a matter of implementing bounds-checking, and then your program should work, unless I'm totally missing something here...
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
You need to do your bounds-checking. If the last elements are equal, you return true, will which will transfer through all the calls of the function.
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
You're getting close, but you're still missing some things. For instance, if left > size, you don't return anything, which should be a compiler error. You did try to compile the code first, right? Like joeprogrammer suggested, you could use an extra condition to check if you just compared the last elements. Or, you could return true if left >= size, which would be just in an else block (fixes the above error as well, but someone could just call the function with the wrong left or size to get it to return true).
Other than that, it should be if(left < size) because of 0-based counting (the legal range of indices are 0 to size-1).
Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
> if (a(left) == b(left))
When did ( ) become the array subscript operator?
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
> if (a(left) == b(left))
When did ( ) become the array subscript operator?
when [ left] got parsed out ;)
Infarction
Posting Virtuoso
1,580 posts since May 2006
Reputation Points: 683
Solved Threads: 53
>because all of the arrays are the same size right?
That's what I asked you. I don't know the requirements of your assignment, but that seems the only logical thing to assume here.
>Where would i implement that?
You would implement the check after you know that the 2 elements are equal. If it's the last element, then you return true.
Also don't forget that the last element is actually size-1, to take in account the arrays starting at element 0.
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
You've almost got it -- but not quite. Ask yourself: what does the while() statement do?
What do I want to return when I find out that the last 2 elements are equal?
In other words, you need to nest 2 if() statements. What the heck, you've shown effort, I'll just show you what I mean:
if (a[ left ] == c[ left ])
{
if (left == size-1) // check if this is the last element
return true;
else
return Equal2(a, c, left + 1, size); // keep going
}
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
Some points:You figured out that you need to use substring - that's good. However, for simply grabbing the first character you should do this instead:
s1[0] == // etc.
This function has the same problem as before -- where does it end? You need to make sure that you don't call it again when s1.length() == 1.
You're essentially returning the count as soon as the character isn't a vowel. How useful is that? You should keep calling the function until the string has only 1 character left. THEN return the count.
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339