二级C语言操作例题(五)

一、程序填空题

在此程序中,函数fun的功能是:计算形参x所指数组中N个数的平均值(规定所有数均为正数),将所指数组中小于平均值的数据移至数组的前部,大于等于平均值的数据移至x所指数组的后部,平均值作为函数值返回,在主函数中输出平均值和移动后的数据。

例如,有10个正数:47、30、32、40、6、17、45、15、48、26,其平均值为30.500000。

移动后的输出为:30、6、17、15、26、47、32、40、45、48。

#include  <stdlib.h>

#include  <stdio.h>

#define   N   10

double fun(double  *x)

{ int  i, j;    double  av, y[N];

  av=0;

  for(i=0; i<N; i++)

/**********found**********/

av+=__1__;

  for(i=j=0; i<N; i++)

    if( x[i]<av ){

      y[j]=x[i]; x[i]=-1;

/**********found**********/

  __2__;

}

  i=0;

  while(i<N)

  {  if( x[i]!= -1 ) y[j++]=x[i];

/**********found**********/

     __3__;

  }

  for(i=0; i<N; i++)x[i] = y[i];

  return  av;

}

void main()

{ int  i;     double  x[N];

  for(i=0; i<N; i++){ x[i]=rand()%50; printf("%4.0f ",x[i]);}

  printf("\n");

  printf("\nThe average is: %f\n",fun(x));

  printf("\nThe result :\n",fun(x));

  for(i=0; i<N; i++)  printf("%5.0f ",x[i]);

  printf("\n");

}

答案:(1) x[i]/N (2) j++ (3) i++

二、程序修改题

在此程序中,函数fun的功能是:统计字符串中各元音字母(即A、E、I、O、U)的个数。注意:字母不分大小写。

例如,输入”THIs is a boot”,则输出是1 0 2 2 0。

#include  <stdlib.h>

#include  <conio.h>

#include  <stdio.h>

/*************found**************/

fun(char *s, int num[5])

{ int k, i=5;

  for(k=0;k<i;k++)

/*************found**************/

      num[i]=0;

  for(;*s;s++)

      { i=-1;

/*************found**************/

        switch(s)

           { case 'a': case'A':{i=0;break;}

             case 'e': case 'E':{i=1;break;}

             case 'i': case 'I':{i=2;break;}

             case 'o': case 'O':{i=3;break;}

             case 'u': case 'U':{i=4;break;}

           }

       if(i>=0)

       num[i]++;

      }

}

void main()

{ char s1[81]; int num1[5], i;

  system("CLS");

  printf("\nPlease enter a string: ");

gets(s1);

  fun(s1, num1);

  for(i=0;i<5;i++) printf("%d ",num1[i]);

printf("\n");

}

答案:(1) void fun(char *s, int num[5]) (2) num[k]=0; (3) switch(*s)

三、程序设计题

在此程序中,编写函数fun,该函数的功能是:求出二维数组周边元素之和,作为函数值返回。二维数组中的值在主函数中赋予。

例如,若二维数组中的值为:

则函数值为61。

#include<conio.h>

#include<stdio.h>

#include<stdlib.h>

#define  M  4

#define  N  5

int fun( int a [M][N])

{

  

}

void main()

{

  FILE *wf;

  int aa[M][N]={{1,3,5,7,9},{2,9,9,9,4},{6,9,9,9,8},{1,3,5,7,0}};

  int i, j, y;

  system("CLS");

  printf ("The original data is :\n ");

  for(i=0; i<M;i++)

     {for (j=0; j<N;j++)

          printf("%6d ",aa[i][j]);

      printf("\n ");

     }

  y=fun(aa);

  printf("\nThe sun: %d\n ",y);

  printf("\n ");

/******************************/

  wf=fopen("out.dat","w");

  fprintf (wf,"%d",y);

  fclose(wf);

/*****************************/

}

答案:

 int i,j,sum=0;

 for(i=0;i<M;i++)

    for(j=0;j<N;j++)

        if(i==0||i==M-1||j==0||j==N-1)   /*只要下标中有一个为0或M-1或N-1,则它一定是周边元素*/

           sum=sum+a[i][j];              /*将周边元素相加*/

 return sum;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值