Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<vector>
- #include<cstdlib>
- #include<ctime>
- #include<random>
- #include<vector>
- #include<cstdlib>
- #include<ctime>
- #include<random>
- using namespace std;
- double find_median(vector<double>);
- double find_median(vector<double> len)
- {
- {
- int i;
- double temp;
- int n=len.size();
- int mid;
- double median;
- bool swap;
- do
- {
- swap = false;
- for (i = 0; i< len.size()-1; i++)
- {
- if (len[i] > len[i + 1])
- {
- temp = len[i];
- len[i] = len[i + 1];
- len[i + 1] = temp;
- swap = true;
- }
- }
- }
- while (swap);
- for (i=0; i<len.size(); i++)
- {
- if (len[i]>len[i+1])
- {
- temp=len[i];
- len[i]=len[i+1];
- len[i+1]=temp;
- }
- mid=len.size()/2;
- if (mid%2==0)
- {
- median= len[i]+len[i+1];
- }
- else
- {
- median= (len[i]+0.5);
- }
- }
- return median;
- }
- }
- int main()
- {
- int n,i;
- cout<<"Input the vector size: "<<endl;
- cin>>n;
- vector <double> foo(n);
- mt19937 rand_generator;
- rand_generator.seed(time(0));
- uniform_real_distribution<double> rand_distribution(0,0.8);
- cout<<"original vector: "<<" ";
- for (i=0; i<n; i++)
- {
- double rand_num=rand_distribution(rand_generator);
- foo[i]=rand_num;
- cout<<foo[i]<<" ";
- }
- double median;
- median=find_median(foo);
- cout<<endl;
- cout<<"The median of the vector is: "<<" ";
- cout<<median<<endl;
- }
- const auto median_it = len.begin() + len.size() / 2;
- std::nth_element(len.begin(), median_it , len.end());
- auto median = *median_it;
- assert(!len.empty());
- if (len.size() % 2 == 0) {
- const auto median_it1 = len.begin() + len.size() / 2 - 1;
- const auto median_it2 = len.begin() + len.size() / 2;
- std::nth_element(len.begin(), median_it1 , len.end());
- const auto e1 = *median_it1;
- std::nth_element(len.begin(), median_it2 , len.end());
- const auto e2 = *median_it2;
- return (e1 + e2) / 2;
- } else {
- const auto median_it = len.begin() + len.size() / 2;
- std::nth_element(len.begin(), median_it , len.end());
- return *median_it;
- }
- for (i=0; i<len.size(); i++){
- if (len[i]>len[i+1])
- std::sort(len.begin(), len.end());
- double median = len[len.size() / 2];
- 0.5 * (len[len.size() / 2 - 1] + len[len.size() / 2]);
- #include<vector>
- double find_median(std::vector<double> len);
- // Return the number of failures - shell interprets 0 as 'success',
- // which suits us perfectly.
- int main()
- {
- return find_median({0, 1, 1, 2}) != 1;
- }
- #include <algorithm>
- #include <limits>
- #include <vector>
- double find_median(std::vector<double> len)
- {
- if (len.size() < 1)
- return std::numeric_limits<double>::signaling_NaN();
- const auto alpha = len.begin();
- const auto omega = len.end();
- // Find the two middle positions (they will be the same if size is odd)
- const auto i1 = alpha + (len.size()-1) / 2;
- const auto i2 = alpha + len.size() / 2;
- // Partial sort to place the correct elements at those indexes (it's okay to modify the vector,
- // as we've been given a copy; otherwise, we could use std::partial_sort_copy to populate a
- // temporary vector).
- std::nth_element(alpha, i1, omega);
- std::nth_element(i1, i2, omega);
- return 0.5 * (*i1 + *i2);
- }
- #include <iostream>
- bool test_median(const std::vector<double>& v, double expected)
- {
- auto actual = find_median(v);
- if (abs(expected - actual) > 0.01) {
- std::cerr << actual << " - expected " << expected << std::endl;
- return true;
- } else {
- std::cout << actual << std::endl;
- return false;
- }
- }
- int main()
- {
- return test_median({0, 1, 1, 2}, 1)
- + test_median({5}, 5)
- + test_median({5, 5, 5, 0, 0, 0, 1, 2}, 1.5);
- }
- #include <ctime>
- #include <functional>
- #include <random>
- int main(int argc, char **argv)
- {
- std::vector<double> foo;
- const int n = argc > 1 ? std::stoi(argv[1]) : 10;
- foo.reserve(n);
- std::mt19937 rand_generator(std::time(0));
- std::uniform_real_distribution<double> rand_distribution(0,0.8);
- std::generate_n(std::back_inserter(foo), n, std::bind(rand_distribution, rand_generator));
- std::cout << "Vector:";
- for (auto v: foo)
- std::cout << ' ' << v;
- std::cout << "nMedian = " << find_median(foo) << std::endl;
- }
- typename<class Iterator>
- auto find_median(Iterator alpha, Iterator omega)
- {
- using value_type = typename Iterator::value_type;
- if (alpha == omega)
- return std::numeric_limits<value_type>::signaling_NaN();
- }
Add Comment
Please, Sign In to add comment