2D Array Help

I need help writing a neighbor count function for my program, these are the guidelines:
neighborCount:Given two arguments that indicate the row and column of a particular cell in the matrix, this function returns the number of neighbors that have the value "true". Most positions in the grid have 8 neighbors like the center square in a tic-tac-toe game. The four corner positions have only 3 neighbors each. The remaining positions around the edge of the grid have 5 neighbors each. This function must use assert to exit the program if the row or column is out-of-bounds.

Additional neighborCount() Requirement: In this function you must use your "get()" function to access the matrix, instead of accessing your 2D array data member directly. So, if your data member is named "m", you'll say "get(row, col)" instead of "m[row][col]". This will be a safer programming practice, since the get() function will do range checking for you (i.e., it will make sure that row and col are not out-of-bounds of the 2D array).

Heres what I have so far:

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
 #include <iostream>
#include <cassert>
using namespace std;

class boolMatrix {
public:
    static const int NUM_ROWS = 20;
    static const int NUM_COLS = 20;
    boolMatrix();
    bool get(int row, int col) const;
    void set(int row, int col, bool value);
    int rowCount(int row) const;
    int colCount(int col) const;
    int totalCount() const;
    void print() const;
    
private:
    bool array[NUM_ROWS][NUM_COLS];
    
};

bool boolMatrix::get(int row, int col) const
{
    assert(row >= 0 && row < NUM_ROWS - 1);
    assert(col >= 0 && col < NUM_COLS - 1);
    return array[row][col];
}



boolMatrix::boolMatrix()
{
    for (int row = 0; row < NUM_ROWS; row++){
        for (int col = 0; col < NUM_COLS; col++){
            array[row][col] = false;
        }
    }
    
}

void boolMatrix::set(int row, int col, bool value)
{
   
   
    array[row][col] = value;
}

int boolMatrix::rowCount(int row) const
{
    assert(row >= 0 && row < NUM_ROWS - 1);
    
    int rowtotal = 0;
    for (int col = 0; col < NUM_COLS; col++){
        if (array[row][col] == true){
            rowtotal++;
        }
    }
    return rowtotal;
}

int boolMatrix::colCount(int col) const
{
    
    assert(col >= 0 && col < NUM_COLS - 1);
    int coltotal = 0;
    for (int row = 0; row < NUM_ROWS; row++){
        if (array[row][col] == true){
            coltotal++;
        }
    }
    return coltotal;
}

int boolMatrix::totalCount() const
{
    int total = 0;
    for (int row = 0; row < NUM_ROWS; row++){
        for (int col = 0; col < NUM_COLS; col++){
            if (array[row][col] == true){
                total++;
            }
        }
    }
    return total;
}


void boolMatrix::print() const
{
    cout << "  ";
    for (int col = 0; col < NUM_COLS; col++)
    {
        cout << col % 10;
    }
    cout << endl;
    for (int row = 0; row < NUM_ROWS; row++)
    {
        cout << "  " << row % 100;
        for (int col = 0; col < NUM_COLS; col++){
            if ( array[row][col] == true ){
                cout << "*";
            } else if ( array[row][col] == false ){
                cout << " ";
            }
        }
        cout << endl;
    }
}


#include <iostream>

using namespace std;

int main() {
    boolMatrix matrix1;
    
    for (int i = 0; i < 50; i++) {
        matrix1.set(rand() % 20, rand() % 20, true);
    }
    
    matrix1.print();
    cout << endl;
    cout << matrix1.rowCount(10) << endl;
    cout << matrix1.colCount(10) << endl;
    cout << matrix1.totalCount() << endl;
    
    for (int row = 0; row < boolMatrix::NUM_ROWS; row++) {
        for (int col = 0; col < boolMatrix::NUM_COLS; col++) {
            
        }
        cout << endl;
    }
}
What is the problem? What kind of help do you need?
Topic archived. No new replies allowed.