I need help getting my neighborcount function to work correctly, as follows
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).

Here's what I have so far:

#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;
    int neighborCount(int row, int col) const;

private:
    bool array[NUM_ROWS][NUM_COLS];

};

bool boolMatrix::get(int row, int col) const
{
    assert(row >= 0 && row < NUM_ROWS);
    assert(col >= 0 && col < NUM_COLS);
    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)
{

    assert(row >= 0 && row < NUM_ROWS );
    assert(col >= 0 && col < NUM_COLS );
    array[row][col] = value;
}

int boolMatrix::rowCount(int row) const
{
    assert(row >= 0 && row < NUM_ROWS);

    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;
    }
}

int boolMatrix::neighborCount(int row, int col) const
{
    int total = 0;

    int row_start = row - 1;
    if(row_start <= 0)
        row_start = 0;

    int row_end = row + 1;
    if(row_end <= 0)
        row_end = 0;
    int col_start = 0;
    if(col_start <= 0)
        col_start = 0;
    int col_end = col + 1;
    if(col_end <= 0)
        col_end = 0;

    for (int i = row_start; i <= row_end; i++){
        for (int j = col_start; j <= col_end; j++){

            if(i == row && j == col)
                continue;
            if (get(row, col) == true)
                total++;
            }
        }

    return total;
}

#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 << matrix1.neighborCount(row, col);
        }
        cout << endl;
    }

}

Can someone tell me if this is all correct?

Hmmmmm. Just submitted a long thoughful post and it went... I don't know where. I'm not typing it all out again. Bummer. It was a good one. OP, stick a space between your neighborCount output so you can see which digits make up which cells on the printout.

cout << matrix1.neighborCount(row, col) << " ";

You can have at most 8 neighbors so none of those cells should be greater than 8. Nothing on an edge should be greater than 5. Nothing on a corner should be more than 3.

I also repost my pseudocode from your last thread. Does your C++ code match it?

int row_start = /* 0 or row-1, whichever is bigger */
int row_end = /* NUM_ROWS-1 or row+1, whichever is smaller */
int col_start = /* 0 or col-1, whichever is bigger */
int col_end = /* NUM_COLS-1 or col+1, whichever is smaller */

Yeah, it all checks out correctomundo homie. Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.