一、程序填空题
在此程序中,函数fun的功能是:3*4的矩阵中找出在行上最大、在列上最小的那个元素,若没有符合条件则输出相应信息。
例如,有下列矩阵:
程序执行结果为:find: a[2][2]=9
#include <stdio.h>
#define M 3
#define N 4
void fun(int (*a)[N])
{ int i=0,j,find=0,rmax,c,k;
while( (i<M) && (!find))
{ rmax=a[i][0]; c=0;
for(j=1; j<N; j++)
if(rmax<a[i][j]) {
/**********found**********/
rmax=a[i][j]; c= __1__ ; }
find=1; k=0;
while(k<M && find) {
/**********found**********/
if (k!=i && a[k][c]<=rmax) find= __2__ ;
k++;
}
if(find) printf("find: a[%d][%d]=%d\n",i,c,a[i][c]);
/**********found**********/
__3__ ;
}
if(!find) printf("not found!\n");
}
void main()
{ int x[M][N],i,j;
printf("Enter number for array:\n");
for(i=0; i<M; i++)
for(j=0; j<N; j++) scanf("%d",&x[i][j]);
printf("The array:\n");
for(i=0; i<M; i++)
{ for(j=0; j<N; j++) printf("%3d",x[i][j]);
printf("\n\n");
}
fun(x);
}
答案:(1) j (2) 0 (3) i++
二、程序修改题
在此程序中,函数fun的功能是:根据整形参m的值,计算如下公式的值。
例如,若m中的值为5,则应输出0.536389。
#include <stdio.h>
double fun ( int m )
{ double y = 1.0 ;
int i ;
/**************found**************/
for(i = 2 ; i < m ; i++)
/**************found**************/
y -= 1 /(i * i) ;
return( y ) ;
}
void main( )
{ int n = 5 ;
printf( "\nThe result is %lf\n", fun ( n ) ) ;
}
答案:(1) for(i = 2 ; i <= m ; i++) (2) y -= 1.0/(i * i);
三、程序设计题
在此程序中,m个人的成绩存放在score数组中,请编写函数fun,它的功能是:将低于平均分的分数放在below所指的数组中。
例如,当score数组中的数据为10、20、30、40、50、60、70、80、90时,函数返回的人数应该是4,below中的数据应为10、20、30、40。
#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int fun(int score[],int m, int below[])
{
}
void main()
{
FILE *wf;
int i, n, below[9];
int score[9]={10,20,30,40,50,60,70,80,90};
system("CLS");
n=fun(score, 9, below);
printf("\nBelow the average score are: ");
for(i=0;i<n;i++)
printf("%d ",below[i]);
/******************************/
wf=fopen("out.dat","w");
for(i=0;i<n;i++)
fprintf(wf,"%d ",below[i]);
fclose(wf);
/*****************************/
}
答案:
int i,j=0;
float av=0.0;
for(i=0;i<m;i++)
av=av+score[i]/m; /*求平均值*/
for(i=0;i<m;i++)
if(score[i]<av) /*如果分数低于平均分,则将此分数放入below数组中*/
below[j++]=score[i];
return j; /*返回低于平均分的人数*/