recursive problem

This is a discussion on recursive problem within the C++ Programming forums, part of the General Programming Boards category; hi can anyone help me please i want to write a recursive function that will find the minimum value of ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    38

    Question recursive problem

    hi

    can anyone help me please
    i want to write a recursive function that will find the minimum value of integer array and return this value the function takes an integer array and the array size as arguments.
    i tried to solve it like this and it doesn't work so please tell me what is wrong?

    int min(int a[] , int s) {
    if ( s==1 )
    return a[0];
    else if( s>1){
    if(min(a, s-1) < a[s])
    return min(a, s-1);
    else return a[s];
    }
    }

  2. #2
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    Code:
    int min(int a[],int s){
            if(s==1)return a[0];               
            return min(a,s-1)<a[s-1]?min(a,s-1):a[s-1];
    }
    but keep in mind (if this isn't for an assignment) that using recursion is not the best way to do this, just a regular linear search would be better.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    38

    Smile Thanx

    thanku Salem and thanku beeege

    i appreciate your help
    and i did it
    thank god and thankyou it works

    bye my friends

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive Problem.
    By penance in forum C Programming
    Replies: 4
    Last Post: 07-07-2005, 11:16 AM
  2. recursive function problem
    By jk81 in forum C Programming
    Replies: 2
    Last Post: 10-25-2002, 07:02 AM
  3. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 06:18 AM
  4. Problem with a recursive function
    By fofone in forum C Programming
    Replies: 2
    Last Post: 03-07-2002, 08:21 AM
  5. Recursive problem...
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2002, 04:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21