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