|
-
June 19th, 2006, 04:58 AM
#1
Logic error with recursion to find smallest in an array
Huy.
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)
#include <stdio.h>
#define SIZE 6
int find_smallest(int array[]);
int main()
{
int arr[] = {1,5,3,4,8,6};
int smallest;
smallest = find_smallest(arr);
printf("the smallest number in the array is %d\n",smallest);
return 0;
}
int find_smallest(int array[])
{
static int progress_marker = 0;
register int smallest;
if (progress_marker >= SIZE-2)
{
if (array[SIZE-2] > array[SIZE-1])
smallest = array[SIZE-1];
else smallest = array[SIZE-2];
return smallest;
}
else
{
++progress_marker;
find_smallest(array+progress_marker);
}
}
I doubt if the static variable is playing foul here..nonetheless id appreciate if some gurus would lend me a helping hand albeit virtually..hehe
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|