Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. // Name: Julians Krumins
  2. // Date: February 26, 2020
  3. // Project: ECE_Exercise_21
  4.  
  5. #include<iostream>
  6. #include<cstdlib>
  7. using namespace std;
  8.  
  9.  
  10. void new_random_array(double a[], int s)
  11. {
  12. for (int n=0; n < s; n++)
  13. {
  14. a[n] = double(rand() % 100);
  15. }
  16. }
  17.  
  18. void display_array(double a[], int s)
  19. {
  20. for (int n = 0; n < s; n++)
  21. {
  22. cout << a[n] << endl;
  23. }
  24. }
  25.  
  26. double array_max(double a[], int s)
  27. {
  28. double max = a[0];
  29. for (int n = 1; n < s; n++)
  30. {
  31. if (a[n] > max)
  32. {
  33. max = a[n];
  34. }
  35. }
  36. return max;
  37. }
  38.  
  39. double array_min(double a[], int s)
  40. {
  41. double min = a[0];
  42. for (int n = 1; n < s; n++)
  43. {
  44. if (a[n] < min)
  45. {
  46. min = a[n];
  47. }
  48. }
  49. return min;
  50. }
  51.  
  52. void array_sort(double a[], int s)
  53. {
  54. double swap = 0;
  55. for (int i = s - 1; i > 0; i--)
  56. {
  57. for (int j = 0; j < i; j++)
  58. {
  59. if (a[j] < a[j + 1])
  60. {
  61. swap = a[j];
  62. a[j] = a[j + 1];
  63. a[j + 1] = swap;
  64. }
  65. }
  66. }
  67. return;
  68. }
  69. void array_disp(double a[], int s)
  70. {
  71. array_sort(a, s);
  72. for (int i = 0; i < s; i++)
  73. {
  74. cout << a[i] << " " << endl;
  75. }
  76. cout << endl;
  77. }
  78.  
  79. double array_mean(double a[], int s)
  80. {
  81. double sum = 0;
  82. for (int n = 0; n < s; n++)
  83. {
  84. sum = sum + a[n];
  85. }
  86. return sum / s;
  87. }
  88.  
  89. double array_median(double a[], int s)
  90. {
  91. double med = 0;
  92. array_sort(a, s);
  93. if (s % 2 == 1)
  94. {
  95. med = a[s / 2];
  96. }
  97. else
  98. {
  99. med = (a[s / 2] + a[s / 2 - 1]) / 2.0;
  100. }
  101. return med;
  102. }
  103. double array_variance(double a[], int s)
  104. {
  105. double mean = array_mean(a, s);
  106. double var = 0;
  107. for (int n = 0; n < s; n++)
  108. {
  109. var = var + (a[n] - mean) * (a[n] - mean);
  110. }
  111. var = var / (double(s) - 1);
  112. return var;
  113. }
  114.  
  115. int main()
  116. {
  117. const int size = 10;
  118. double array[size];
  119. int selection = 0;
  120.  
  121. do
  122. {
  123. system("cls");
  124.  
  125. cout << "Main Menu" << endl;
  126. cout << "1. Create a new array" << endl;
  127. cout << "2. Display array" << endl;
  128. cout << "3. Find Maximum value" << endl;
  129. cout << "4. Find Minimum value" << endl;
  130. cout << "5. Sort the array in descending order and display" << endl;
  131. cout << "6. Find mean value" << endl;
  132. cout << "7. Find median value" << endl;
  133. cout << "8. Find variance" << endl;
  134. cout << "9. End." << endl << endl;
  135. cout << "Make a selection: ";
  136. cin >> selection;
  137.  
  138. switch (selection)
  139. {
  140. case 1:
  141. cout << "Created array. " << endl;
  142. new_random_array(array, size);
  143. cout << endl;
  144. system("pause");
  145. break;
  146. case 2:
  147. cout << "Display array: " << endl;
  148. display_array(array, size);
  149. cout << endl;
  150. system("pause");
  151. break;
  152. case 3:
  153. cout << "Maximum value: ";
  154. cout << array_max(array, size);
  155. cout << endl;
  156. system("pause");
  157. break;
  158. case 4:
  159. cout << "Minimum value: ";
  160. cout << array_min(array, size);
  161. cout << endl;
  162. system("pause");
  163. break;
  164. case 5:
  165. cout << "5. Sorted in descending order: " << endl;
  166. array_disp(array, size);
  167. cout << endl;
  168. system("pause");
  169. break;
  170. case 6:
  171. cout << "mean value: ";
  172. cout << array_mean(array, size);
  173. cout << endl;
  174. system("pause");
  175. break;
  176. case 7:
  177. cout << "median value: ";
  178. cout << array_mean(array, size);
  179. cout << endl;
  180. system("pause");
  181. break;
  182. case 8:
  183. cout << "variance: ";
  184. cout << array_variance(array, size);
  185. cout << endl;
  186. system("pause");
  187. break;
  188. case 9:
  189. break;
  190. system("cls");
  191. }
  192. } while (selection != 0);
  193. return 0;
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement