Thread: Noob Question

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    3

    Noob Question

    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 !!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    3
    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....

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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:
    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 );
    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.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    3
    is it possible not to use any loops ?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by juzu
    is it possible not to use any loops ?
    Sure. That was just a quick example.
    Code:
    void showarray( int size, int array[] )
    {
        if( size > -1 )
        {
            printf("array[%d] is %d\n", size, array[size] );
            showarray( size-1, array );
        }
    }
    I've probably given you too much. But basicly that's all you need for your problem.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quick noob question
    By thanatos1 in forum C# Programming
    Replies: 2
    Last Post: 06-17-2009, 08:28 PM
  2. another noob question
    By clb2003 in forum C Programming
    Replies: 4
    Last Post: 02-12-2009, 01:28 PM
  3. Noob printf question
    By lolguy in forum C Programming
    Replies: 3
    Last Post: 12-14-2008, 08:08 PM
  4. Very noob question :(.
    By GamerProduction in forum Tech Board
    Replies: 4
    Last Post: 04-14-2007, 05:40 AM
  5. Noob question ( little dos program )
    By Demon1s in forum C++ Programming
    Replies: 13
    Last Post: 04-04-2003, 09:28 PM