Recursion Funtion Problem

This is a discussion on Recursion Funtion Problem within the C++ Programming forums, part of the General Programming Boards category; I am unable to write a recursive function that takes in a vector of numbers and returns the median. double ...

  1. #1
    Unregistered
    Guest

    Recursion Funtion Problem

    I am unable to write a recursive function that takes in a vector of numbers and returns the median.
    double median (vector <double> vec)
    {
    typedef vector<double> :: size_type vec_sz;
    vec_sz size = vec.size();
    if (size==0)
    return false;
    sort(vec.begin(), vec.end());
    vec_sz mid = size/2;
    return size%2 ==0 ?(vec[mid]+vec[mid-1])/2 : vec[mid];
    }

    Could anyone convert the into a recursive function call .

    Thanking you in advance for your reply and time.

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    114
    I won't write your code, but I see there that you have missed the basics of refursive functions: The function calls itself. THis is how the basicas should look:

    Code:
    double median(vector <double> vec)
    {
        if(vec.size() != 0){
            return median(vec);
       }else{
            return 0;
       }
    }
    Now, before returning median(vec), do your median calculations...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple Recursion problem
    By wOo[FIN.K.L] in forum C Programming
    Replies: 8
    Last Post: 06-12-2008, 06:58 AM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  4. Problem of understanding recursion
    By ixing in forum C Programming
    Replies: 2
    Last Post: 05-02-2004, 04:52 PM
  5. recursion problem
    By dustinc in forum C++ Programming
    Replies: 1
    Last Post: 10-29-2001, 04:29 AM

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