C期末13

/*------------------------------------------------
【程序设计】
--------------------------------------------------

题目:根据以下分工计算s,计算结果作为函数值返回:
n通过形参传入。s=1+1/(1+2)+1/(1+2+3)+…+1/(1+2+3+…+n)
例如:若n 的值为11时,函数的值为1.833333
*********Begin**********和**********  End  **********不可删除



------------------------------------------------*/
void  TestFunc(); 
#include<stdio.h>
#include<string.h>
double fun (int n)
{
  /*********Begin**********/
  




  /**********  End  **********/
}

main()
{   int n;
        double s;
        printf("\nPlease enter N:");
        scanf("%d",&n);
        s=fun(n);
        printf("The result is: %lf\n",s);
  TestFunc();
}

void TestFunc()
{
  FILE *IN,*OUT;
  int i;
  double s;
     IN=fopen("in.dat","r");
  if(IN==NULL)
  {
    printf("Read FILE Error");
  }
  OUT=fopen("out.dat","w");
  if(OUT==NULL)
  {
    printf("Write FILE Error");
  }
  
       fscanf(IN,"%d",&i);
    s=fun(i);
     fprintf(OUT,"%lf",s);
  fclose(IN);
  fclose(OUT);
}

答案【参考代码】
int i,j,t;
double s=0;
for(i=1;i<=n;i++)
{ t=0;
for(j=1;j<=i;j++)
t+=j;
s=s+1.0/t;
}
return s;

【考生代码】
double s=0;
int a=0,j,i;
for( i=1;i<=n;i++)
{ for(j=1;j<=i;j++)
a+=j;s+=1.0/a;}
return s;
2

/*------------------------------------------------
【程序设计】
--------------------------------------------------

功能:求小于lim的所有素数并放在aa数组中,该函数返回
      所求出素数的个数。
*********Begin**********和**********  End  **********不可删除

------------------------------------------------*/

#include<stdio.h>
#include<conio.h>
#define MAX 100

int fun(int lim,int aa[MAX])
{
  /**********Begin**********/
  
  
  
  
  
  
  /**********  End  **********/
}
main()
{
  int limit,i,sum;
  int aa[MAX];
  clrscr();
  printf("Please Input aInteger:");
  scanf("%d",&limit);
  sum=fun(limit,aa);
  for(i=0;i<sum;i++){
    if(i%10==0&&i!=0) printf("\n");
    printf("%5d",aa[i]);
    
  }
  NONO();
  getch();
}
NONO()     
{     
  int i,j,array[100],sum,lim;
  FILE *rf, *wf ;     
  rf = fopen("in.dat", "r") ;
  wf = fopen("out.dat", "w") ;
  for (j=0; j <= 5; j++)
  {
    fscanf(rf, "%d", &lim);
    sum=fun(lim,array);
    for(i=0;i<sum;i++)
      fprintf(wf, "%7d", array[i]);
    fprintf(wf, "\n");     
  }     
  fclose(rf);
  fclose(wf);
} 

答案
【参考代码】
int n=0;
int i,j;
for(i=2;i<=lim;i++)
{for(j=2;j<i;j++)
if(i%j= =0) break;
if(j==i) aa[n++]=i;
}
return n;

【考生代码】
int i,j,a=0;
for(i=2;i<lim;i++)
for(j=2;j<i;j++)
if(i%j!=0){aa[i]=i;a++;}
return a;
3

/*------------------------------------------------------
【程序改错】
--------------------------------------------------------
函数fun的功能是:求1到20的阶乘的和。
--------------------------------------*/
#include "stdio.h"
main ()
{ 
  int n,j;
  float s=0.0,t=1.0;
  for(n=1;n<=20;n++)
  {        
/**********ERROR**********/
    s=1;
    for(j=1;j<=n;j++)
/**********ERROR**********/
      t=t*n;
/**********ERROR**********/
    s+t=s;
  }
 printf("jiecheng=%f\n",s);
}

答案
【改错1】 错误
【学生答案】
s=0;
【参考答案】
t=1;
t=1.0;

==============================
【改错2】 正确
【学生答案】
t=t*j;
【参考答案】
t= t * j ;

==============================
【改错3】 正确
【学生答案】
s=s+t;
【参考答案】
s= s + t ;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值