分享个自己弄得小程序,功能是统计二维数组周边元素之和。刚开始弄得把四角的元素重复统计了。琢磨好久才发现问题所在。特地分享出来,希望对大家有帮助!
#include <stdio.h>
#define M 4
#define N 5
int fun ( int a[M][N] )
{
int tot = 0, i, j ;
for(i = 0 ; i < N ; i++)
{
tot += a[0][i] ;
tot += a[M-1][i] ;
}
for(i = 1 ; i < M - 1 ; i++)
{
tot += a[i][0] ;
tot += a[i][N-1] ;
}
return tot ;
}
int main( )
{
int aa[M][N]={ {1,3,5,7,9},
{2,9,9,9,4},
{6,9,9,9,8},
{1,3,5,7,0}
};
int i, j, y;
printf ( "The original data is : \n" );
for ( i=0; i<M; i++ )
{
for ( j =0; j<N; j++ )
printf( "%6d", aa[i][j] );
printf ("\n");
}
y = fun ( aa );
printf( "\nThe sum: %d\n" , y );
printf("\n");
return 0;
}
|
|