求两道c语言题目

来源:百度知道 编辑:UC知道 时间:2024/06/27 13:48:12
9、请编写函数fun,函数的功能是求出二维数组周边元素之和,作为函数值返回。二维数组中的值在主函数中赋予。
10、取子串函数
编写求子串函数substr(s,n1,n2),在串s中从n1位置开始取n2个字符的子串

在devc++中调试可运行。

#include <stdlib.h>
#include <stdio.h>

#define ROW 3

int fun( int tmp[ROW][ROW], int r, int c )//r为行c为列
{
if ( r>=ROW||c>=ROW )
{
return -1;
}
int a[4][2]={{-1,0},{1,0},{0,-1},{0,1}};//左右上下
int i=0;
int sum=0;
for( i=0; i<4; i++ )
{
if ( r+a[i][0]<0 || c+a[i][1]<0 || r+a[i][0]>=ROW || c+a[i][1]>=ROW )
{
continue;
}
printf("tmp[%d][%d]=%d\n",r+a[i][0],c+a[i][1],tmp[r+a[i][0]][c+a[i][1]]);
sum+= tmp[r+a[i][0]][c+a[i][1]];
}
return sum;
}

main()
{
int tmp[ROW][ROW];
int i,j;
int r;
i = 0;
for ( i=0; i<ROW; i++ )
{
for ( j=0; j<ROW; j++ )
{
printf( "tmp[%d]