1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
#include <iostream>
#include <algorithm>
const int MAX_COLS = 100 ;
void adjustRow( int ncols, int row[] )
{
if( ncols < 2 ) return ;
// don't try to be be too clever; just make a copy
// (we need the old unmodified values for our calculations)
int temp[MAX_COLS] ;
std::copy( row, row+ncols, temp ) ;
row[0] = ( temp[0] + temp[1] ) / 2 ;
row[ncols-1] = ( temp[ncols-2] + temp[ncols-1] ) / 2 ;
for( int i = 1 ; i < ncols-1 ; ++i ) row[i] = ( temp[i-1] + temp[i] + temp[i+1] ) / 3 ;
}
void adjustSignals( int nrows, int ncols, int G[][MAX_COLS] )
{
for( int r = 0 ; r < nrows ; ++r ) adjustRow( ncols, G[r] ) ;
}
int main()
{
const int NROWS = 3 ;
int ncols = 5 ;
int a[NROWS][MAX_COLS]
{
{ 4, 7, 3, 5, 8 },
{ 8, 9, 7, 8, 6 },
{ 5, 4, 6, 7, 6 }
};
adjustSignals( NROWS, ncols, a ) ;
for( int r = 0 ; r < NROWS ; ++r )
{
for( int c = 0 ; c < ncols ; ++c ) std::cout << a[r][c] << ' ' ;
std::cout << '\n' ;
}
}
|