1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
|
#include <chrono>
#include <fstream>
#include <iostream>
#include <random>
#include <valarray>
struct Coord3d {
int x {};
int y {};
int z {};
Coord3d() = default;
constexpr Coord3d(int x_arg, int y_arg, int z_arg);
/*explicit*/ operator int() const;
int getIndex(int x_arg, int y_arg, int z_arg) const;
int getIndex(const Coord3d& rhs) const;
};
// TODO: check if negative values
constexpr Coord3d::Coord3d(int x_arg, int y_arg, int z_arg)
: x { x_arg }
, y { y_arg }
, z { z_arg }
{
}
int Coord3d::getIndex(int x_arg, int y_arg, int z_arg) const
{
return z_arg * x * y + y_arg * x + x_arg;
}
int Coord3d::getIndex(const Coord3d& rhs) const
{
return getIndex(rhs.x, rhs.y, rhs.z);
}
Coord3d::operator int() const
{
return x * y * z;
}
// ============================================================================
// For debugging:
void print3dArrOnFile(const std::valarray<double>& arr3d, const Coord3d& coord);
void insertCriticalElem( std::valarray<double>& arr3d,
const Coord3d& arr_coord,
double critical,
const Coord3d& crit_coord );
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
inline void clearScreen();
void fillArrWithInitValues( std::valarray<double>& arr3d,
const Coord3d& coord );
double getRndDouble(double min, double max);
void fillArrWithRndDouble(std::valarray<double>& arr3d);
void divideAllElemsBy100(std::valarray<double>& arr3d);
Coord3d findCriticalElem( const std::valarray<double>& arr3d,
double critical,
const Coord3d& coord );
int main()
{
constexpr Coord3d coord(10, 300, 300);
std::valarray<double> arr3d( coord );
// ========================================================================
// Step - 1 -
// Give arr3d initial values:
fillArrWithInitValues(arr3d, coord);
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// print3dArrOnFile(arr3d, Side_x, Side_y, Side_z); // just debug
// clearScreen();
// ========================================================================
// Step - 2 -
// Discard the freshly given initial values in favor of random values:
fillArrWithRndDouble(arr3d);
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// ========================================================================
// Step - 3 -
// Divide the freshly given random values by 100:
divideAllElemsBy100(arr3d);
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Debug: insert critical element on purpose:
// REMOVE WHEN DEBUGGED!!
insertCriticalElem(arr3d, coord, 7.0, Coord3d(5, 150, 150));
// ========================================================================
// Step - 3 -
// Verify if any element is beyond value 6:
constexpr double vcrit { 6.0 };
Coord3d found { findCriticalElem(arr3d, vcrit, coord) };
if (found.x != -1) {
std::cout << "Critical element found at position ("
<< found.x << ", "
<< found.y << ", "
<< found.z << "). Value: "
<< arr3d[ found.z * coord.x * coord.y
+ found.y * coord.x
+ found.x]
<< '\n';
}
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// do {} while (ite < itMax); is hard to read
constexpr int Max_Iterations { 100 };
for(int loop {}; loop < Max_Iterations; ++loop) {
// ...
}
}
void print3dArrOnFile(const std::valarray<double>& arr3d, const Coord3d& coord)
{
std::ofstream fout("mydebug.txt");
if (!fout) {
std::cerr << "Cannot open 'mydebug.txt' fro writing.\nExiting now.\n";
return;
}
for (int z {}; z < coord.z; ++z) {
for (int y {}; y < coord.y; ++y) {
fout << "- - -\n";
for (int x {}; x < coord.x; ++x) {
fout << arr3d[coord.getIndex(x, y, z)] << ' ';
}
fout << '\n';
}
fout << '\n';
}
fout << '\n';
}
void insertCriticalElem( std::valarray<double>& arr3d,
const Coord3d& arr_coord,
double critical,
const Coord3d& crit_coord )
{
arr3d[arr_coord.getIndex(crit_coord)] = critical;
}
void clearScreen()
{
for (int i{}; i < 50; ++i) { std::cout << '\n'; }
}
void fillArrWithInitValues( std::valarray<double>& arr3d,
const Coord3d& coord )
{
for (int z {}; z < coord.z; ++z) {
for (int y {}; y < coord.y; ++y) {
for (int x {}; x < coord.x; ++x) {
arr3d[coord.getIndex(x, y, z)] = 0.1 * x + 0.1 * y + 0.01 * z;
}
}
}
}
double getRndDouble(double min, double max)
{
static std::mt19937 eng {
static_cast<unsigned>(
std::chrono::high_resolution_clock::now().time_since_epoch().count()
)
};
std::uniform_real_distribution<> dst(min, max);
return dst(eng);
}
void fillArrWithRndDouble(std::valarray<double>& arr3d)
{
arr3d.apply( [](double) {
return getRndDouble(0.0, 5.99);
} );
}
void divideAllElemsBy100(std::valarray<double>& arr3d)
{
arr3d /= 100;
}
Coord3d findCriticalElem( const std::valarray<double>& arr3d,
double critical,
const Coord3d& coord )
{
Coord3d found(-1, -1, -1);
for (int z {}; z < coord.z; ++z) {
for (int y {}; y < coord.y; ++y) {
for (int x {}; x < coord.x; ++x) {
// For readability:
int index { coord.getIndex(x, y, z) };
if ( arr3d[index] >= critical ) {
found.x = x;
found.y = y;
found.z = z;
return found;
}
}
}
}
return found;
}
|