Hi all,
i just started learning C this month...
how do i write a recursive function that returns the index of the smallest element in an array ?
CAn someone please give me some directions pls...
THanks !!
Hi all,
i just started learning C this month...
how do i write a recursive function that returns the index of the smallest element in an array ?
CAn someone please give me some directions pls...
THanks !!
Let's see what you've done so far, and what you're having problems with. Read the sticky notes at the top of the forum while you're at it.
Quzah.
Hope is the first step on the road to disappointment.
attached my file ...
know where the logical error is...i am able to find the
smallest element but unable to find the index for the smallest
element....
If you have an array, and you're counting through it, then the index of whatever value you're currently looking at, is the counter's value.
Quick example:
Try using a variable to keep track of the index you're working on. Pass it as an argument, rather than always using array[0] and array[1]. Return that value when you find the smallest.Code:int array[5] = { 1, 4, 6, 8, 0 }; int x; for( x = 0; x < 5; x++ ) printf("array[%d] holds %d, the index is %d\n", x, array[x], x );
Quzah.
Hope is the first step on the road to disappointment.
is it possible not to use any loops ?
Sure. That was just a quick example.Originally posted by juzu
is it possible not to use any loops ?
I've probably given you too much. But basicly that's all you need for your problem.Code:void showarray( int size, int array[] ) { if( size > -1 ) { printf("array[%d] is %d\n", size, array[size] ); showarray( size-1, array ); } }
Quzah.
Hope is the first step on the road to disappointment.