#include <iostream>
const unsigned dim = 3;
typedef int array_type [dim][dim];
void average(array_type& dest, const array_type& src)
{
// corners:
dest[ 0 ][ 0 ] =
(src[ 0 ][ 0 ] + src[ 0 ][ 1 ] + src[ 1 ][ 0 ] + src[ 1 ][ 1 ])/4;
dest[dim-1][ 0 ] =
(src[dim-1][ 0 ] + src[dim-2][ 0 ] + src[dim-1][ 1 ] + src[dim-2][ 1 ])/4;
dest[ 0 ][dim-1] =
(src[ 0 ][dim-1] + src[ 1 ][dim-1] + src[ 0 ][dim-2] + src[ 1 ][dim-2])/4;
dest[dim-1][dim-1] =
(src[dim-1][dim-1] + src[dim-2][dim-1] + src[dim-1][dim-2] + src[dim-2][dim-2])/4;
// top, bottom, and sides:
for (unsigned i = 1; i < dim - 1; ++i)
{
const unsigned j = dim - 1;
dest[0][i] =
(src[0][i-1] + src[0][i] + src[0][i+1] + src[ 1 ][i-1] + src[ 1 ][i] + src[ 1 ][i+1])/6;
dest[j][i] =
(src[j][i-1] + src[j][i] + src[j][i+1] + src[j-1][i-1] + src[j-1][i] + src[j-1][i+1])/6;
dest[i][0] =
(src[i-1][0] + src[i][0] + src[i+1][0] + src[i-1][ 1 ] + src[i][ 1 ] + src[i+1][ 1 ])/6;
dest[i][j] =
(src[i-1][j] + src[i][j] + src[i+1][j] + src[i-1][j-1] + src[i][j-1] + src[i+1][j-1])/6;
}
// middle:
for (unsigned i = 1; i < dim - 1; ++i)
{
for (unsigned j = 1; j < dim - 1; ++j)
{
dest[i][j] = (src[i-1][j-1] + src[i-1][j] + src[i-1][j+1] +
src[ i ][j-1] + src[ i ][j] + src[ i ][j+1] +
src[i+1][j-1] + src[i+1][j] + src[i+1][j+1]) / 9 ;
}
}
}
void print(const array_type& array)
{
for (unsigned i = 0; i < dim; ++i)
{
for (unsigned j = 0; j < dim; ++j)
std::cout << array[i][j];
std::cout << '\n';
}
std::cout << '\n';
}
int main()
{
array_type a =
{
1, 2, 3,
4, 5, 6,
7, 8, 9
};
array_type b = {};
print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VtpXSArIHNyY1sgMSBdW2krMV0pLzY7CiAgICAgICAgZGVzdFtqXVtpXSA9IAoJCQkoc3JjW2pdW2ktMV0gKyBzcmNbal1baV0gKyBzcmNbal1baSsxXSArIHNyY1tqLTFdW2ktMV0gKyBzcmNbai0xXVtpXSArIHNyY1tqLTFdW2krMV0pLzY7CgogICAgICAgIGRlc3RbaV1bMF0gPSAKCQkJKHNyY1tpLTFdWzBdICsgc3JjW2ldWzBdICsgc3JjW2krMV1bMF0gKyBzcmNbaS0xXVsgMSBdICsgc3JjW2ldWyAxIF0gKyBzcmNbaSsxXVsgMSBdKS82OwogICAgICAgIGRlc3RbaV1bal0gPSAKCQkJKHNyY1tpLTFdW2pdICsgc3JjW2ldW2pdICsgc3JjW2krMV1bal0gKyBzcmNbaS0xXVtqLTFdICsgc3JjW2ldW2otMV0gKyBzcmNbaSsxXVtqLTFdKS82OwogICAgfQoKICAgIC8vIG1pZGRsZToKICAgIGZvciAodW5zaWduZWQgaSA9IDE7IGkgPCBkaW0gLSAxOyArK2kpCiAgICB7CiAgICAgICAgZm9yICh1bnNpZ25lZCBqID0gMTsgaiA8IGRpbSAtIDE7ICsraikKICAgICAgICB7CiAgICAgICAgICAgIGRlc3RbaV1bal0gPSAoc3JjW2ktMV1bai0xXSArIHNyY1tpLTFdW2pdICsgc3JjW2ktMV1baisxXSArCiAgICAgICAgICAgICAgICAgICAgICAgICAgc3JjWyBpIF1bai0xXSArIHNyY1sgaSBdW2pdICsgc3JjWyBpIF1baisxXSArCiAgICAgICAgICAgICAgICAgICAgICAgICAgc3JjW2krMV1bai0xXSArIHNyY1tpKzFdW2pdICsgc3JjW2krMV1baisxXSkgLyA5IDsKICAgICAgICB9CiAgICB9Cn0KCnZvaWQgcHJpbnQoY29uc3QgYXJyYXlfdHlwZSYgYXJyYXkpCnsKICAgIGZvciAodW5zaWduZWQgaSA9IDA7IGkgPCBkaW07ICsraSkKICAgIHsKICAgICAgICBmb3IgKHVuc2lnbmVkIGogPSAwOyBqIDwgZGltOyArK2opCiAgICAgICAgICAgIHN0ZDo6Y291dCA8PCBhcnJheVtpXVtqXTsKICAgICAgICBzdGQ6OmNvdXQgPDwgJ1xuJzsKICAgIH0KICAgIHN0ZDo6Y291dCA8PCAnXG4nOwp9CgppbnQgbWFpbigpCnsKICAgIGFycmF5X3R5cGUgYSA9IAogICAgewogICAgICAgIDEsIDIsIDMsCiAgICAgICAgNCwgNSwgNiwKICAgICAgICA3LCA4LCA5CiAgICB9OwoKICAgIGFycmF5X3R5cGUgYiA9IHt9OwoKICAgIHByaW50KGEpOwogICAgcHJpbnQoYik7CgogICAgYXZlcmFnZShiLCBhKTsKICAgIHByaW50KGIpOwp9