diff --git a/histogram.cpp b/histogram.cpp index 4c759c6..c65e454 100644 --- a/histogram.cpp +++ b/histogram.cpp @@ -3,41 +3,47 @@ #include using namespace std; -void +bool find_minmax(const vector& numbers, double& min, double& max) { - min = numbers[0]; - max = numbers[0]; - for (double x : numbers) { - if (x < min) { - min = x; - } - else if (x > max) { - max = x; - } - } + if (numbers.empty()) { + return false; + } + + min = numbers[0]; + max = numbers[0]; + for (double x : numbers) { + if (x < min) { + min = x; + } + else if (x > max) { + max = x; + } + } + + return true; } std::vector make_histogram(const std::vector& numbers, size_t bin_count) { - vector bins(bin_count); + vector bins(bin_count); - double min = 0, max = 0; - find_minmax(numbers, min, max); + double min = 0, max = 0; + find_minmax(numbers, min, max); - double bin_size = (max - min) / bin_count; - for (int i = 0; i < numbers.size(); i++) { - bool found = false; - for (int 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; + double bin_size = (max - min) / bin_count; + for (int i = 0; i < numbers.size(); i++) { + bool found = false; + for (int 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; } diff --git a/histogram_internal.h b/histogram_internal.h index a6ef22d..bc6165c 100644 --- a/histogram_internal.h +++ b/histogram_internal.h @@ -1,4 +1,4 @@ #pragma once #include -void +bool find_minmax(const std::vector& numbers, double& min, double& max); \ No newline at end of file diff --git a/main.cpp b/main.cpp index f233c47..8568006 100644 --- a/main.cpp +++ b/main.cpp @@ -21,42 +21,42 @@ input_data() { cerr << "Enter number count: "; cin >> number_count; - Input in; - in.numbers.resize(number_count); + Input in1; + in1.numbers.resize(number_count); cerr << "Enter numbers: "; for (size_t i = 0; i < number_count; i++) { - cin >> in.numbers[i]; + cin >> in1.numbers[i]; } cerr << "Enter bin count: "; - cin >> in.bin_count; + cin >> in1.bin_count; cerr << "Enter stroke colour without spaces or in code format:"; cin.ignore(); - getline(cin, in.stroke); + getline(cin, in1.stroke); - for (i = 0; i < in.stroke.length(); i++) { - if (isspace(in.stroke[i])) { + for (i = 0; i < in1.stroke.length(); i++) { + if (isspace(in1.stroke[i])) { cerr << "invalid input"; exit(0); } } - if (in.stroke[0] == check2[0]) { - if (in.stroke.length() != 7) { + if (in1.stroke[0] == check2[0]) { + if (in1.stroke.length() != 7) { cerr << "invalid input"; exit(0); } } - return in; + return in1; } int main() { const size_t SCREEN_WIDTH = 80; const size_t MAX_ASTERISK = SCREEN_WIDTH - 3 - 1; - auto in = input_data(); - auto bins = make_histogram(in.numbers, in.bin_count); - show_histogram_svg(bins, in.stroke); + auto in1 = input_data(); + auto bins = make_histogram(in1.numbers, in1.bin_count); + show_histogram_svg(bins, in1.stroke); return 0; } diff --git a/unittest.cpp b/unittest.cpp index f91aa73..44c8a0a 100644 --- a/unittest.cpp +++ b/unittest.cpp @@ -13,9 +13,7 @@ TEST_CASE("distinct positive numbers") { TEST_CASE("empty vector") { double min = 0; double max = 0; - find_minmax({NULL}, min, max); - CHECK(min == 0); - CHECK(max == 0); + CHECK(!find_minmax({}, min, max)); } TEST_CASE("one element") { double min = 0;