Guest User

Untitled

a guest
Jun 24th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include<cstdlib>
  4. #include<ctime>
  5. #include<random>
  6. #include<vector>
  7. #include<cstdlib>
  8. #include<ctime>
  9. #include<random>
  10.  
  11. using namespace std;
  12.  
  13. double find_median(vector<double>);
  14. double find_median(vector<double> len)
  15. {
  16. {
  17. int i;
  18. double temp;
  19. int n=len.size();
  20. int mid;
  21. double median;
  22. bool swap;
  23.  
  24. do
  25. {
  26. swap = false;
  27. for (i = 0; i< len.size()-1; i++)
  28. {
  29. if (len[i] > len[i + 1])
  30. {
  31. temp = len[i];
  32. len[i] = len[i + 1];
  33. len[i + 1] = temp;
  34. swap = true;
  35. }
  36. }
  37. }
  38. while (swap);
  39.  
  40. for (i=0; i<len.size(); i++)
  41. {
  42. if (len[i]>len[i+1])
  43. {
  44. temp=len[i];
  45. len[i]=len[i+1];
  46. len[i+1]=temp;
  47. }
  48. mid=len.size()/2;
  49. if (mid%2==0)
  50. {
  51. median= len[i]+len[i+1];
  52. }
  53. else
  54. {
  55. median= (len[i]+0.5);
  56. }
  57. }
  58. return median;
  59. }
  60. }
  61. int main()
  62. {
  63. int n,i;
  64.  
  65. cout<<"Input the vector size: "<<endl;
  66. cin>>n;
  67. vector <double> foo(n);
  68. mt19937 rand_generator;
  69. rand_generator.seed(time(0));
  70.  
  71. uniform_real_distribution<double> rand_distribution(0,0.8);
  72. cout<<"original vector: "<<" ";
  73. for (i=0; i<n; i++)
  74. {
  75. double rand_num=rand_distribution(rand_generator);
  76. foo[i]=rand_num;
  77. cout<<foo[i]<<" ";
  78.  
  79. }
  80. double median;
  81. median=find_median(foo);
  82.  
  83. cout<<endl;
  84.  
  85. cout<<"The median of the vector is: "<<" ";
  86. cout<<median<<endl;
  87. }
  88.  
  89. const auto median_it = len.begin() + len.size() / 2;
  90. std::nth_element(len.begin(), median_it , len.end());
  91. auto median = *median_it;
  92.  
  93. assert(!len.empty());
  94. if (len.size() % 2 == 0) {
  95. const auto median_it1 = len.begin() + len.size() / 2 - 1;
  96. const auto median_it2 = len.begin() + len.size() / 2;
  97.  
  98. std::nth_element(len.begin(), median_it1 , len.end());
  99. const auto e1 = *median_it1;
  100.  
  101. std::nth_element(len.begin(), median_it2 , len.end());
  102. const auto e2 = *median_it2;
  103.  
  104. return (e1 + e2) / 2;
  105.  
  106. } else {
  107. const auto median_it = len.begin() + len.size() / 2;
  108. std::nth_element(len.begin(), median_it , len.end());
  109. return *median_it;
  110. }
  111.  
  112. for (i=0; i<len.size(); i++){
  113. if (len[i]>len[i+1])
  114.  
  115. std::sort(len.begin(), len.end());
  116. double median = len[len.size() / 2];
  117.  
  118. 0.5 * (len[len.size() / 2 - 1] + len[len.size() / 2]);
  119.  
  120. #include<vector>
  121.  
  122. double find_median(std::vector<double> len);
  123.  
  124. // Return the number of failures - shell interprets 0 as 'success',
  125. // which suits us perfectly.
  126. int main()
  127. {
  128. return find_median({0, 1, 1, 2}) != 1;
  129. }
  130.  
  131. #include <algorithm>
  132. #include <limits>
  133. #include <vector>
  134.  
  135. double find_median(std::vector<double> len)
  136. {
  137. if (len.size() < 1)
  138. return std::numeric_limits<double>::signaling_NaN();
  139.  
  140. const auto alpha = len.begin();
  141. const auto omega = len.end();
  142.  
  143. // Find the two middle positions (they will be the same if size is odd)
  144. const auto i1 = alpha + (len.size()-1) / 2;
  145. const auto i2 = alpha + len.size() / 2;
  146.  
  147. // Partial sort to place the correct elements at those indexes (it's okay to modify the vector,
  148. // as we've been given a copy; otherwise, we could use std::partial_sort_copy to populate a
  149. // temporary vector).
  150. std::nth_element(alpha, i1, omega);
  151. std::nth_element(i1, i2, omega);
  152.  
  153. return 0.5 * (*i1 + *i2);
  154. }
  155.  
  156. #include <iostream>
  157. bool test_median(const std::vector<double>& v, double expected)
  158. {
  159. auto actual = find_median(v);
  160. if (abs(expected - actual) > 0.01) {
  161. std::cerr << actual << " - expected " << expected << std::endl;
  162. return true;
  163. } else {
  164. std::cout << actual << std::endl;
  165. return false;
  166. }
  167. }
  168.  
  169. int main()
  170. {
  171. return test_median({0, 1, 1, 2}, 1)
  172. + test_median({5}, 5)
  173. + test_median({5, 5, 5, 0, 0, 0, 1, 2}, 1.5);
  174. }
  175.  
  176. #include <ctime>
  177. #include <functional>
  178. #include <random>
  179.  
  180. int main(int argc, char **argv)
  181. {
  182. std::vector<double> foo;
  183.  
  184. const int n = argc > 1 ? std::stoi(argv[1]) : 10;
  185. foo.reserve(n);
  186.  
  187. std::mt19937 rand_generator(std::time(0));
  188. std::uniform_real_distribution<double> rand_distribution(0,0.8);
  189. std::generate_n(std::back_inserter(foo), n, std::bind(rand_distribution, rand_generator));
  190.  
  191. std::cout << "Vector:";
  192. for (auto v: foo)
  193. std::cout << ' ' << v;
  194.  
  195. std::cout << "nMedian = " << find_median(foo) << std::endl;
  196. }
  197.  
  198. typename<class Iterator>
  199. auto find_median(Iterator alpha, Iterator omega)
  200. {
  201. using value_type = typename Iterator::value_type;
  202.  
  203. if (alpha == omega)
  204. return std::numeric_limits<value_type>::signaling_NaN();
  205. }
Add Comment
Please, Sign In to add comment