I need help inserting the assert function within my code, and creating the neighborcount function as listed below, heres the guidelines:

default constructor: initialize all of the array elements to false
get: return the current contents of a single array element. Use arguments to indicate the row and column of the array element that should be returned. This function must use assert to exit the program if the row or column is out-of-bounds.
set: set the contents of a single array element. Use arguments to indicate the row and column of the array element that should be set, and the value to which it should be set. This function must use assert to exit the program if the row or column is out-of-bounds.
rowCount: return the number of true values that exist in any given row. This function must use assert to exit the program if the row is out-of-bounds.
colCount: return the number of true values that exist in any given column. This function must use assert to exit the program if the column is out-of-bounds.
totalCount: return the number of true values that exist in the entire array.
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).

print: display the contents of the array, including the row and column indices. For each element of the array, a true value must be displayed as an asterisk ("*") and a false value must be displayed as a space. This member function is the only one that displays output. It should be formatted as demonstrated here. Make sure that the row and column labels still work correctly if the constants (NUM_ROWS and NUM_COLS) are set to something different, say, 30 instead of 20.

Heres my code:

#include <iostream>
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
{
    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
{
    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
{
    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;
    }
}

You'll need to #include the cassert library.

http://www.cplusplus.com/reference/cassert/

A valid row index will be 0 <= row < NUM_ROWS.

So if a function is passed a parameter called row, you can stick this line at the top of the function:

assert(row >= 0 && row < NUM_ROWS);

For columns, substitute col for row and NUM_COLS for NUM_ROWS in the line above.

Ok thanks, got that taken care of, any idea the code I need for the neighborCount?

You can use your totalCount function as a template, as far as a loop within a loop where the row and column changes. It'll be the same functions except...

  1. The indexes won't range from 0 to NUM_COLS-1 or NUM_ROWS-1. They will range from max(0, row-1) to min(row+1, NUM_ROWS-1). Ditto with the columns.
  2. Use your get function instead of the brackets to access the matrix
  3. Don't count "yourself" i.e. the (row, col) entry is not a "neighbor". So take the totalCount function...

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

Change it to your neighborCount spec...

int boolMatrix::neighborCount(int row, int col) 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; 
}

Change it around a little bit...

int boolMatrix::neighborCount(int row, int col) const
{
    int total = 0;
    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 */

    // rename row and col since you already have them
    for (int i = row_start; i <= row_end; i++){
        for (int j = col_start; j <= col_end; j++){
             // change the if criteria to use get instead of brackets
             // also change the if criteria to make sure you don't add "yourself"
            if (array[i][j] == true){
                total++;
            }
        }
    }
    return total;
}

For #1, you can write a quick max and min function, you can do a quick if/then statement or you can use the ? operator, like so:

int row_start = (row <= 0 ? 0 : row-1);

which is the equivalent of...

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

As far as making sure that you don't count yourself, compare i and j to row and col, so...

            if(i == row && j == col) // this is "myself"
                continue; // skip this check
            if (array[i][j] == true)
                total++;

Last but not least, get rid of the array[i][j] and use your get function instead.

Edited 4 Months Ago by AssertNull: Code not being detected

Thanks, heres what I have when putting them all together, is this correct?

int boolMatrix::neighborCount(int row, int col) const
{
    int total = 0;
    int row_start = row - 1;
    int row_end = row + 1;
    int col_start = col - 1;
    int col_end = col + 1;

    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 (array[i][j] == true)
                total++;
            }
        }

    return total;
}

When I compile this all its returning is a bunch of jumbled numbers, garbage basically, can you refine what you wrote to me or something? Not making sense....

is this correct?

You tell me. Does it give the right answers when you compile and run it with a bunch of different values, including when row is equal to 0? See prior post...

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

You used the first line but not the second and third lines.

If you haven't written the "get" function yet, write it. Then see line 14 of your code...

if (array[i][j] == true)

Now look at your spec. Does your code follow the spec?

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).

Edited 4 Months Ago by AssertNull: Fixed Markup

The get function is line 21 of the code I posted.

OK, didn't see that. My point is that you aren't calling it. Line 14 has the brackets in it.

When I compile this all its returning is a bunch of jumbled numbers, garbage basically,

If there's an error, you should get an error message. Look at the very top of the output. Lots of times one error will cause a whole bunch more. Look at the very first error message and ignore the rest.

can you refine what you wrote to me or something?

Post your updated code that did not compile.

Heres the output of what im running:

  01234567890123456789
  0     *      **      
  1             *      
  2      *             
  3         *      *   
  4                * * 
  5  * **   *    *     
  6                    
  7   *     *  *       
  8   **     ** *      
  9 *  *            *  
  10   *        **      
  11                  * 
  12  *   **            
  13                * * 
  14                 *  
  15             **     
  16               * *  
  17      * *      *    
  18 *  *               
  19 *       *        * 

3
1
47
00000399000000399399000000
0000000000000399000000
0000003790000000000000
000000000359000000359000
000000000000000033903390
003190319319000319000031900000
00000000000000000000
00027900000279002790000000
000259259000002592590259000000
02390023900000000000023900
00021900000000219219000000
0000000000000000001990
00179000179179000000000000
000000000000000015901590
0000000000000000013900
000000000000011911900000
0000000000000009909900
00000079079000000790000
0590059000000000000000
03900000003900000000390
Program ended with exit code: 0

It compiles and runs, it just doesnt output what its supposed to, its supposed to return the number of neighbors that have the value, true. I don't think its supposed to be all those numbers

heres the code:

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 = NUM_ROWS - 1;

    int col_start = 0;

    int col_end = NUM_COLS - 1;

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

Ok, will do, thanks

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

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 = NUM_ROWS - 1;

    int col_start = 0;

    int col_end = NUM_COLS - 1;

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

OK, Last post from me. Gotta move on. You might want to start a new updated thread.

You appear to be misinterpreting some of my posts and copy/pasting without stopping and thinking and looking at your code. You get the weird output because of lines 153 to 158. I told you to test out your program by throwing a bunch of values at neighborCount. But I meant do it one at a time, then erase that code.

Here is what you need to do in order to test your code. First, you need to fill the array in NON-randomly, so get rid of the rand() call and hard-code in some true/false values for the matrix. And possibly reduce the size of the array. When stuff starts working, put back in the randomization and increase the array size. For example, the easiest matrix to test is all false or all true. Figure out what the printout should be before you run the program. Is it that?

Now, you can never have 9 neighbors, it should always be 0 through 8. You have 9's in your printout. First one to check is (0,0). Its value should be at most 3 since it has three neighbors. Is it? Now go through your code with row = 0 and col = 0. row_start should be 0, col_start should be 0, row_end should be 1, col_end should be 1. Are they?

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 = NUM_ROWS - 1;

    int col_start = 0;

    int col_end = NUM_COLS - 1;

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

No, row_end is NUM_ROWS - 1 = 19. It's supposed to be the minimum of 19 and 1, so 1. You copied and pasted my code for row_start, which was a MODEL for row_end, col_start, and col_end. You left them as-is. Take my row_start code (3 lines) and change the row_end, col_start, and col_end code. 4 sets of 3 lines each, 3 lines for each of 4 variables, so 12 lines to set row_start, row_end, col_start, col_end.

Now, look at your assert statments. Pass your rowCount function the parameters 19. 10 is a bad test case. You want to pass the edge cases: -1, 0, 19, 20. The first and last should abort the program. The middle two should work. Do they?

  1. Hard-code a matrix that is easy to test as well as good test cases taht test the code. Get rid of the random statements for now.
  2. Figure out on paper what every function should display for these tests.
  3. Does the program execute correctly? Is the display correct?
  4. If not, go through the function line by line and figure out what went wrong. Correct code. Repeat steps 1 to 3.
  5. If everything worked, throw a few more test cases at it. When they all work, you're done.

Good luck.

Everything seems to be working fine except for the neighborcount function. Anyhow thank you for your help.

This question has already been answered. Start a new discussion instead.