#include <iostream>
#include <vector>
#include <math.h>
#include "histogram.h"


using namespace std;

 static void find_minmax(const vector<double>& A, double& min, double& max)
{
	min = A[0];
	for (auto i = 0; i < A.size(); i++)
	{
		if (A[i] < min)
		{
			min = A[i];
		}
	}

	max = A[0];
	for (auto i = 0; i < A.size(); i++)
	{
		if (A[i] > max)
		{
			max = A[i];
		}
	}
	return;
}

vector <size_t> make_histogram(const vector<double>& A, size_t bin)
{
	vector<size_t>B(bin);
	size_t max_count;
	double max, min;
	find_minmax(A, min, max);
	double step = (max - min) / (bin);

	for (size_t i = 0; i < A.size(); i++)
	{
		for (size_t j = 0; j < bin; j++)
		{
			if ((A[i] >= (min + j * step)) && (A[i] < (min + (j + 1)*step)))
			{
				B[j]++;
				break;
			}
		}
	}

	for (size_t i = 0; i < A.size(); i++)
	{
		if (A[i] == max)
			B[bin - 1]++;
	}
	return B;
}
