Android面试题——求出二维数组周边元素之和

函数fun功能是:求出二维数组周边元素之和,作为函数值返回,二维数组中的值在主函数中赋予。

#define _CRT_SECURE_NO_WARNINGS   
#include<stdio.h>  
#define M 4  
#define N 5  
int fun(int a[M][N])  
{  
    int i, s = 0;  
    for (i = 0;i < N;i++)  
        s += a[0][i] + a[M - 1][i];  
    for (i = 1;i < M - 1;i++)  
        s += a[i][0] + a[i][N - 1];  
    return s;  
}  
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 sun:%d\n", y);  
    printf("\n");  
    system("pause");  
    return 0;  
}  

猜你喜欢

转载自blog.csdn.net/turodog/article/details/53245196