main
BykovDA 2 месяцев назад
Родитель 61d83436b4
Сommit 0a9b08ce4d

@ -0,0 +1,35 @@
#include "histogram.h"
bool
find_minmax(const std::vector<double>& numbers, double& min, double& max) {
min = numbers[0];
max = numbers[0];
for(double x : numbers){
if(x<min){
min=x;
}
if(x>max){
max=x;
}
}
}
std::vector<size_t>
make_histogram(const std::vector<double>& numbers,size_t bin_count){
std::vector<size_t> bins(bin_count);
double min,max;
find_minmax(numbers,min,max);
double bin_size=(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*bin_size;
auto hi=min+(j+1)*bin_size;
if((lo<=numbers[i])&&(numbers[i]<hi)){
bins[j]++;
found=true;
}
}if(!found){
bins[bin_count-1]++;
}
}
return bins;
}

@ -0,0 +1,8 @@
#ifndef HISTOGRAM_INTERNAL_H_INCLUDED
#define HISTOGRAM_INTERNAL_H_INCLUDED
#include <vector>
#include <iostream>
bool
find_minmax(const std::vector<double>& numbers, double& min, double& max);
#endif

@ -0,0 +1,38 @@
#define DOCTEST_CONFIG_NO_MULTITHREADING
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"
#include "histogram_internal.h"
TEST_CASE("distinct positive numbers") {
double min = 0;
double max = 0;
find_minmax({2, 2}, min, max);
CHECK(min == 2);
CHECK(max == 2);
}
TEST_CASE("empty vector"){
double min = 0;
double max = 0;
CHECK(!find_minmax({}, min, max));
}
TEST_CASE("vector of one elements"){
double min = 0;
double max = 0;
find_minmax({3}, min, max);
CHECK(min == max);
}
TEST_CASE("distinct negative numbers"){
double min = 0;
double max = 0;
find_minmax({-1, -2}, min, max);
CHECK(min == -2);
CHECK(max == -1);
}
TEST_CASE("vector of the same elements"){
double min = 0;
double max = 0;
find_minmax({3,3,3}, min, max);
CHECK(min == 3);
CHECK(max == 3);
}
Загрузка…
Отмена
Сохранить