7.12 Lab 10 Part 1: Vectors - Calculating the median General description This la
ID: 3779351 • Letter: 7
Question
7.12 Lab 10 Part 1: Vectors - Calculating the median
General description
This lab allows you to implement the main as you see fit to test your function. 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> // std::sort
using namespace std;
//function prototype
float median(vector<int> num);
int main()
{
//declare vector variable of type int
vector<int> v;
int n,num;
//enter numbers into vectors
cout<<"No of elements in vector"<<endl;
cin>>n;
cout<<"Enter elements of vector: ";
for(int i = 0 ; i < n; i++)
{
cout<<"Element"<<i+1<<":";
cin>>num;
v.push_back(num);
}
//sort vector using built in sort
sort(v.begin(), v.end());
//call median function to
cout<<"Median value in given vector = "<<median(v)<<endl;
}
/*Function Definition
Name: vectorMedian
Parameters: a previously sorted vector of integers
Returns: the median value*/
float median(vector<int> v)
{
float median;
int index;
//if there are odd number of elements in vector ,there will be one median value
if( v.size() % 2 == 1)
{
index=v.size()/2;
median = v[index];
return median;
}
//if there are even number of elements in vector ,then median value will be average of the two middle spots
if( v.size() % 2 == 0)
{
index=v.size()/2;
median = float(v[index] +v[index-1])/2;
return median;
}
}
---------------------------------------------------------------------------------
output
No of elements in vector
4
Enter elements of vector:
Element1:8
Element2:4
Element3:3
Element4:7
Median value in given vector = 5.5
one more execution output
No of elements in vector
5
Enter elements of vector:
Element1:14
Element2:7
Element3:8
Element4:4
Element5:3
Median value in given vector = 7
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.