就业数据资源平台
当前位置:首页 > C语言程序设计
计算机等级考试二级C语言试题及答案7

填空题:请补充函数,该函数的功能是:输出一个N*N矩阵,要求非对对角线上的元素赋值0,对角线元素赋值1。
  仅在横线上填入所编写的若干表达式或语句,勿改动函数中的内容。
  #include <stdio.h>
  #define N 10
  main()
  {
  int bb[N][N];
  int i, j, n;
  printf(" \nInput n:\n");
  scanf("%d", &n);
  for (i=0; i<n; i++)
  for (j=0; j<n; j++)
  {
  ___1___;
  if (i == j)
  bb[i][j] = ___2___;
  if (___3___)
  bb[i][j] = 1;
  }
  printf(" \n***** the result ******* \n");
  for (i=0; i<n; i++)
  {
  printf(" \n\n");
  for (j=0; j<n; j++)
  printf("%4d", bb[i][j]);
  }
  }
  参考答案:
  答案:填空题:bb[i][j]=0
  1
  j==n-1-i或j==n-i-1
改错题:下列给定程序中,fun函数的功能是:传入一个整数m,计算如下公式的值。
  t=1-1/2-1/3-…-1/m
  例如,若输入5,则应输入-0.283333。
  请改正函数fun中的错误或在横线处填上适当的内容并把横线删除,使它能计算出正确的结果。
  注意:不要改动main函数,不得增行或减行,也不得更改程序的结构。
  #include <conio.h>
  #include <stdio.h>
  double fun(int m)
  {
  double t = 1.0;
  int i;
  /********found********/
  for (i=2; i<=m; i++)
  t = 1.0 - 1/i;
  /********found********/
  ___填空___
  }
  main() 
  {
  int m;
  printf("\nPlease enter 1 integer numbers:\n");
  scanf("%d", &m);
  printf("\n\nThe result is %lf\n", fun(m));
  }
  参考答案:
  改错题:t-=1.0/I;或t=t-1.0/i
  return t;
编程题:假定输入的字符串中只包含字母和*号。请编写函数,它的功能是:除了字符串前导和尾部的*号之外,将串中其他*号全部删除。形参已指向字符串中第一个字母,形参已指向字符串中最后一个字母。在编写函数时,不得使用C语言提供的字符串函数。
  例如,若字符串中的内容为****A*BC*DEF*G********,删除后,字符串中的内容则应当是****ABCDEFG********。在编写函数时,不得使用C语言提供的字符串函数。
  请勿改动主函数和其他函数中的内容,仅在函数的花括号中填入所编写的若干语句。
  #include <stdio.h>
  #include <conio.h>
  #include <string.h>
  void fun( char *a, char *h, char *p)
  {
  }
  main()
  {
  char s[81],*t,*f;
  FILE *out;
  printf("Enter a string:\n");
  gets(s);
  t=f=s; www.Examda.CoM
  while(*t)
  t++;
  t--;
  while(*t=='*')
  t--;
  while(*f=='*')
  f++;
  fun(s,f,t);
  printf("The string after deleted:\n");
  puts(s);
  out=fopen ("out.dat", "w");
  strcpy(s, "****A*BC*DEF*G********");
  fun(s, s+4, s+13);
  fprintf(out, "%s", s);
  fclose (out );
  }
  参考答案:
  编程题:void fun(char *a,char *h,char *p)
  {
  int i=0;
  char *q=a;
  while(q<h)
  {
  a[i]=*q;
  q++;
  i++:
  }
  while(q<p)
  {
  if(*q!=’*’)
  {a[i]=*q;
  i++;
  }
  q++;
  }
  while(*q)
  {
  a[i]=*q;
  i++;
  q++
  }
  a[i]=’\0’;
  }
就业数据资源平台