Thread: recursive find of the smallest member..

1. recursive find of the smallest member..

i wrote a function that finds recursively the smallest member in an int array
and it says that "min is being used without being defined"

i cant put a value into min
i need to get it from the process.

??
Code:
```#include <stdio.h>
int recFindMin (int array[] ,int index);

int main()
{
int t,array[3];

array[0]=2;
array[1]=1;
array[2]=3;
t=recFindMin(array,2);
printf("%d\n",t);

return 0;
}

int recFindMin (int array[] ,int index)
{
int min,local;
if (index==0)
{
return array[index];
}

local=recFindMin (array ,index-1);
if (array[index]>local)
{
min=local;
}
return min;
}//end func```

2. And exactly why do you need min at all?

More importantly, what is min when you have a
Code:
`array[index]<=local`
--
Mats

3. min is the total minimum
and local is the temporary

4. what is the problem in the function code?

5. Originally Posted by transgalactic2
what is the problem in the function code?
Did you not trace through your function for the case I described: What is the value of min when your second if-statement is false?

--
Mats

6. in the case you described
nothing changes
min changes only if there is a smaller number then it.
i added an empty else case.

i traced it
but from the start my array has only one cell of value 2
??
Code:
```#include <stdio.h>
int recFindMin (int array[] ,int index);

int main()
{
int t,array[3];

array[0]=2;
array[1]=1;
array[2]=3;
t=recFindMin(array,2);
printf("%d\n",t);

return 0;
}

int recFindMin (int array[] ,int index)
{
int min,local;
if (index==0)
{
return array[index];
}

local=recFindMin (array ,index-1);
if (array[index]>local)
{
min=local;
}
else
{
}
return min;
}//end func```

7. So, what value does min actually have in that situation? To put it another way, what value does a variable have that hasn't been set?

--
Mats

8. i think iteratively
min doesnt change
its stays with the same value from the previous call

9. Originally Posted by transgalactic2
i think iteratively
Start thinking recursively then

For example, suppose there is a "magic function" that gives you the minimum value in the rest of the array. To implement recFindMin to find the minimum, all you need to do is compare the current value with the value returned from that "magic function", returning the smaller one. Easy, isn't it?

Oh, and did I mention that the "magic function" is, coincidentally, recFindMin?

10. thanks i solved it

11. thats what i am doing i am comparing the index member with the rest of the members behind it
??

12. Originally Posted by transgalactic2
thats what i am doing i am comparing the index member with the rest of the members behind it
??
Looks like you posted on the wrong thread.

13. no i am on the correct thread
i am trying to fix this function

i have done the compare with the rest part
but something is missing
??
Code:
```#include <stdio.h>
int recFindMin (int array[] ,int index);

int main()
{
int t,array[3];

array[0]=2;
array[1]=1;
array[2]=3;
t=recFindMin(array,2);
printf("%d\n",t);

return 0;
}

int recFindMin (int array[] ,int index)
{
int min,local;
if (index==0)
{
return array[index];
}

local=recFindMin (array ,index-1);
if (array[index]>local)
{
min=local;
}
else
{
}
return min;
}//end func```

14. how to fix it

15. What is the initial value of local?.
First try to solve the problem yourself using a debugger and use the forum only as a last resort.

Popular pages Recent additions