C program Using Vectors to calculate mean Implement the main

C++ program Using Vectors to calculate mean

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

Solution

//OUTLINE:

//if Size of vector is even implies median is middle two values of sorted vector

//if Size of vector is odd implies median is the middle value of sorted vector input.

//middle two values are v[size/2] and v[size/2-1] since indices start from 0.

//therefore median is (v[size/2-1]+v[size/2])/2 if size is even

//median is v[size/2] if size is odd

//Program_code

#include <iostream>
#include<algorithm> //for sorting vectors
#include <vector> //for using vectors
using namespace std;
double vectorMedian(vector<int> median)
{
   sort(median.begin(),median.end()); //soritng vector to calculate median directly
   if(median.size() %2 == 0) //checking whether vector has even elements or odd number of elements.
       return (median[median.size()/2-1]+median[median.size()/2])/2.0;
   else //since size of vector is odd median will be middle element
       return median[median.size()/2];
}

//Main_code to test implementation
int main ()
{
vector<int> test1,test2; //two test vectors to test whether median is correct or incorrect
   cout<<\"Test 1\"<<endl<<\"create a vector with values 111 to 1 in decreasing order\"<<endl;
for (int i=111; i>0; i--) test1.push_back(i); //pushing 111 to 1 into vector test1 middle element is 56
  
cout<<\"OutPut Median of Vector is: \"<<vectorMedian(test1)<<endl;
  
cout<<\"Test 2\"<<endl<<\"create a vector with values 1000 to 1 in decreasing order\"<<endl;
for (int i=1000; i>0; i--) test2.push_back(i); //pushing integers 1000 to 1 into vector test2 middle elements are 500 and 501 hence median is 500.5.
  
cout<<\"OutPut Median of Vector is: \"<<vectorMedian(test2)<<endl;
  

return 0;
}

C++ program Using Vectors to calculate mean Implement the main as you see fit to test your function. The goal is to implement the function that calculates the m
C++ program Using Vectors to calculate mean Implement the main as you see fit to test your function. The goal is to implement the function that calculates the m

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site