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

考试站(www.examzz.com)   【考试站:中国教育考试第一门户】   2013年4月7日
 编程题
  编写函数fun,函数功能是:根据以下公式计算s,计算结果作为函数值返回;n通过型参传入。
  S=1+
  例如:若n的值为11时,函数的值为1.833333。
  注意:部分源程序给出如下。
  请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
  试题程序:#include<conio.h>
  #include<stdio.h>
  #include<string.h>
  float fun (int n)
  {
  }
  main()
  {
  int n;
  float s;
  FILE *out;
  printf("\nPlease enter N:");
  scanf("%d",&n);
  s=fun(n);
  printf("The result is: %f\n",s);
  s = fun(28);
  out = fopen("out.dat", "w");
  fprintf(out, "%f", s);
  fclose(out);
  }
  答案是:
  float fun(int n)
  {
  int i;
  float s=1.0,t=1.0;
  for(i=2;i<=n;i++)
  {
  t=t+i;
  s=s+1/t;
  }
  return s;
  }
首页 1 2 3 尾页

相关文章