diff options
Diffstat (limited to 'CST116F2021-Lab8/13b/13b.cpp')
| -rw-r--r-- | CST116F2021-Lab8/13b/13b.cpp | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/CST116F2021-Lab8/13b/13b.cpp b/CST116F2021-Lab8/13b/13b.cpp new file mode 100644 index 0000000..cb7d5a8 --- /dev/null +++ b/CST116F2021-Lab8/13b/13b.cpp @@ -0,0 +1,37 @@ +// 13b.cpp : This file contains the 'main' function. Program execution begins and ends there. +// + +#include <iostream> +#include <math.h> +#include <algorithm> +#include "13b.h" + +using namespace std; + +const int SIZE = 10; + +int main() +{ + float nums[SIZE] = { 3, 5.3, 7, 3.7, 2, 9, 10}; + int amt = 7; + + cout << "The median value in the array is: " << FindMedian(nums, amt); +} + +float FindMedian(float nums[], const int amt) +{ + float sum; + + sort(nums, nums + amt); + + if (amt % 2 == 0) + { + sum = nums[amt / 2 - 1] + nums[amt / 2]; + return sum / 2.0; + } + else + { + return nums[(int)ceil(amt / 2.0)]; + } + +}
\ No newline at end of file |