| | |
help with recursion
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Feb 2007
Posts: 20
Reputation:
Solved Threads: 0
can someone tell me what is wrong with this code? We are supposed to use recursion to see if the two arrays are equal.
C++ Syntax (Toggle Plain Text)
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; }
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:
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
leftis 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.
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
•
•
Join Date: Feb 2007
Posts: 20
Reputation:
Solved Threads: 0
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.
>I dont really understand what you mean by i need to index the arrays using left.
In other words:
Arrgh, those stupid [ left ] tags keep parsing out!
In other words:
C++ Syntax (Toggle Plain Text)
if (a[ left ] == b[ left ]) { // element matches } else { // element doesn't math; return false }
Arrgh, those stupid [ left ] tags keep parsing out!
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
•
•
Join Date: Feb 2007
Posts: 20
Reputation:
Solved Threads: 0
I actually kind of figured out what was wrong but i cant seem to figure out how to implement it.
C++ Syntax (Toggle Plain Text)
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; } }
Last edited by ammochck21; Feb 28th, 2007 at 2:05 am.
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.
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
![]() |
Similar Threads
- Recursion: when do you use it? (C++)
- C++ Beginner - #include recursion problem (C++)
- Recursion (C++)
- number formatting using recursion (Java)
- Need help with recursion and arrays (C++)
- powers of two, recursion. (C)
- Create menu from properties file by recursion (Java)
- Recursion (C)
Other Threads in the C++ Forum
- Previous Thread: help me?!
- Next Thread: quick question
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






