|
-
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
-
June 19th, 2006, 06:49 AM
#2
Re: Logic error with recursion to find smallest in an array
Enjoy... 
Code:
int find_smallest(int array[])
{
static int progress_marker = 0;
static int smallest;
if(0 == progress_marker)
smallest= array[0];
if (progress_marker >= SIZE)
return smallest;
else
{
if(smallest > array[progress_marker] )
smallest = array[progress_marker];
progress_marker++;
find_smallest(array);
}
}
-
June 19th, 2006, 07:18 AM
#3
Re: Logic error with recursion to find smallest in an array
You should pass what you named progress_maker as parameter, otherwise it's not quite recursion. Make prototype as follows:
Code:
int find_smallest(int* arr, int length);
This way when you need to move to the tail of array you call
Code:
find_smallest(arr+1,length-1);
Though bisection will make stack usage fairer.
"Programs must be written for people to read, and only incidentally for machines to execute."
-
June 27th, 2006, 11:00 AM
#4
Re: Logic error with recursion to find smallest in an array
Hi.
I have tried the following recursive version and it works fine (as long as no two elements have the same value )
#include <stdio.h>
#define SIZE 6
void printarray(const int [],const int );
int recur_min(const int *array);
int main()
{
int array[SIZE],i,smallest;
printf("enter the elements of the array and press enter after entering each element\n");
for (i=0;i<=SIZE-1;i++)
scanf("%d",&array[i]);
array[SIZE]='\0';
printarray(array,SIZE);
smallest = recur_min(array);
printf("the smallest in the array is %d\n",smallest);
return 0;
}
void printarray(const int array[], const int size)
{ // Print array on standard output ----------------------------------------
int i;
printf("\n");
for (i=0;i<size; i++)
printf("%d\n",array[i]);
printf("\n");
}
int recur_min(const int *array)
{
static int smallest;
if ( *(array+2) != '\0')
{
smallest = recur_min(array+1);
if (*array < smallest) smallest = *array;
return smallest;
}
else
{
if(*array < *(array+1))
smallest = *array;
else
smallest = *(array+1);
return smallest;
}
}
However it generates a windows error which says it needs to close the non responsive program i.e. the exe file of the project
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.
Additionally, how do I format my messages so that the code part of the message appears in a neat indented format which is easier to read? Is there something in which I can wrap my code before submitting a message??
I would appreciate any insights from u guys.
Best Regards,
Aijaz Baig.
-
June 27th, 2006, 11:18 AM
#5
Re: Logic error with recursion to find smallest in an array
Did you read my comment? It's not quite recursion, so it may be rejected even if you happen to do it right your way.
Try debugging. Run your code step-by-step to figure if it does what you intended. Your code is too complex, there's very simple correct code.
"Programs must be written for people to read, and only incidentally for machines to execute."
-
June 27th, 2006, 11:19 AM
#6
Re: Logic error with recursion to find smallest in an array
An integer array should not be 'null terminated' because '\0' == 0. You would probably want to be able to have 0 values in your array. In addition, the use of the static variable is a littlefunny. It would probably cause a stack overflow if there was infinite recursion. You can format your code neatly using the [ code ] [ / code ] tags.
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
|