My problem is that after replacing almost all the zeros in the vector with natural numbers, the program stops without crashing the program or any output code. My vector is not displayed on screen after several iterations of the loop without going through the loop to the end.
This is a console that displays the output of another program. Here everything ended correctly with code 0.
This is what the program gives me. It's like it's not finished. I don't understand why it doesn't exit with some code.
I tried to use Debugger, but it doesn't catch the crash ChatGPT could not solve my problem.
Code:
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <chrono>
#include <thread>
using namespace std;
int main() {
setlocale(LC_ALL, "ru");
srand(static_cast<unsigned int>(time(NULL)));
int delayInSeconds = 1; // Delay in seconds
int D = 20;
int T = 14;
int K1 = 4;
int K2 = 8;
int N = 8;
int k;
int probability;
int x, y;
vector<vector<int>> N_N(N, vector<int>(N, 0)); // field
for (int i = 0; i < D; i++) {
// Every day we increase the number of days lived in each bact by 1
for (int f = 0; f < N; f++) { for every cell of the field
for (int l = 0; l < N; l++) {
if (N_N[f][l] != 0) // if there is a bacterium
N_N[f][l] = N_N[f][l] + 1; // increase its number of days lived by 1
}
}
//adding k bacteria
k = rand() % (K2 - K1) + K1;
for (int j = 0; j < k; j++) { // for every k
bool added = false;
do { // iterate over coordinates until all bacteria can be inserted
x = rand() % N;
y = rand() % N;
if (N_N[x][y] == 0) {
N_N[x][y] = 1;
added = true;
}
} while (!added);
}
//simulation of bacterial death
for (int a = 0; a < N; a++) { //for every cell of the field
for (int b = 0; b < N; b++) {
if (N_N[a][b] >= T / 2) { // if the life of the bacterium is greater than T:2
probability = rand() % 100; // probability
if (probability <= 2) // if probability < 2%
N_N[a][b] = 0; // the bacterium dies and the cell is released
}
}
}
// simulation of bacterial reproduction
if (i > 0) { // "next and every day"
for (int c = 0; c < N; c++) {
for (int d = 0; d < N; d++) {
if (N_N[c][d] <= T) { // "after T days after appearance, the bacterium stops reproducing"
probability = rand() % 100; // probability of occurrence for each bacterium
if (probability <= 5 && c < N && d + 1 < N){ // if it is less than 5% and if the coordinates do not go beyond the field
if (N_N[c][d + 1] == 0) // if the cell is empty
N_N[c][d + 1] = 1; // birth of bacteria
}
probability = rand() % 100;
if (probability <= 5 && c - 1 >= 0 && d + 1 < N) {
if (N_N[c - 1][d + 1] == 0)
N_N[c - 1][d + 1] = 1;
}
probability = rand() % 100;
if (probability <= 5 && c - 1 >= 0 && d < N) {
if (N_N[c - 1][d] == 0)
N_N[c - 1][d] = 1;
}
probability = rand() % 100;
if (probability <= 5 && c - 1 >= 0 && d - 1 >= 0) {
if (N_N[c - 1][d - 1] == 0)
N_N[c - 1][d - 1] = 1;
}
probability = rand() % 100;
if (probability <= 5 && c < N && d - 1 >= 0) {
if (N_N[c][d - 1] == 0)
N_N[c][d - 1] = 1;
}
probability = rand() % 100;
if (probability <= 5 && c + 1 < N && d - 1 >= 0) {
if (N_N[c + 1][d - 1] == 0)
N_N[c + 1][d - 1] = 1;
}
probability = rand() % 100;
if (probability <= 5 && c + 1 < N && d < N) {
if (N_N[c + 1][d] == 0)
N_N[c + 1][d] = 1;
}
probability = rand() % 100;
if (probability <= 5 && c + 1 < N && d + 1 < N) {
if (N_N[c + 1][d + 1] == 0)
N_N[c + 1][d + 1] = 1;
}
}
}
}
}
// output on display
for (int e = 0; e < N; e++) {
for (int f = 0; f < N; f++) {
if (N_N[e][f] == 0)
cout << " " << " ";
if (N_N[e][f] != 0)
cout << N_N[e][f] << " ";
}
cout << endl;
}
//this_thread::sleep_for(chrono::seconds(1));
/*auto start = chrono::steady_clock::now();
while (chrono::duration_cast<chrono::seconds>(chrono::steady_clock::now() - start).count() < delayInSeconds) {}*/
/*if (i < D - 1)
system("cls");*/
}
return 0;
}
bool added = false;
do { // iterate over coordinates until all bacteria can be inserted
x = rand() % N;
y = rand() % N;
if (N_N[x][y] == 0) {
N_N[x][y] = 1;
added = true;
}
} while (!added);
What will happen if for every x
and y
your N_N[x][y] != 0
? You got an infinite loop here.