Please help I need it to be written in c The goal is to impl

Please help I need it to be written in c++.

The goal is to implement the function that calculates the median of a vector of integers.

Function Definition

Name: vectorMedian

Parameters: a previously sorted vector of integers

Returns: the median value

Development and Testing

Develop the function in steps.

Outline the problem as an algorithm.

Take your outline and produce a series of comments in your code.

Beginning writing your code.

Test your code by writing a main function to call your function, test as you develop parts of your algorithm.

Description of median

We will assume the vector of integers is previously sorted, you can read about how to do that below. If we are given a vector containing {3, 4, 7, 8, 14} then the median will be 7 because that is the middle spot. However, if we have {3, 4, 7, 8} then the median is calculated as a floating point average of the two middle spots, so 4 and 7 are the middle values giving a median of 5.5.

Sorting Vectors

Within the algorithm library there is a sort procedure (a.k.a void function). The function call arguments are the beginning and the end of the vector, and the vector will be sorted when the function completes. An example call would be: sort(v.begin(), v.end());

Solution

#include <iostream>
#include <vector>
using namespace std;


double vectorMedian(vector<int> vvalues)
{
double median;
size_t size = vvalues.size();

if (size % 2 == 0)
{
median = (vvalues[size / 2 - 1] + vvalues[size / 2]) / 2;
}
else
{
median = vvalues[size / 2];
}

return median;
}

int main() {
// create a vector to store int
vector<int> vvalues;
int i,n,m;
cout << \"How many values do you wish to push into the vector\" << endl;

cin >> n;

for(i=0;i<n;i++)

{

cout << \"\ Enter the element : \";

cin >> m;

vvalues.push_back(m);

}

sort(vvalues.begin(), vvalues.end());
cout << \"Median of the vector : \" ;

cout << vectorMedian(vvalues);

return 0;

}

Please help I need it to be written in c++. The goal is to implement the function that calculates the median of a vector of integers. Function Definition Name:
Please help I need it to be written in c++. The goal is to implement the function that calculates the median of a vector of integers. Function Definition Name:

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site