code: ошибка в функции вывода гистограммы
Этот коммит содержится в:
59
histogram.cpp
Обычный файл
59
histogram.cpp
Обычный файл
@@ -0,0 +1,59 @@
|
||||
#include <vector>
|
||||
#include "histogram.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
static
|
||||
void
|
||||
find_minmax(const vector<double>& numbers, double& min, double& max) {
|
||||
min = numbers[0];
|
||||
max = numbers[0];
|
||||
for (size_t i = 0; i < numbers.size(); i++){
|
||||
if (numbers[i] < min){
|
||||
min = numbers[i];
|
||||
}
|
||||
if (numbers[i] > max){
|
||||
max = numbers[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
vector <double> make_histogram(const vector<double> numbers, size_t bin_count){
|
||||
|
||||
double min, max;
|
||||
find_minmax(numbers, min, max);
|
||||
double bin_size = (max - min) / bin_count;
|
||||
vector <double> bins;
|
||||
bins.resize(bin_count);
|
||||
|
||||
for (std::size_t i = 0; i < numbers.size(); i++)
|
||||
{
|
||||
bool found = false;
|
||||
for (std::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]++;
|
||||
}
|
||||
}
|
||||
double out;
|
||||
for (std::size_t i = 0; i < bin_count-1; i++)
|
||||
{
|
||||
if (bins[i] > bins[i+1])
|
||||
{
|
||||
out = bins[i];
|
||||
bins[i] = bins[i + 1];
|
||||
bins[i + 1] = out;
|
||||
}
|
||||
}
|
||||
return bins;
|
||||
}
|
||||
|
||||
5
histogram.h
Обычный файл
5
histogram.h
Обычный файл
@@ -0,0 +1,5 @@
|
||||
#ifndef HISTOGRAM_H_INCLUDED
|
||||
#define HISTOGRAM_H_INCLUDED
|
||||
std::vector<size_t>
|
||||
make_histogram(const std::vector<double>& numbers, const std::size_t bin_count);
|
||||
#endif // HISTOGRAM_H_INCLUDED
|
||||
41
lab_03.cbp
Обычный файл
41
lab_03.cbp
Обычный файл
@@ -0,0 +1,41 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||
<CodeBlocks_project_file>
|
||||
<FileVersion major="1" minor="6" />
|
||||
<Project>
|
||||
<Option title="lab_03" />
|
||||
<Option pch_mode="2" />
|
||||
<Option compiler="gcc" />
|
||||
<Build>
|
||||
<Target title="Debug">
|
||||
<Option output="bin/Debug/lab_03" prefix_auto="1" extension_auto="1" />
|
||||
<Option object_output="obj/Debug/" />
|
||||
<Option type="1" />
|
||||
<Option compiler="gcc" />
|
||||
<Compiler>
|
||||
<Add option="-g" />
|
||||
</Compiler>
|
||||
</Target>
|
||||
<Target title="Release">
|
||||
<Option output="bin/Release/lab_03" prefix_auto="1" extension_auto="1" />
|
||||
<Option object_output="obj/Release/" />
|
||||
<Option type="1" />
|
||||
<Option compiler="gcc" />
|
||||
<Compiler>
|
||||
<Add option="-O2" />
|
||||
</Compiler>
|
||||
<Linker>
|
||||
<Add option="-s" />
|
||||
</Linker>
|
||||
</Target>
|
||||
</Build>
|
||||
<Compiler>
|
||||
<Add option="-Wall" />
|
||||
<Add option="-fexceptions" />
|
||||
</Compiler>
|
||||
<Unit filename=".gitignore" />
|
||||
<Unit filename="histogram.cpp" />
|
||||
<Unit filename="histogram.h" />
|
||||
<Unit filename="main.cpp" />
|
||||
<Extensions />
|
||||
</Project>
|
||||
</CodeBlocks_project_file>
|
||||
73
main.cpp
73
main.cpp
@@ -1,58 +1,37 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include "histogram.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
const int max_length = 80; // ìàêñèìàëüíàÿ äëèíà
|
||||
const int indent = 4; // îòñòóï
|
||||
|
||||
int main()
|
||||
{
|
||||
size_t number_count, bin_count;
|
||||
cerr << "Enter number count: ";
|
||||
cin >> number_count; // ââîä êîë-âà îöåíîê
|
||||
vector<double> numbers(number_count);
|
||||
struct Input {
|
||||
vector<double> numbers;
|
||||
size_t bin_count{};
|
||||
};
|
||||
|
||||
Input
|
||||
input_data() {
|
||||
size_t number_count;
|
||||
cout << "Enter number count: ";
|
||||
cin >> number_count;
|
||||
Input in;
|
||||
in.numbers.resize(number_count);
|
||||
cerr << "Enter numbers: " << endl;
|
||||
for (int i = 0; i < number_count; i++) {
|
||||
cin >> numbers[i]; // ââîä îöåíîê
|
||||
for (size_t i = 0; i < number_count; i++) {
|
||||
cin >> in.numbers[i];
|
||||
}
|
||||
cerr << "Enter bin count: ";
|
||||
cin >> bin_count; // ââîä êîë-âà êîðçèí
|
||||
vector<size_t> bins(bin_count);
|
||||
for (int i = 0; i < bin_count; i++) {
|
||||
bins[i] = 0;
|
||||
}
|
||||
double min = 5;
|
||||
double max = 0;
|
||||
for (int i = 0; i < number_count; i++) { // îïðåäåëåíèå ìàêñ è ìèí îöåíîê
|
||||
if (numbers[i] > max) {
|
||||
max = numbers[i];
|
||||
}
|
||||
if (numbers[i] < min) {
|
||||
min = numbers[i];
|
||||
}
|
||||
}
|
||||
double bin_size = (max - min) / bin_count; // îïðåäåëåíèå ðàçìåðà êîðçèíû è øàãà
|
||||
double step = bin_size;
|
||||
cin >> in.bin_count;
|
||||
return in;
|
||||
}
|
||||
|
||||
for (int i = 0; i < number_count; i++) {
|
||||
bool found = false; // óñëîâèå âõîæäåíèÿ â êàêîé-ëèáî ïðîìåæóòîê, êðîìå ïîñëåäíåãî
|
||||
for (int j = 0; (j < bin_count - 1) && !found; j++) {
|
||||
double lo = min + j * bin_size; // íèæíÿÿ ãðàíèöà ïðîìåæóòêà
|
||||
double hi = min + (j + 1) * bin_size; // âåðõíÿÿ ãðàíèöà ïðîìåæóòêà
|
||||
if ((lo <= numbers[i]) && (numbers[i] < hi)) { // ïðîâåðêà óñëîâèÿ âõîæäåíèÿ ÷èñëà â ïðîìåæóòîê
|
||||
bins[j]++;
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
bins[bin_count - 1]++;
|
||||
}
|
||||
}
|
||||
|
||||
double max_bin = bins[0];
|
||||
void show_histogram(const vector<double> bins, size_t bin_count){
|
||||
double max_bin = bins[0];
|
||||
int k;
|
||||
for (int i = 0; i < bin_count; i++) { // íàõîæäåíèå ìàêñ çíà÷åíèÿ bin[]
|
||||
for (size_t i = 0; i < bin_count; i++) { // íàõîæäåíèå ìàêñ çíà÷åíèÿ bin[]
|
||||
if (bins[i] > max_bin) {
|
||||
max_bin = bins[i];
|
||||
}
|
||||
@@ -66,7 +45,7 @@ int main()
|
||||
|
||||
cerr << endl;
|
||||
int out;
|
||||
for (int i = 0; i < bin_count; i++) {
|
||||
for (size_t i = 0; i < bin_count; i++) {
|
||||
cout.width(3); // âûðàâíèâàíèå
|
||||
cout.fill(' ');
|
||||
cout << bins[i] << "|";
|
||||
@@ -81,5 +60,13 @@ int main()
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
auto in = input_data();
|
||||
auto bins = make_histogram(in.numbers, in.bin_count);
|
||||
show_histogram(bins, in.bin_count);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user