need an example

This is a discussion on need an example within the C++ Programming forums, part of the General Programming Boards category; blah blah blah...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    32

    need an example

    blah blah blah
    Last edited by sjcc; 05-07-2009 at 03:21 PM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What have you done so far? We do not do other homework for you, but we will advice you, if you show us that you are working on it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    32
    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    
    
    double average(const vector<double>& a);
    
    double median(const vector<double>& a);
    
    int main()
    {
       vector<double> z;
       
    
       
       z.push_back(32);
       z.push_back(31);
       z.push_back(29);
       z.push_back(30);
       z.push_back(32);
       z.push_back(31.4);
       
       cout << "The average of the data set is: " 
            << average(z)
            << ".\n";
            
            
       cout << "The median of the data set is: " 
            <<median(z)
            << ".\n";
    
        return 0;
    }
    
    
    double average(const vector<double>& a)
    {
       double sum = 0;
       
       unsigned int i;
       for( i = 0; i < a.size(); i++ )
          sum += a[i];
    
       double avg = 0;
       if (a.size() > 0)
          avg = sum / a.size();
       
       return avg;
    }
    double median(const vector<double>& a)
    {
       //first create a duplicate of the vector.
       //Sort the duplicate, and not the original vector.
       vector<double> dup = a;
       sort(dup.begin(), dup.end());
       
       int med_index = dup.size() / 2;
       double med;
       if (dup.size()%2 > 0)  //odd number of elements: 2 5 10
          med = dup[ med_index ];
       else
       {  //even number of elements: 3, 4, 9, 10
          med = (dup[med_index] + dup[med_index - 1])/2.0;
       }
       return med;
    }
    here you go..

    parts of the prob. is to find the median and avg. and no i was not expecting you to do it for me... I was just asking for an example

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    And is there a problem?

    Perhaps the median function should check for empty vector too.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Second try... The first got lost when I fumbled the touch-pad on my work-laptop...

    What is not working with your median/average functions? The look accurate enough to me.

    I don't really see the point of passing the vector by reference to median, and then duplicating it. It would be simpler coding wise to just pass it by value and thus creating a copy immediately.

    We get A LOT (like a few every day) of people asking similar "can someone give me an example to of ..." in an effort to solve their homework without themselves doing any work, so you have to understand that it's easy to assume that your question is of the same kind.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

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