![]() |
|
#1
|
||||
|
||||
Logic error with recursion to find smallest in an arrayHuy.
I am using recursion to find the smallest number in a given array. To keep it simple I have fixed the array length at 6. Heres my code. (I am using Visual C++ version 6) CPP / C++ / C Code:
I doubt if the static variable is playing foul here..nonetheless id appreciate if some gurus would lend me a helping hand albeit virtually.. Last edited by LuciWiz : 19-Jun-2006 at 05:04.
Reason: Please insert your C++ code between [c++] & [/c++] tags
|
||||
|
#2
|
|||
|
|||
Re: Logic error with recursion to find smallest in an arrayI see a couple of things you'll need to change.
1) Each time you call the recursive function you are passing an array that is a little bit shorter then the last time you called. For example the first time you call it the entire array gets passed: {1,5,3,4,8,6} You then increment progress_marker and pass (arr+progress_marker), so the second call you are really passing this: {5,3,4,8,6} Now when you call find_smallest(arr+progress_marker) for the 3rd time,progress_marker=2 so {5,3,4,8,6} + 2 will actually send: {4,8,6} (Notice how 3 gets skipped) The next call sends {4,8,6} + 3 which actually jumps outside of your original array!! Change this line: find_smallest(arr+progress_marker); to find_smallest(arr+1) 2) You never actually set smallest to anything unless this line is true: CPP / C++ / C Code:
In side your else you need something like: CPP / C++ / C Code:
And you would then need to change smallest to be a static. Give those changes a try, and repost if you still have any issues. |
|
#3
|
||||
|
||||
Re: Logic error with recursion to find smallest in an arrayHi.
I have tried the following recursive version and it works fine (as long as no two elements have the same value ) CPP / C++ / C Code:
I am using VC++ version 6.0 as u guys already know. Can somebody explain why? Is it because of stack overflow? but isnt this program a lil too small for that.I would appreciate any insights from u guys. Best Regards, Aijaz Baig. Last edited by LuciWiz : 28-Jun-2006 at 00:31.
Reason: Please insert your C++ code between [c++] & [/c++] tags
|
|
#4
|
|||
|
|||
Re: Logic error with recursion to find smallest in an arrayWhen you declare an array and you are going to use the '\0' to terminate it, you have to make sure to add 1 more to the size of your array.
When you do: #define SIZE 6 int array[SIZE] You get an array that has been allocated 6 spaces (0-5) like this: array[0] array[1] array[2] array[3] array[4] array[5] When you then try and do array[SIZE]='\0', you are actually writing one past the end of your array, because SIZE is 6. So array[6]='\0'. Simply change int array[SIZE] to int array[SIZE+1] |
|
#5
|
|||
|
|||
Re: Logic error with recursion to find smallest in an arrayi did it using following program, it returns smallest number among the array elements.
CPP / C++ / C Code:
Regards, Raja.P Last edited by LuciWiz : 13-Jul-2006 at 09:16.
Reason: Please insert your C++ code between [c++] & [/c++] tags
|
Recent GIDBlog
Vista ?Widgets? on Windows XP by LocalTech
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Need help deleting the last element in the array | headphone69 | C++ Forum | 2 | 15-Mar-2006 19:31 |
| I'm New - ARRAY question! | NewBieFemAle | Java Forum | 3 | 07-Dec-2005 02:56 |
| How do you find the return address in an array of integers? | racermike1967 | C Programming Language | 2 | 07-Nov-2005 10:08 |
| template comiling problems - need expert debugger! | crq | C++ Forum | 1 | 01-Feb-2005 21:26 |
| Creating N string | gwk | C Programming Language | 3 | 20-Jul-2004 23:27 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The