当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
2015年计算机二级C语言上机操作题及答案(93)
发布时间:2011/7/28 15:43:09 来源:城市学习网 编辑:ziteng
  一、 填空题
  请补充fun函数,该函数的功能是:交换数组中aa中最大和最小两个元素的位置,结果重新保存在原数组中,其他元素位置不变。注意数组aa中没有相同的元素。
  例如,输入“45,55,62,42,35,52,78,95,66,73”,则输出“45,55,62,42,95,52,78,35,66,73”。
  请勿改动主函数main和其他函数中的任何内容,仅在fun函数的横线上填入所编写的若干表达式或语句。
  #include
  #define  N 10
  void fun(int  aa[])
  {
  int  i, t;
  int  max = 0, min = 0;
  for (i=0; i {
  if (___1___)
  max = i;
  if (___2___)
  min = i;
  }
  t = aa[max];
  ___3___;
  aa[min] = t;
  }
  main()
  {
  int  i;
  int  aa[N] = {45, 55, 62, 42, 35, 52, 78, 95, 66, 73};
  printf("\n******** original list ***********\n");
  for (i=0; i  printf("M", aa[i]);
  fun(aa);
  printf("\n******** new list ***********\n");
  for (i=0; i  printf("M", aa[i]);
  }
  答案:
  1、aa[max]aa[max]
  2、aa[min]>aa[i]或aa[i]3、aa[max]=aa[min] [NextPage]    二、 改错题
  下列给定程序中,函数fun的功能是:用下面的公式__的近似值,直到最后一项的绝对值小于指定的数(参数num)为止:
  例如,程序运行后,输入0.0001,则程序输出3.1414。
  请改正程序中的错误,使它能输出正确的结果。
  注意:不要改动main函数,不得增行或删行,不得更改程序的结构!
  #include
  #include
  float fun(float  num)
  {
  int  s;
  float  n, t, pi;
  t = 1;
  pi = 0;
  n = 1;
  s = 1;
  /********found********/
  while (t >= num)
  {
  pi = pi+t;
  n = n+2;
  s = -s;
  /********found********/
  t = s%n;
  }
  pi = pi*4;
  return pi;
  }
  main()
  {
  float  n1, n2;
  printf("Enter a float number: ");
  scanf("%f", &n1);
  n2 = fun(n1);
  printf("%6.4f\n", n2);
  }
  答案:
  1、while(t>=num)应改为while (fabs(t)>=num)
  2、t=s%n;应改为 t=s/n;
[NextPage]   三、 编程题
  请编写函数fun,它的功能是计算系列级数和,和值由函数返回。
  例如,当n=10,x=0.3时,函数值为1.349859。
  请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
  #include
  #include
  #include
  double fun( double x, int n)
  {
  }
  main()
  {
  int i;
  FILE *out;
  printf("%f\n", fun(0.3,10));
  out=fopen("out.dat","w");
  for (i = 0; i < 10; i++)
  fprintf(out, "%f\n", fun((i+4)/10.0, 10));
  fclose(out);
  }
  答案:
  double  fun ( double  x ,  int  n)
  {
  double  s=1.0,  p=1.0 ;
  int  i , j,  t;
  for( i=1 ;  i<=n;  i++)
  {
  t=1;
  for(j=1;j<=i; j++)
  t=t*j;
  p=p*x;
  s=s+p/t;
  }
  return  s;
  }
广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved