C语言填空考试题概要.pptVIP

  • 173
  • 0
  • 约7.14千字
  • 约 13页
  • 2016-11-23 发布于湖北
  • 举报
C语言填空考试题概要

6、函数fun的功能是根据形参i的值返回某个函数的值。当调用正确时,程序输出: ??? x1=5.000000, x2=3.000000, x1*x1+x1*x2=40.000000 #include stdio.h double f1(double x) { return x*x; } double f2(double x, double y) { return x*y; } /**********found**********/ __1__fun(int i,double x,double y) { if (i==1) /**********found**********/ ??? return __2__(x); else /**********found**********/ ??? return __3__(x,y); } main() { double x1=5, x2=3, r; r=fun(1,x1,x2); r+=fun(2,x1,x2); printf(\nx1=%f, x2=%f, x1*x1+x1*x2=%f\n\n,x1, x2, r);} 答案: 第一处:double fun(int i,double x,double y) 第二处:return f1(x); 第三处:return f2(x,y); 27、给定程序中,函数fun的功能是:计算下式前n项的和作为函数值返回。s=(1*3)/22+(3*5)/42+(5*7)/62+(2*n-1)* (2*n+1)/(2*n)2 ??? 例如,当形参n的值为10时,函数返回:9.612558。 #include stdio.h double fun(int n) { int i;??? double s,t; /**********found**********/ s=__1__; /**********found**********/ for(i=1;i=__2__;i++) { t=2.0*i; /**********found**********/ ??? s=s+(2.0*i-1)*(2.0*i+1)/__3__;} return s; } main() { int n=-1; while(n0) { printf(Please input(n0): ); scanf(%d,n); } printf(\nThe result is: %f\n,fun(n)); } 答案:第1处 s=0;第2处 for(i=1;i=n;i++) 第3处 s=s+(2.0*i-1)*(2.0*i+1)/(t*t); 44、函数fun的功能是进行数字字符转换。若形参ch中是数字字符‘0’~‘9’,则‘0’转换成‘9’,‘1’转换成‘8’,‘2’转换成‘7’,……‘9’转换成‘0’;若是其它字符则保持不变;并将转换后的结果作为函数值返回。 #include stdio.h /**********found**********/ __(1)___fun(char ch) /**********found**********/ {if(ch=0__(2)___) /**********found**********/ return 9-(ch- ___(3)__); return ch;} main() { char c1,c2; printf(\nThe result:\n); c1=2;c2=fun(c1); printf(c1=%c c2=%c\n,c1,c2); c1=8;c2=fun(c1); printf(c1=%c c2=%c\n,c1,c2); c1=a;c2=fun(c1); printf(c1=%c c2=%c\n,c1,c2); } 第1处char fun(char ch)第2处 if(ch=‘0’ch=‘9’) 第3处 return ‘9’-(ch-‘0’); 53、函数fun的功能是:计算f(x)=1+x-x2/2!+x3/3!-x4/4!直到|xn/n!|10-6若x=2.5,函数值为:1.917915。 (自己补充) #include stdio.h #include math.h double fun(double x) { double sum=1,t,n=2; __1__; do { sum+=t; t*=(-1.0)*x/__2_; n+=1.0; } while(------__3___=1e-6); return sum;} main() { printf(%f,fun(2.5));} 答案:第1处 t=x

文档评论(0)

1亿VIP精品文档

相关文档