blah blah blah
blah blah blah
Last edited by sjcc; 05-07-2009 at 03:21 PM.
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.
here you go..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; }
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
And is there a problem?
Perhaps the median function should check for empty vector too.
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
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.