Percolation/Mean cluster density: Difference between revisions

m
Made code more C++ idiomatic.
m (Minor formatting.)
m (Made code more C++ idiomatic.)
Line 224:
#include <string>
#include <vector>
#include <iomanip>
 
std::random_device random;
Line 245 ⟶ 246:
 
void display() const {
for ( uint64_t row = 0; row < grid.size(); row++row ) {
for ( uint64_t col = 0; col < grid.size(); col++col ) {
uint64_t value = grid[row][col];
char ch = ( value < GRID_CHARACTERS.length() ) ? GRID_CHARACTERS[value] : '?';
Line 257 ⟶ 258:
void count_clusters() {
clusters = 0;
for ( uint64_t row = 0; row < grid.size(); row++row ) {
for ( uint64_t col = 0; col < grid.size(); col++col ) {
if ( grid[row][col] == CLUSTERED ) {
clusters += 1;
Line 285 ⟶ 286:
void create_grid(int32_t grid_size, double probability) {
grid.assign(grid_size, std::vector<int32_t>(grid_size, 0));
for ( int32_t row = 0; row < grid_size; row++row ) {
for ( int32_t col = 0; col < grid_size; col++col ) {
if ( distribution(generator) < probability ) {
grid[row][col] = CLUSTERED;
Line 307 ⟶ 308:
 
Grid grid(size, probability);
std::cout << "This " << size << " by " << size << " grid contains "
printf("%s%d%s%d%s%d%s%s",
"This ", size, " by ", size, " grid contains ",<< grid.cluster_count(), << " clusters:", "\n")<< std::endl;
grid.display();
 
printf("%s%s%d%s",std::cout << "\n", " p = 0.5, iterations = ", << test_count, "\n")<< std::endl;
std::vector<int32_t> gridSizesgrid_sizes = { 10, 100, 1'000, 10'000 };
for ( int32_t gridSizegrid_size : gridSizesgrid_sizes ) {
double sumDensity = 0.0;
for ( int32_t test = 0; test < test_count; test++ ) {
Grid grid(gridSizegrid_size, probability);
sumDensity += grid.cluster_density();
}
double result = sumDensity / test_count;
std::cout << " n = " << std::setw(5) << grid_size
printf("%s%5d%s%.6f%s", " n = ", gridSize, ", simulation K = ", result, "\n");
<< ", simulations K = " << std::fixed << result << std::endl;
}
}
908

edits