向量没有输出到控制台

问题描述 投票:0回答:1

我的问题是,在用自然数替换向量中的几乎所有零之后,程序停止,而不会导致程序或任何输出代码崩溃。经过几次循环迭代后,我的向量没有显示在屏幕上,而没有经过循环到底。

这是一个显示另一个程序输出的控制台。这里一切都以代码 0 正确结束。 示例

这就是程序给我的。好像还没完。我不明白为什么它不退出一些代码。 问题

我尝试使用调试器,但它没有捕获崩溃 ChatGPT 无法解决我的问题。 代码:

#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;

}
c++ vector console-application
1个回答
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);

如果对于每个

x
y
你的
N_N[x][y] != 0
会发生什么?你这里有一个无限循环。

© www.soinside.com 2019 - 2024. All rights reserved.