C语言机试模拟试题
一、改错题:(30分)
1、求一维数组a中的值为偶数的元素之和。
#include <stdio.h>
  sum ( int arr[ ],int n )
  { int i,s;
    s = 0;
    for ( i=0; i<n; i++)
      if (arr[i] % 2 == 0)
  /************found************/
        s=s+i;改为:s = s + arr[i];
    return (s);
  }
  void main()
  { int a[10]={10,4,2,7,3,12,5,34,5,9},i,s;
  /************found************/
    s = sum( a ,2 ); 改为:s = sum( a ,10 );
    printf("The result is: %d\n", s);
  }
2、求一维数组a中的值为奇数的元素之和。
#include <conio.h>
  #include <stdio.h>
    int sum( int b[ ],int n )
  { int i,s = 0;
    for ( i=0; i<n; i++)
      if (b[i] % 2 == 1)
  /************found************/
        s = s + b[i] 改为:s = s + b[i];
    return (s);
  }
  void main()
  { int a[12]={10,4,2,7,3,12,5,34,5,9,21,19},n;
  /************found************/
    n = sum(a,2); 改为:n = sum(a,12);
    printf("The result is :%d\n",n );
  }
3、求二维数组a中的最大值和最小值。
#include <stdio.h>
  void main()
  { int a[3][3]={4,4,34,37,3,12,5,6,5},i,j,max,min;
    //clrscr();
    max = min = a[0][0];
    for ( i=0; i<3; i++)
  /************found************/
    for ( j=1; j<3; j++)改为:for ( j=0; j<3; j++)
    { if ( max < a[i][j] )
        max = a[i][j];
  /************found************/
      if (min < a[i][j]) 改为:if (min > a[i][j])
    min = a[i][j];
      }
    printf("The max is: %d\n", max);
    printf("The min is: %d\n", min);
  }
4、求一维数组a中的最大元素及其下标。
  #include <stdio.h>
  void main()
  { int a[10]={1,4,2,7,3,12,5,34,5,9},i,max,pos;
    //clrscr();
    max = a[0];
    pos = 0;
    for ( i=1; i<10; i++)
  /************found************/
      if (max > a[i]) 改为:if (max< a[i])
      {
    max = a[i];
  /************found************/
    i = pos; 改为:pos = i;
      }
    printf("The max is: %d ,pos is: %d\n", max , pos);
c语言数组最大值最小值
  }
5、猴子吃桃的问题,求第一天的桃子数。
  #include <stdlib.h>
  #include <math.h>
  void main()
  { int i,j,x,p;
    //clrscr();
    x=1;
  /************found************/
  j=12; 改为:j=11;
    while(j>=1)
    { p=(x+1)*2;
  /************found************/
    p=x; 改为:x = p;
      j--;
    }
    printf("total is %d\n",p);
  }
6、求一维数组a中的值为奇数的元素的平均值。
  #include <stdio.h>
  double average( int arr[ ], int n )
  { int k=0,i; double s;
    s = 0;
    for ( i=0 ;i<n; i++)
  /************found************/
      if (arr[i] % 2 = 1) 改为:if (arr[i] % 2 = =1)
    { s = s + arr[i]; k++; }
    return (s/k) ;
  }
  void main()
  {
  int a[12]={10,4,2,7,3,12,5,34,5,9,21,18};
  double s;
  // clrscr();
  /************found************/
  s = average(a[12],12); 改为:s = average(a,12);
  printf("The result is: %.2f\n", s);
  }
二、填空题:(30分)
1、 程序的功能是:在第一个循环中从键盘上给a数组的前M(M<100)个数组元素依次赋值,在第二个循环中使a数组前M个元素中的值对称折叠,在最后一个循环中输出折叠后的 a数组的前M个元素。程序如下:
#define  M  5
  void main( )
  { int i,a[100],t;
    //clrscr();
      for(i=0;i<M;i++)
  /**************found************/
      ___(1)___ -填写的语句为: scanf(“%d”,&a[i]);
  /**************found************/
    for(i=0;___(2)___;i++) -填写的语句为:i<M
      a[M-i-1]=a[i];
    for(i=0;i<M;i++) 
      printf("%5d",a[i]);
    printf("\n"); 
  }
2、 计算正整数num的各位上的数字之和。
#include <stdio.h>
  void main( )
  {    int num,k;
    //clrscr() ;
  /************found************/
    ___(1)___; 填写的语句为:k=0;
    printf("Please enter a number:") ;
    scanf("%d",&num) ;
    do
    {
  /************found************/
    k=___(2)___ ; 填写的语句为:k=k+num%10;
    num/=10;
    } while(num) ;
    printf("\n%d\n",k) ;
  }
3、 求cman=m!/n!*(m-n)!之值,例如:m=12,n=2时,cman is 66
#include <math.h>
    #include <stdio.h>
  long int fun(int x)
  {long int s=1; int i;
  for (i=1;i<=x;i++)