打印动态二维数组的问题(执行问题)

Problem with printing a dynamic 2d array (execution problem)

我已经在这里写下了这段代码,但我的问题是每当我点击 运行 按钮并执行我的程序时,这些值都很好,但是二维数组的每个元素都在单独的一行中打印出来,它没有像我想要的那样打印一个区域尺寸(大小 n x n)的正方形,我该如何解决这个问题?

my code

应该是这样的what i want it to look like

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int countingNumOfMs(char** surface, int rows, int cols, int dimensions)
{
    int counter = 0;
    int i, j = 0;
    for (i = (rows - 1); i <= (rows + 1); i++) {
        for (j = (cols - 1); j <= (cols + 1); j++) {
            if (i < 0 || i >= dimensions || j < 0 || j >= dimensions)
                continue;

            else if (i == rows && j == cols)
                continue;
            else if (surface[i][j] == 'M')
                counter++;
        }
    }
    return counter;
}

bool turnIntoM(char** surface, int rows, int cols, int dimensions)
{
    int microOrganismCount = countingNumOfMs(surface, rows, cols, dimensions);
    if (surface[rows][cols] == 'M') {
        if (microOrganismCount != 2 && microOrganismCount != 3)
            return false;
    }
    else {
        if (microOrganismCount == 3)
            return true;
    }
}

void nextSurface(char** surface, int dimensions)
{
    int rows, cols;
    char temp[dimensions][dimensions];

    for (rows = 0; rows < dimensions; rows++) {
        for (cols = 0; cols < dimensions; cols++) {
            if (turnIntoM(surface, rows, cols, dimensions)) {
                temp[rows][cols] = 'M';
            }
            else {
                temp[rows][cols] = '-';
            }
        }
    }

    for (rows = 0; rows < dimensions; rows++) {
        for (cols = 0; cols < dimensions; cols++) {
            surface[rows][cols] = temp[rows][cols];
        }
    }
}

void showCurrentIteration(char** surface, int dimensions)
{
    int rows, cols;
    for (rows = 0; rows < dimensions; rows++) {
        cout << "[";
        for (cols = 0; cols < dimensions; cols++) {
            cout << surface[rows][cols];
            cout << "]" << endl;
        }
    }
    cout << endl;
}

char** makeAnIteration(int dimensions)
{
    srand(time(0));

    char** surface = new char*[dimensions];

    int rows, cols;
    for (rows = 0; rows < dimensions; rows++)

        surface[rows] = new char[dimensions];

    int counter = 0;

    for (rows = 0; rows < dimensions; rows++) {
        for (cols = 0; cols < dimensions; cols++)

            surface[rows][cols] = '-';
    }

    while (counter < (2 * dimensions)) {
        rows = rand() % dimensions;
        cols = rand() % dimensions;
        if (surface[rows][cols] == 'M')
            continue;

        surface[rows][cols] = 'M';
        counter++;
    }

    return surface;
}

int main()
{
    int dimensions;
    cout << "Enter dimensions of the surface in size of (n x n) \nEnter the value of n: " << endl;
    cin >> dimensions;
    char** surface = makeAnIteration(dimensions);

    cout << "Enter the number of iterations: " << endl;
    int iterations;
    cin >> iterations;

    for (int i = 0; i < iterations; i++) {
        showCurrentIteration(surface, dimensions);
        nextSurface(surface, dimensions);
    }

    return 0;
}

您放置了线

cout << "]" << endl;

放错地方了。它应该是 after 内部循环,而不是 inside that.

void showCurrentIteration(char **surface, int dimensions)
{
    int rows, cols;
    for (rows = 0; rows < dimensions; rows++)
    {
        cout << "[";
        for (cols = 0; cols < dimensions; cols++)
        {
            cout << surface[rows][cols];
            // wrong place
            // cout << "]" << endl;
        }
        // it should be here
        cout << "]" << endl;
    }
    cout << endl;
}