当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
2015年计算机二级C语言上机题库及答案(63)
发布时间:2011/9/28 11:38:38 来源:城市学习网 编辑:ziteng
  一、填空题:给定程序中,函数fun的功能是:计算形参x所指数组中N个数的平均值(规定所有数均为正数),将所指数组中大于平均值的数据移至数组的前部,小于等于平均值的数据移至x所指数组的后部,平均值作为函数值返回,在主函数中输出平均值和移动后的数据。

  例如,有10个正数:46 30 32 40 6 17 45 15 48 26,平均值为: 30.500000

  移动后的输出为:46 32 40 45 48 30 6 17 15 26

  请在程序的下划线处填入正确的内容并把下划线删除, 使程序得出正确的结 果。

  注意:源程序存放在考生文件夹下BLANK1.C中。

  不得增行或删行,也不得更改程序的结构!

  给定源程序:

  #include

  #include

  #define N 10

  double fun(double *x)

  {int i, j; double s, av, y[N];

  s=0;

  for(i=0; i

  /**********found**********/

  av=__1__;

  for(i=j=0; i

  if(x[i]>av){

  /**********found**********/

  y[__2__]=x[i]; x[i]=-1;}

  for(i=0; i

  /**********found**********/

  if(x[i]!= __3__) y[j++]=x[i];

  for(i=0; i

  return av;

  }

  main()

  {int i; double x[N];

  for(i=0; i

  printf("\nThe average is: %f\n",fun(x));

  printf("\nThe result :\n",fun(x));

  for(i=0; i

  }

  解题答案:

  /*********第一空**********/

  av=s/N;

  /**********第二空**********/

  y[j++]=x[i]; x[i]=-1;}

  /**********第三空**********/

  if(x[i]!= -1) y[j++]=x[i];

  ****************************************** [NextPage]   二、改错题:给定程序MODI1.C的功能是:读入一个英文文本行,将其中每个单词的第一个字母改成大写,然后输出此文本行(这里的“单词”是指由空格隔开的字符串)。

  例如,若输入:I am a student to take the examination.,

  则应输出:I Am A Student To Take The Examination.。

  请改正程序中的错误,使程序能得出正确的结果。

  注意:不要改动 main 函数,不得增行或删行,也不得更改程序的结构!

  给定源程序:

  #include

  #include

  /************found************/

  include

  /************found************/

  upfst (char p)

  {int k=0;

  for (; *p; p++)

  if (k)

  {if (*p == ' ') k = 0;}

  else if (*p != ' ')

  {k = 1; *p = toupper(*p);}

  }

  main()

  {char chrstr[81];

  printf("\nPlease enter an English text line: "); gets(chrstr);

  printf("\n\nBefore changing:\n %s", chrstr);

  upfst(chrstr);

  printf("\nAfter changing:\n %s\n", chrstr);

  }

  解题答案:

  /************found************/

  #include

  /************found************/

  upfst(char *p)

  ******************************************

 [NextPage]   三、程序题:程序定义了N×N的二维数组,并在主函数中赋值。请编写函数fun,函数的功能是:求出数组周边元素的平均值并作为函数值返给主函数中的s。

  例如:a 数组中的值为

  |0 1 2 7 9|

  |1 9 7 4 5|

  a = |2 3 8 3 1|

  |4 5 6 8 2|

  |5 9 1 4 1|

  则返回主程序后s的值应为: 3.375。

  注意: 部分源程序存在文件PROG1.C文件中。

  请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。

  给定源程序:

  #include

  #include

  #define N 5

  double fun (int w[][N])

  {

  }

  main ()

  {int a[N][N]={0,1,2,7,9,1,9,7,4,5,2,3,8,3,1,4,5,6,8,2,5, 9,1,4,1};

  int i, j;void NONO ();

  double s ;

  printf("***** The array *****\n");

  for (i =0; i

  {for (j =0; j

  {printf("M", a[i][j]);}

  printf("\n");

  }

  s = fun (a);

  printf ("***** THE RESULT *****\n");

  printf("The sum is : %lf\n",s);

  NONO();

  }

  void NONO ()

  {/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */

  FILE *rf, *wf ;

  int i, j, a[5][5]; double s ;

  rf = fopen("in.dat","r");

  wf = fopen("out.dat","w");

  for(i = 0 ; i < 5 ; i++)

  for(j = 0 ; j < 5 ; j++) fscanf(rf, "%d ", &a[i][j]);

  s = fun(a);

  fprintf(wf, "%lf\n", s);

  fclose(rf);

  fclose(wf);

  }

  参考答案:

  double fun ( int w[][N] )

  {

  int i, j, n=0;

  double sum=0;

  for ( i =0; i

  sum+=w[0][i]+w[N-1][i]; n+=2;

  }

  for ( i =1; i

  sum +=w[i][0]+w[i][N-1];

  n+=2;

  }

  return sum/n;

  }

  

广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved