#include <vector>
#include "histogram.h"
using namespace std;

void find_minmax(const vector<double> &numbers, double &min, double &max)
{
    min = numbers[0];
    max = numbers[0];
    for (auto el : numbers)
    {
        if (el > max)
        {
            max = el;
        }
        if (el < min)
        {
            min = el;
        }
    }
}

vector<size_t> make_histogram(const vector<double> &numbers, const size_t bin_count){
        vector<size_t> bins(bin_count);
        double max, min;
        find_minmax(numbers, min, max);
        double binSize = (max - min) / bin_count;
        for (size_t i = 0; i < numbers.size(); i++)
        {
            bool found = false;
            for (size_t j = 0; (j < bin_count - 1) && !found; j++)
            {
                auto lo = min + j * binSize;
                auto hi = min + (j + 1) * binSize;
                if ((lo <= numbers[i]) && (numbers[i] < hi))
                {
                    bins[j]++;
                    found = true;
                }
            }
            if (!found)
            {
                bins[bin_count - 1]++;
            }
        }
        return bins;
    }