c++ - Specific matrix function task -


let's have function send these parameters. reference on matrix m (filled 0 , 1), coordinate x, , coordinate y. f(m,x,y). x , y coordinates of cell in matrix. function has return matrix n 3x3 format. n has filled count of how many 1's around every cell around (x,y). please @ picture in order understand better. picture not know how work way around matrix information. can please me @ least start? :d

matrix class

                class matrix         {         private:             int cols;             int rows;             int **cells;          public:             matrix(const int& x , const int& y);         //constructor             matrix(const matrix& m);                     //copy constructor             int get_at(const int& x, const int& y)const;  //returns value cell[x][y]             void set_at(const int& x, const int& y);      //sets value in cell[x][y]             int get_rows()const;                          //returns number of rows                          int get_columns()const;                       //returns number of columns          };            

question: how cells in matrix m around cell[x][y]

answer: cell[x+i][y+j] i,j can -1 , 0 , 1, except case = j = 0 because thats m[x][y] obviously.

you need consider cell[x][y] can on edge of matrix , example: if x=0 , y=0, cell[x-1][anythig] or cell[anything][y-1] result in error(accessing non existent cell).

this happens if:

  • x+i < 0 || x+i >= m.get_rows() conclusion: x+i non existing row position

  • y+j < 0 || y+j >= m.get_columns() conclusion: y+i non existing column position

function count_ones

the next function counts appearance of 1 in matrix m around m.cell[x][y]

                int count_ones(const matrix& m, const int& x , const int& y)                 {                     int rows    = m.get_rows();                     int columns = m.get_columns();                      int count = 0;              //iterate through cells in matrix m around m.cell[x][y]                           for( int = -1 ; <= 1 ; ++i)                     {                            //check row existence                         if( x + < 0 || x + >= rows )continue;                          for(int j = -1 ; j <= 1 ; ++j)                         {                           //check column existence ,                         //avoid counting  when i==0 && j==0 because thats m.cell[x][y]                            if( y + j = columns || ( i==0 && j==0 ) )continue;                            if(m.get_at(x+i,y+j) == 1 )++count;//checks m.cell[x+i][y+j]== 1                          }                      }                      return count;                 }        

and function need

            matrix f( const matrix& m , const int& x , const int& y )         {             matrix n(3,3);              for(int i=-1 ; <= 1 ; ++i  )//iterate around m.cell[x][y]                 for(int j=-1; j <= 1 ; ++j)//-------||-----------------                 {           //now count 1's around current cell m.cell[x+i][y+j]          //and place count number inside matrix n @ right position                      n.set_at( i+1, j+1 , count_ones( m , x+i , y+j ) );                  }              return n;// make sure have copy constructor or else wont work               }        

Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -