-3

I am looking to find out if I initialized this array correctly because when I display it it does not display what I thought it should display. Here is the code:

 #include <iostream>
    #include <string.h>
    void Pause()
    {
        std::cin.get();
        std::cin.ignore();
    }

    int main() 
    {
        int n = 0;
        int size;

        std::cout << "Enter what width you want the board to be: ";
        std::cin >> size;

        int **p_p_Board;
        p_p_Board = new int*[size];

        //Loops for initializing array
        for (int i = 0; i < size; i++) 
        {
            p_p_Board[i] = new int[size]; 
        }
            for (int i = 0; i < size; i++)
        {
            for (int j = 0; i < size; i++)
            {
                //Initializing Array
                p_p_Board[i][j] = 0;
            }
        }

        //Display loops
        for (int i = 0; i < size; i++)
        {
            for (int j = 0; j < size; j++)
            {
                //Displaying the array
                std::cout << p_p_Board[i][j];
                n++;
               if (n == size)
                {
                    std::cout << std::endl;
                    n = 0;
                }
            }
        }

        Pause();

        for (int i = 0; i < size; i++)
        {
            delete [] p_p_Board[i];
        }
        delete p_p_Board;
    }

And here is the display:

Enter what width you want the board to be: 8
0-842150451-842150451-842150451-842150451-842150451-842150451-842150451
0-842150451-842150451-842150451-842150451-842150451-842150451-842150451
0-842150451-842150451-842150451-842150451-842150451-842150451-842150451
0-842150451-842150451-842150451-842150451-842150451-842150451-842150451
0-842150451-842150451-842150451-842150451-842150451-842150451-842150451
0-842150451-842150451-842150451-842150451-842150451-842150451-842150451
0-842150451-842150451-842150451-842150451-842150451-842150451-842150451
0-842150451-842150451-842150451-842150451-842150451-842150451-842150451

As you can see it prints the number -842150451 after the first number in each row. While the first number in each row is correctly set to 0, the rest are not. Is this correct or do I have an error in my code?

Areos
  • 1

2 Answers2

3

for (int j = 0; i < size; i++)

should be

for (int j = 0; j < size; j++)

but your initialisation code could be mostly omitted if you used std::vector rather than raw pointers:

std::vector<std::vector<int>> board(size);
for (auto& row : board)
{
  row.resize(size);
}

You'd also then be able to omit the deletes and your code would be much less likely to leak memory.

Your display code can be simplified too:

for (const auto& row : board)
{
    for (int element : row)
    {
        std::cout << element;
    }
    std::cout << "\n";
}
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
0

Small typo:

for (int j = 0; i < size; i++)
//              ^         ^^^ should be 'j'

You can also set the value to 0 in one loop using std::fill_n:

int **p_p_Board = new int*[size];
for (int i = 0; i != size; ++i) {
    p_p_Board[i] = new int[size];  
    std::fill_n(p_p_Board[i], size, 0);
}

Or use vector as Alan suggests

Andreas DM
  • 10,685
  • 6
  • 35
  • 62