Reset 2D Array

So I need to reset an array so that all its elements end up with the int 0. However my reset_board() seems to crash the program.

What am I doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
 #include <iostream>
#include <string>
#include <array>
#include <cstdlib>
#include <ctime>
using namespace std;

void draw_board(int matrix[6][7])
{
    cout <<"\n1|2|3|4|5|6|7\n"
         <<"-------------\n";
     for(int i = 0; i < 6; ++i)
     {
        for(int j = 0; j < 7; ++j)
        {
            cout << matrix[i][j] << " ";
        }
        cout << "\n";
     }
     cout << "\n";
}

void reset_board(int matrix[6][7])
{
    for (int i = 0; i < 6; ++i)
    {
        for (int j = 0; j < 7; ++i)
        {
            matrix[i][j] = 0;
        }
    }
}

int main()
{
    int matrix[6][7] =
    {
         {0,0,0,0,0,0,0},
         {0,0,0,0,0,0,0},
         {0,0,0,0,0,0,0},
         {0,0,0,0,0,0,0},
         {0,0,0,0,0,0,0},
         {0,0,0,0,0,0,0}
    };

    draw_board(matrix);

    matrix[5][5] = 7;

    draw_board(matrix);

    reset_board(matrix);

    draw_board(matrix);
}
Take a closer look at your second for loop in the reset_board function.
for (int j = 0; j < 7; ++i) ← HEre is the problem
Last edited on
Thanks.
Topic archived. No new replies allowed.