2013年计算机二级C语言上机题库十四_第3页

考试站(www.examzz.com)   【考试站:中国教育考试第一门户】   2013年4月12日
编程题
  请编写fun函数,函数的功能是求出二维数组周边的元素之和,作为函数值返回,二维数例如:若二维数组中的值为:
  13579
  29994
  69998
  13570
  则函数为61。
  注意:部分源程序给出如下。
  请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
  试题程序:
  #include <conio.h>
  #include <stdio.h>
  #define M 4
  #define N 5
  int fun ( int a[M][N] )
  {
  } 
  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;
  FILE *out;
  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");
  out = fopen("out.dat", "w");
  fprintf(out, "%d" , y);
  fclose(out);
  }
  答案是:
  int fun (int a[M][N])
  {
  int i,j,s=0;
  for(j=0;j<N;j++)
  {
  s+=a[0][j];
  s+=a[M-1][j];
  }
  for(i=1;i<=M-2;i++)
  {
  s+=a[i][0];
  s+=a[i][N-i];
  }
  return s;
  }
首页 1 2 3 尾页

相关文章