I have an assignment and Im trying to do it, but I need to figure out how to do the recursive funtions. I know how to do it with the loop but I want it with the recursive. The funtion is find the smallest value
int findsmallest(int list[], int size)
I have an assignment and Im trying to do it, but I need to figure out how to do the recursive funtions. I know how to do it with the loop but I want it with the recursive. The funtion is find the smallest value
int findsmallest(int list[], int size)
Lets say I want to have an exponential function. Meaning I'll take the first parameter (x) to the second parameter (y) power. Here is what it would look like recursively.
I just want to show you an example of recursion, but at the same time not give you the answer. The most fun is learning .Code:int power(int x, int y) { if (y == 0) return 1; else return x * power(x, y-1); }
Better yet, if that doesn't help, can you post the code of your looped method, and I'll help you understand how to translate to recursion.
Last edited by guyonasm; 01-22-2006 at 02:55 PM.
Thanks for your reply. I am having a hard time figuring out the recursive. I put the loop because you said can help me to figure out how to do it if i put the loop...so i can learn how to translate it.
Code:int find_smallest(int list[] ) { int i, small =list[0]; i = 1; while (i< list.length ) { if ( small > list[i] ) small = list[i]; list+=1; } return small; }
That code looks nice . I'm assuming you're actually using Java , but for the sake of this being a C++ forum, I'm going to take what you have, and explain it in terms of C++, as your list.length is something I've only seen done while coding in Java .
Now that I'm thinking about it, it might be easier just to show you a way.
There is no <arrayname>.length to determine the size of the array in C++, so it must be provided as a parameter, which is size, and curr, is the current position within the array. This function swaps the lowest value, into the first element of the array until it hits the end of the array, and then returns the first element.Code:int findsmall(int list[], int curr, int size) { if (size-1 == curr) return list[0]; else if(list[curr] < list[0]) { // swap values int temp = list[curr]; list[curr] = list[0]; list[0] = temp; } findsmall(list, curr+1, size); return list[0]; }
There are many other ways to go about it, and assuming you use Java, the third parameter isn't even needed.
Just a word of advice. You should never really need a loop inside of a recursive function because it defeats the point of recursion if the loop is doing the work. On the other hand if you can get a loop to make it work, it shouldnt be to hard to see how to change that into a recursive function.
C++ Rules!!!!
------------
Microsoft Visual Studio .NET Enterprise
Code:int findsmallest(int list[], int size) { if ( size == 1 ) return list[0]; else { int rest = findsmallest( list+1, size-1); return list[0] < rest ? list[0] : rest; } }
Salem, that is beautiful, because not only is it smaller and simpler, but you don't even modify the array as I do .Originally Posted by Salem