Search code examples
c++arraysmultidimensional-arrayprintingdynamic-memory-allocation

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


I've written this code down here but my problem is whenever i hit the run button and execute my program the values are fine but every element of the 2D array is printed out in a separate line, it's not printing a square with area its dimensions (size n x n) like i wanted, how can i fix this?

my code

it should be like this 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;
}

Solution

  • You placed the line

    cout << "]" << endl;
    

    at a wrong place. It should be after the inner loop, not 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;
    }