diff --git a/ex4.2/CaloGrid.cc b/ex4.2/CaloGrid.cc index bc4f79a..cd557f0 100644 --- a/ex4.2/CaloGrid.cc +++ b/ex4.2/CaloGrid.cc @@ -2,13 +2,13 @@ #include #include "CaloGrid.hh" -CaloCell* CaloGrid::cell(int x, int y ) { - if ( x > nx or x < 0 ) { +CaloCell* CaloGrid::cell( int x, int y ) { + if ( x >= nx or x < 0 ) { std::cout << "CaloGrid::cell() Error: out of grid (x)" << std::endl ; return nullptr; } - if ( y > ny or y < 0 ) { + if ( y >= ny or y < 0 ) { std::cout << "CaloGrid::cell() Error: out of grid (y)" << std::endl ; return nullptr; } diff --git a/ex4.2/CaloGrid.hh b/ex4.2/CaloGrid.hh index 28e1caa..decad8b 100644 --- a/ex4.2/CaloGrid.hh +++ b/ex4.2/CaloGrid.hh @@ -26,7 +26,7 @@ class CaloGrid CaloCell* cell(int x, int y); const CaloCell* cell(int x, int y) const { - return const_cast (cell(x, y)); + return const_cast(this)->cell(x, y); } @@ -44,7 +44,7 @@ class CaloGrid // set Cell Ids for ( int i = 0; i < nx ; i++ ) { for ( int j = 0; j < ny ; j++ ) { - cells[i*ny +j].setId( i*ny+j ); + cells[ i*ny+j ].setId( i*ny+j ); } } } diff --git a/ex4.2/main.cpp b/ex4.2/main.cpp index a4c0faf..56cdd3a 100644 --- a/ex4.2/main.cpp +++ b/ex4.2/main.cpp @@ -4,6 +4,10 @@ #include "Point.hh" #include "Calorimeter.hh" +void printCellId( const CaloCell* cell ) { + std::cout << cell->getId() << std::endl; +} + int main() { CaloCell cell(0, 1) ; Point p ; @@ -13,11 +17,18 @@ int main() { grid.cell(1, 1); - int nx = 2; - int ny = 2; + int nx = 3; + int ny = 4; Calorimeter calo(nx, ny) ; - std::cout << calo.grid().cell(0,0)->getId() << std::endl; - std::cout << calo.grid().cell(nx-1,ny-1)->getId() << std::endl; + std::cout << "CellId at Start: " << calo.grid().cell(0,0)->getId() << std::endl; + std::cout << "CellId at End: " << calo.grid().cell(nx-1,ny-1)->getId() << std::endl; + + std::cout << "CellId at Center: " ; + printCellId(calo.grid().cell(nx/2,ny/2)); + + + const CaloGrid grid2(5,5); + grid2.cell(3,3); }