Skip to content

Instantly share code, notes, and snippets.

@ProfAndreaPollini
Created January 31, 2025 06:17
Show Gist options
  • Save ProfAndreaPollini/1903ece37a70836eca15a55ea2bbf291 to your computer and use it in GitHub Desktop.
Save ProfAndreaPollini/1903ece37a70836eca15a55ea2bbf291 to your computer and use it in GitHub Desktop.
intermometro -2 (30/1/2025)
// aggiunta della rappresentazione con asterischi e della gestione di distribuzioni diverse, utilizzando <random>
// INTERMOMETRO
//
// date N misurazioni interere di un intermometro
// rappresentare con una adeguata struttura dati tali misurazioni
// - ricavare temperatura minima, massima e media del dataset delle misurazioni
// - ricavare un istogramma delle stesse sia in forma testuale che grafica
#include <iostream>
#include <vector>
#include <algorithm>
struct valore {
int x;
float y;
};
using func = std::vector<valore>;
int has_x(const func& f,int val) {
for (int i = 0; i < f.size(); ++i) {
if (f[i].x == val) {
return i;
}
}
return -1;
}
void add_value(func& f,int val) {
auto pos = has_x(f,val);
if (pos == -1) {
f.push_back({val,1});
} else {
f[pos].y++;
}
}
std::vector<int> get_x(const func&f) {
std::vector<int> out;
for (int i = 0; i < f.size(); ++i) {
out.push_back(f[i].x);
}
return out;
}
func to_percentuali(const func& f) {
func out;
//calcolo il numero di elementi in f
int sum = 0;
for (int i = 0; i < f.size(); ++i) {
sum += f[i].y;
}
for (int i = 0; i < f.size(); ++i) {
auto x = f[i].x;
auto y = f[i].y;
valore v = {x, y / sum};
out.push_back(v);
}
return out;
}
void stampa_percentuali(func f) {
for(int i = 0;i <100;i++) {
auto pos = has_x(f,i);
if (pos == -1) {
std::cout << i << std::endl;
} else {
auto y = f[pos].y * 120;
std::cout << i << "\t";
for(int j = 0; j < y; j++) {
std::cout << "*";
}
std::cout << std::endl;
}
}
}
#include <random>
int random(int a, int b) {
std::random_device rd;
std::mt19937 rng(rd());
std::normal_distribution<float> dist(50.0, 1.5);
return int(dist(rng));
}
int main() {
std::vector<int> misurazioni = {4,1, 2, 2, 4, 3, 3, 7, 3, 1, 2};
func istogramma;
for(auto v:misurazioni) {
add_value(istogramma,v);
}
for(const auto& v:istogramma) {
std::cout << v.x << " " << v.y << std::endl;
}
auto x = get_x(istogramma);
std::ranges::sort(x);
std::cout << " ORDINATA\n";
for(auto val_x:x) {
auto pos = has_x(istogramma,val_x);
auto y = istogramma[pos].y;
std::cout << val_x << " " << y << std::endl;
}
for(int i = 0;i <100;i++) {
auto pos = has_x(istogramma,i);
if (pos == -1) {
std::cout << i << std::endl;
} else {
auto y = istogramma[pos].y;
std::cout << i << "\t";
for(int j = 0; j < y; j++) {
std::cout << "❤️";
}
std::cout << std::endl;
}
}
for(int i = 0;i <10000;i++) {
auto nuovo_valore = random(0,100); // creo nuovo valore
add_value(istogramma,nuovo_valore); // lo aggiuno all'istogramma
auto istogramma_percentuali = to_percentuali(istogramma); // calcolo percentuali
if (i % 1000 == 0) {
stampa_percentuali(istogramma_percentuali); // stampo percentuali
std::cout << "\n\n";
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment