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;

}

Recommended Answers

All 29 Replies

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. ;)

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.

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.

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.

>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!

i actually had a(left) == b(left) but when i originally posted it they went away sorry

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...

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;
   }
}

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.

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).

> if (a(left) == b(left))
When did ( ) become the array subscript operator?

when [ left] got parsed out ;)

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!

>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.

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!

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
     }

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

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;
}

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.
s1[0] == // etc.

wouldnt this mean the string is in an array?
Thanks for all your help! your great! I will try again. one more question- the error im getting has to do with the substring == a character. how do i make it work?

>wouldnt this mean the string is in an array?
Well, in a way, yes.

Look at the old C strings. They were basically an array of characters.

char *myString;
myString = "this is a string";
// myString[0] == 't'
// myString[1] == 'h'
// myString[2] == 'i'
// etc.

What the C++ strings are essentially trying to do is provide the same functionality that the old C strings have. So although a string object isn't actually a direct array like the C strings were, it's basically provided the equivalent features.

string myString;
myString = "this is a string";
// myString[0] == 't'
// exactly the same as before

>the error im getting has to do with the substring == a character.
Trash the substring idea and simply reference the first character like I used in my example. It will work, and will look much cleaner than all those .substr() calls.

[edit]A little bit of clarification: you don't actually need to remove the last substr() call that you use when doing the recursion.[/edit]

In case you're still wondering about the errors: there's 2 things you did wrong. substr() returns a string, not a char. So you should be using double quotes for comparison instead of single ones.

Secondly, the second parameter you're passing to substr is totally wrong. It represents the length of the string. I don't think you want a 100-character string. ;)

well i changed some of it- and actually when i changed it to double quotes it gave me even more errors so i changed it back but i did change the s1[0] and that worked. i know i have to change more things but the error i am getting now has to deal with the return vowelcount function. I know this stuff shouldnt be this hard but i am just not getting it. <sigh>

int VowelCount(string s1)
{
  int count;
  if (s1.length() ==1)
   {
    if ((s1[0] == 'A') || (s1[0] == 'a') || (s1[0] == 'E') || (s1[0] == 'e') || (s1[0] == 'I') || (s1[0] == 'i') || (s1[0] == 'O')
       || (s1[0] == 'o') || (s1[0] == 'U') || (s1[0] == 'u'))
    {
     count++;
     return VowelCount(s1[0]+ 1);
    }
   }
  else
   return count;
}

You can leave this line as a substr; it was only the other lines that needed changing:

return VowelCount(s1[0]+ 1);

Let me see if psedocode makes this problem any clearer to you:

integer function countVowels: string

    if this string is more than 1 character then
        call myself making string a char shorter (store result in 'count')
    end if

    if first letter of string contains a vowel
        increment 'count'
    end if

    return value in 'count'

end CountVowels

Do you see how this works? It keeps calling itself until it's reached the last character. Then it checks if it's a vowel, and continues working backwards, adding up the values. It should be trivial to convert this to C++.

You would think this would be trivial! I know you are probably getting frustrated but i really appreciate the help- here is what i have and its not letting me do the vowelcount(s1[0] +1).

heres the new code:

int VowelCount(string s1)
{
  int count=0;
  if (s1.length() > 1)
    {
    count= VowelCount(s1[0]+1);
    }
  if ((s1[0] == 'A') || (s1[0] == 'a') || (s1[0] == 'E') || (s1[0] == 'e') || (s1[0] == 'I') || (s1[0] == 'i') || (s1[0] == 'O') || (s1[0] == 'o') || (s1[0] == 'U') || (s1[0] == 'u'))
   {
    count++;
   }
   return count;
}

>You would think this would be trivial!
Sorry... Believe me though, you get used to it after a while.

>I know you are probably getting frustrated
No, not at all. If I were frustrated, I wouldn't be posting here. ;) It's my pleasure to help people like you who show effort and are actually trying.

>its not letting me do the vowelcount(s1[0] +1)
You actually need to use substr for this. Change it to something like this:

count= VowelCount(s1.substr(1));

The 1 takes off the first character, and since a second parameter isn't specified, the remaining characters are used.

commented: Such a great help! +1

ok well i did everything you said and its actually compiling!!! but its giving me a 0 for the answer so do i have something in the wrong place? also, because i have count =0, everytime i call it, does it set count to 0 everytime? and also, it looks like i am returning the count right after the first letteris read- and its not a vowel so is that why its giving me a 0?

Works fine for me. Could you post your current code, as well as the main() that you're calling it from?

>because i have count =0, everytime i call it, does it set count to 0 everytime?
It works backwards. So it actually accumulates as it returns backwards, and that's after the initialization statements.

nevermind- im an idiot- i wasnt calling it right. thank you so much for everything!!!! You are so awesome-and thanks for making me sort of figure it out :) you're great!

>thank you so much for everything
Anytime. Glad the problem's fixed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.