2006年9月全國等級考試三級c語言上機題庫(二十二)
★題目22(無憂id 39 平方根問題) 請編寫函式countvalue(),它的功能是:求n以內(不包括n)同時能被3與7整除的所有自然數之和的平方根s,並作為函式值返回,最後結果s輸出到檔案out.dat中。例如若n為1000時,函式值應為:s=153.909064。部分源程式存在檔案prog1.c中。請勿改動主函式main()和輸入輸出數據函式progreadwrite()的內容。#include <conio.h>#include <math.h>#include <stdio.h> double countvalue(int n){ int i;double s=0.0;for(i=1;i<n;i++)if(i%21==0) s+=i;return sqrt(s);} main(){clrscr();printf("自然數之和的平方根=%f\n",countvalue(1000));progreadwrite();} progreadwrite(){file *fp,*wf;int i,n;float s; fp=fopen("in.dat","r");if(fp==null){printf("數據檔案in.dat不存在!");return;}wf=fopen("out.dat","w");for(i=0;i<10;i++){fscanf(fp,"%d\n",&n);s=countvalue(n);fprintf(wf,"%f\n",s);}fclose(fp);fclose(wf);}