954,198 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

help with recursion

can someone tell me what is wrong with this code? We are supposed to use recursion to see if the two arrays are equal.

int Equal(int a[], int b[], int left, int size)
{
 if (a == b)
   {
    return Equal(a, b, left + 1, size);
   }
 else
   return false;
}

int Equal2(int a[], int c[], int left, int size)
{
  if (a == c)
    {
     return Equal2(a, c, left + 1, size);
    }
  else
    return false;

}
ammochck21
Newbie Poster
20 posts since Feb 2007
Reputation Points: 10
Solved Threads: 0
 

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
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

I kind of see what you are saying- and when i ran through it, i did it right- which obviously means that i wrote the program wrong. I dont really understand what you mean by i need to index the arrays using left. I also added a while loop saying while its not at the end of the array but then i got a weird number for Equal 2.

ammochck21
Newbie Poster
20 posts since Feb 2007
Reputation Points: 10
Solved Threads: 0
 

Yes i would rather use a for loop for this too but we arent supposed to. the arrays are all of equal size and the first and third arrays are identical and the second one is one number off. I guess i dont understand how to transform a for loop into a recursive function.

ammochck21
Newbie Poster
20 posts since Feb 2007
Reputation Points: 10
Solved Threads: 0
 

>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
Team Colleague
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

ammochck21
Newbie Poster
20 posts since Feb 2007
Reputation Points: 10
Solved Threads: 0
 
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
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

I actually kind of figured out what was wrong but i cant seem to figure out how to implement it.

int Equal(int a[], int b[], int left, int size)
{
  if (left <= size)
   {
    if (a(left) == b(left))
     {
       return  Equal(a, b, left + 1, size);    //need to have a return true here or somewhere
     }
    else
      return false;
   }
}
ammochck21
Newbie Poster
20 posts since Feb 2007
Reputation Points: 10
Solved Threads: 0
 

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
Team Colleague
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
Team Colleague
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
 

Well the thing is the left will never be greater than the size because all of the arrays are the same size right? I feel like i am close but it seems like i am missing the return true code when they are all equal. It seems like as i have it now it just keeps going until the left equals the size then it checks if the last elements match but then doesnt do anything. i cant seem to figure out after it goes through the whole array comparing each element- after they are equal (because two of the arrays are) it doesnt return true. Where would i implement that? I am sorry if i seem to not be getting it but basically i dont like recursion :) and it confuses me. Thanks for everyones help!

ammochck21
Newbie Poster
20 posts since Feb 2007
Reputation Points: 10
Solved Threads: 0
 

>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
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

Ok well yes all of the arrays are the same size. we are only reading in 3 arrays and they are all of size 6. arrays a and c are equal and a and b are different. so heres the new code i have for this- but it doesnt seem to change whether the arrays are equal or not- it always gives me a Yes answer.

int Equal2(int a[], int c[], int left, int size)
{
  int x;
  while (left <= size-1)
   {
    if (a(left) == c(left))  //i know there are supposed to be brackets here
     {
      x= Equal2(a, c, left + 1, size);
      return true;
     }
    else
      return false;
   }
}


i guess i am not understanding- im so sorry!

ammochck21
Newbie Poster
20 posts since Feb 2007
Reputation Points: 10
Solved Threads: 0
 

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
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

OK well thanks so much- i guess i was close! Thank you so much! it works!

ammochck21
Newbie Poster
20 posts since Feb 2007
Reputation Points: 10
Solved Threads: 0
 

ok heres a new problem-

I have to do a vowelcount in a string that I pass in to my function. The function is take each letter in the string and check to see if thats a vowel- if so add it to the count- and call the function again. if its not a vowel then just move on the next letter. i am not to put this into an array or anything- but just leave it as a string. I know i need to use substrings- and recursion again. Here is the code i have- but i keep getting crazy errors messages saying i cant do the substring == 'a' and so on. ill post my code but im embarrassed at how bad it is. Can someone get me pointed in the right direction? I am guessing the substring thing is all wrong on account i have only used it once before. Thanks

int VowelCount(string s1)
{
  int count;
  if ((s1.substr(0,100) == 'A') || (s1.substr(0,100) == 'a') || (s1.substr(0,100) == 'E') || (s1.substr(0,100) == 'e') ||
      (s1.substr(0,100) == 'I') || (s1.substr(0,100) == 'i') || (s1.substr(0,100) == 'O') || (s1.substr(0,100) == 'o') ||
      (s1.substr(0,100) == 'U') || (s1.substr(0,100) == 'u'))
    {
     count++;
     return VowelCount(s1.substr( +1, 100));
    }
  return count;
}
ammochck21
Newbie Poster
20 posts since Feb 2007
Reputation Points: 10
Solved Threads: 0
 

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
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You