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

一、填空题
  给定程序中,函数fun的功能是:将形参std所指结构体数组中年龄最大者的数据作为函数值返回,并在main函数中输出。
  请勿改动主函数main和其它函数中的任何内容,仅在fun函数的横线上填入所编写的若干表达式或语句。
  #include <stdio.h>
  typedef struct
  {
  char name[10];
  int age;
  } STD;
  STD fun(STD std[], int n)
  {
  STD max;
  int i;
  max = ___1___;
  for (i=1; i<n; i++)
  if (max.age < ___2___)
  max = std[i];
  return max;
  }
  main()
  {
  STD std[5] = {"aaa", 17, "bbb", 16, "ccc", 18, "ddd", 17, "eee", 15};
  STD max;
  max = fun(std, 5);
  printf("\nThe result: \n");
  printf("\nName : %s, Age : %d\n", ___3___, max.age);
  }
二、改错题
  下列给定程序中,函数fun的功能是:利用插入排序法对字符串中的字符按从小到大的顺序进行排序。插入法的基本算法是:先对字符串中的头两个元素进行排序。然后把第三个字符插入到前两个字符中,插入后前三个字符依然有序,再把第四个字符插入到前三个字符中,……。待排序的字符串已在主函数中赋予。
  请改正程序中的错误,使它能得出正确的结果。
  注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
  #include <stdio.h>
  #include <string.h>
  #define N 80
  void insert(char *aa)
  {
  int i, j, n;
  char ch;
  n = strlen(aa);
  for (i=1; i<n ;i++)
  {
  /********found********/
  c = aa[i];
  j = i-1;
  while ((j>=0) && (ch<aa[j]))
  {
  aa[j+1] = aa[j];
  j--;
  }
  aa[j+1] = ch;
  }
  }
  main()
  {
  char a[N] = "QWERTYUIOPASDFGHJKLMNBVCXZ";
  printf("The original string : %s\n", a);
  insert(a);
  printf("The string after sorting : %s\n\n", a);
  }
三、编程题
  学生的记录由学生成绩组成,N名学生的数据已在主函数中放入结构体数组s中,请编写函数fun,它的功能是:把分数最低的学生数据放在h所指的数组中,注意:分数最低的学生可能不止一个,函数返回分数最低的学生的人数。
  请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
  #include <stdio.h>
  #define N 16
  typedef struct
  {
  char num[10];
  int s;
  } STREC;
  int fun ( STREC *a, STREC *b )
  {
  }
  main ()
  {  http://ks.examda.com
  STREC s[N]= {{"GA05",85}, {"GA03",76}, {"GA02",69}, {"GA04",85},
  {"GA01",91}, {"GA07",72}, {"GA08",64}, {"GA06", 87},
  {"GA015",85}, {"GA013",91}, {"GA012",64}, {"GA014",91},
  {"GA011",91}, {"GA017",64}, {"GA018",64}, {"GA016",72}};
  STREC h[N];
  int i, n;
  FILE *out;
  n=fun ( s, h );
  printf ("The %d lowest score :\n", n);
  for (i=0; i<n; i++)
  printf ("%s %4d\n", h[i]. num, h[i]. s);
  printf ("\n");
  out=fopen ("out.dat", "w");
  fprintf (out, "%d\n", n);
  for (i=0; i<n; i++)
  fprintf (out, "%4d\n", h[i].s);
  fclose (out );
  }
就业数据资源平台