Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

ID: 3779140 • Letter: #

Question

-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());

Explanation / Answer

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
//Returns the Median of the vector
double vectorMedian(vector<int> data)
{
double median;
//Stores the size of the vector
size_t size = data.size();
//If even number of data is available
if (size % 2 == 0)
{
median = ((double)data[size / 2 - 1] + (double)data[size / 2]) / 2.0;
}
//If odd number of data is available
else
{
median = data[size / 2];
}
return median;
}
int main()
{
//Creates a vector of type integer
vector<int> vecInt;
int no, data, c;
cout<<" Enter how many numbers you want to enter: ";
cin>>no;
cout<<" Enter "<<no<<" Numbers: "<<endl;
//Accepts the numbers and stores it in the vector
for(c = 0; c < no; c++)
{
cout<<"Enter"<<c+1<<" Number: ";
cin>>data;
vecInt.push_back(data);
}
cout<<" Original ";
//Using Iterator displaying the Original Vector data
for (vector<int>::iterator it = vecInt.begin() ; it != vecInt.end(); ++it)
cout << ' ' << *it;

//Sorting the data in the vector
sort(vecInt.begin(),vecInt.end());

cout<<" After Sorting: ";
//Using Iterator displaying the Sorted Vector data
for (vector<int>::iterator it = vecInt.begin() ; it != vecInt.end(); ++it)
cout << ' ' << *it;

//Calls the median method and displays the median
cout<<" Median = "<<vectorMedian(vecInt);

return 0;
}

Output 1:

Enter how many numbers you want to enter: 7

Enter 7 Numbers:
Enter1 Number: 45
Enter2 Number: 56
Enter3 Number: 11
Enter4 Number: 5
Enter5 Number: 78
Enter6 Number: 66
Enter7 Number: 99

Original
45 56 11 5 78 66 99
After Sorting:
5 11 45 56 66 78 99
Median = 56

Output 2:

Enter how many numbers you want to enter: 6

Enter 6 Numbers:
Enter1 Number: 45
Enter2 Number: 6
Enter3 Number: 12
Enter4 Number: 5
Enter5 Number: 89
Enter6 Number: 74

Original
45 6 12 5 89 74
After Sorting:
5 6 12 45 74 89
Median = 28.5