aboutsummaryrefslogtreecommitdiff
path: root/CST116F2021-Lab8/13b/13b.cpp
blob: cb7d5a88b4ffed54dd0cba690f0a187d22802e5d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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)];
    }

}