# Return elements of a specified subgrid in a 9×9 grid array?

I have a 9×9 array:

```+-----+-----+-----+
|4 0 0|0 0 0|0 0 8|
|0 0 3|9 8 7|6 4 5|
|0 0 0|0 0 0|0 0 0|
+-----+-----+-----+
|8 5 6|0 0 0|0 2 0|
|0 0 5|8 1 6|0 0 0|
|6 0 0|0 0 0|0 0 1|
+-----+-----+-----+
|0 6 0|0 0 0|7 5 2|
|5 9 4|7 2 8|1 0 0|
|2 0 0|0 0 0|0 0 4|
+-----+-----+-----+
```

I want to create a function that returns the elements of a particular sub-grid entered in the argument of the function. For example, if I want to return sub-grid 3, I’d call returnSubgrid(3) and I’d get this result:

```+-----+
|0 0 8|
|6 4 5|
|0 0 0|
+-----+
```

What is the mathematical formula for this? This is what I have so far to return the first sub-grid to a string:

```std::string Sudoku::returnSubgrid(int subgrid)
{
std::string output;
for(int i = subgrid - 1; i < subgrid + 2; i++) {
for(int j = subgrid - 1; j < subgrid + 2; j++) {
output += inputBoard_[i][j];
}
}
return output;
}
```

You can achieve like this,

```string returnSubgrid(int subgrid) {
std::string output;
subgrid--;
// inputBoard is 0 based indexing
int row = 3 * (subgrid / 3);
int col = 3 * (subgrid % 3);
for(int i = row; i < row + 3; i++) {
for(int j = col; j < col + 3; j++) {
output += inputBoard_[i][j];
}
}
return output;
}
```