以下程序中函数f的功能是在数组x的n个数(假定n个数互不相同)中找出最大最小数,将其中最小的数与第一个数对换,把最大的数与最后一个数对换.请填空.
#include <stdio.h>
viod f(int x[],int n)
{ int p0,p1,i,j,t,m;
i=j=x[0]; p0=p1=0;
for(m=0;m<n;m++)
{ if(x[m]>i) {i=x[m]; p0=m;}
else if(x[m]<j) {j=x[m]; p1=m;}
}
t=x[p0]; x[p0]=x[n-1]; x[n-1]=t;
t=x[p1];x[p1]= _[14]_______; _[15]_______=t;
}
main()
{ int a[10],u;
for(u=0;u<10;u++) scanf("%d",&a[u]);
f(a,10);
for(u=0;u<10;u++) printf("%d",a[u]);
printf("\n");
}
x[0] x[0]
第1题:
下列给定程序中,函数fun()的功能是求出数组中最小数和次最小数,并把最小数和a[0]中的数对调,次最小数和a[1]中的数对调。
请改正程序中的错误,使它能得到正确结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
include <conio.h>
include <stdio.h>
define N 20
void fun(int *a,int n)
{
int i, m,t,k;
/*************found**************/
for(i=O; i<n; i++)
{
m==i;
for(k=i; k<n; k++)
if(a[k]<a[m])
/*************found**************/
k=m;
t=a[i];
a[i]=a[m];
a[m]=t;
}
}
main()
{
int x,b[N]={11,5,12,0,3,6,9,7,10,8},
n=10,i;
clrscr();
for(i=0; i<n; i++)
printf("%d",b[i]);
printf("\n");
fun(b,n);
for(i=0; i<n; i++)
printf("%d",b[i]);
printf("\n");
}
第2题:
数组Q[0,n-1]作为一个环形队列,f为当前队头元素的前一位置,r为队尾元素的位置,假定队列中元素的个数总小于n,队列中元素的个数是
A.r-f
B.n+f-r
C.n+r-f
D.(n+r-f)mod n
第3题:
以下程序中函数 f 的功能是在数组 x 的 n 个数 ( 假定 n 个数互不相同 ) 中找出最大最小数 , 将其中最小
的数与第一个数对换 , 把最大的数与最后一个数对换 . 请填空 .
#include <stdio.h>
viod f(int x[],int n)
{ int p0,p1,i,j,t,m;
i=j=x[0]; p0=p1=0;
for(m=0;m<n;m++)
{ if(x[m]>i) {i=x[m]; p0=m;}
else if(x[m]<j) {j=x[m]; p1=m;}
}
t=x[p0]; x[p0]=x[n-1]; x[n-1]=t;
t=x[p1]; x[p1]= _[14]_______ ; _[15]_______ =t;
}
main()
{ int a[10],u;
for(u=0;u<10;u++) scanf("%d",&a[u]);
f(a,10);
for(u=0;u<10;u++) printf("%d",a[u]);
printf("\n");
}
第4题:
请编写函数void fun(int x, int pp[], int *n),它的功能是:求出能整除x且不是偶数的各整数,并按从小到大的顺序放在即所指的数组中,这些除数的个数通过形参n返回。
例如,若x中的值为30,则有4个数符合要求,它们是1, 3,5,15。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:
include <conio.h>
include <stdio.h>
void fun (int x, int pp[], int *n)
{
}
main()
{
int x,aa[1000], n, i ;
clrscr();
printf("\nPlease enter an integer
number : \n ") ;
scanf ("%d", &x) ;
fun (x, aa, &n) ;
for (i=0 ; i<n ; i++)
printf ("%d ", aa [i]);
printf ("\n ") ;
}
第5题:
下面函数的功能是:找出一维数组元素中最大的值和它所在的下标,最大值的和它所在的下标通过形参传回。数组元素中的值已在主函数中赋予。主函数中x是数组名,n是x中的数据个数,max存放最大值,index存放最大值所在元素的下标。请填空。
#include<stdlib.h>
#include<stdio.h>
void fun(int a[],int n, int *max, int *d)
{ int i;
*max=a[0];
*d=0;
for(i=0;【15】;i++)
if(*max<【16】)
{*max=a[i];*d=i;}
}
main()
{ int i, x[20], max, index, n=10;
randomize();
for(i=0;i<n;i++)
{
x[i]=rand()%50; printf("%4d",x[i]);
}
printf("\n");
fun(x,n,&max,&index);
printf("Max=%5d,Index=%4d\n",max,index);
}
第6题:
若有如下程序: SET TALK OFF INPUT TO X FOR i=1 TO 3 INPUT TO Y IF Y>X X=Y ENDIF ENDFOR ?X RETURN 本程序的功能是( )。
A.求3个数中的最大值
B.求4个数中的最大值
C.求3个数中的最小值
D.求4个数中的最小值
第7题:
以下函数的功能是求出能整除x且不是偶数的各整数,并按从小到大的顺序放在pp所指的数组中,这些除数的个数通过形参n返回。
例如,若x中的值为30,则有4个数符合要求,它们是1,3,5,15。
请按题意,完成填空。
试题程序:
#include <conio.h>
#include <stdio.h>
void fun (int x, int pp[], int *n)
{ int i,j=0;
【13】
if(x%i==0)pp[j++]=i;
【14】;
}
main ()
{ int? x,aa[1000], n, i ;
clrscr();
printf("\nPlease enter an integer number : \n "); scanf ("%d ", &x);
fun (x, aa, &n);
for (i=0 ; i<n ; i++)
printf ("%d ", aa [i]);
printf ("\n ");
}
第8题:
以下程序中函数 fun 的功能是:统计 person 所指结构体数组中所有性别 (sex) 为 M 的记录的个数 , 存入变量 n 中,并做为函数值返回。请填空:
#include <stdio.h>
#define N 3
typedef struct
{ int num;char nam[10]; char sex;} SS;
int fun(SS person[])
{ int i,n=0;
for(i=0;i<N;i++)
if( 【 14 】 =='M') n++;
return n;
}
main()
{ SS W[N]={{1, "AA", 'F'},{2, "BB",'M'},{3,"CC", 'M'}}; int n;
n=fun(W); printf("n=%d\n",n);
}
第9题:
阅读以下函数说明和C语言函数,将应填入(n)处的字句写在对应栏内。
【程序2.1说明】
已知一个排好序的数组,现输入一个数,要求按原来的顺序规律,将它插入到数组中。
【程序2.1】
include <stdioh>
define N 100
void main()
{
float a[N+l],x;
int i,p;
printf("输入已经排好序的数列: ");
for(i=0; i<N; i++)
scanf(%f",&a[i]);
printf("输入要插入的数:");
scanf("%f",&x);
for(i=0,p=N; i<N; i++)
if(x<a[i])
{
(1)
break;
}
for(i=N-1; i>=p; i--)
(2)
(3)
for(i=0; i<=N; i++)
prinff("%f\t",a[i]);
}
【程序2.2说明】
本程序用变量count统计文件中字符的个数。
【程序2.2】
include <stdio.h>
include <stdlib.h>
void main()
{
FILE *fp;
long count=0;
if((fp=fopen("letter.txt","r"))==NULL)
{
printf("can not open file\n");
exit(0);
}
while(!feof(fp))
{
(4)
count++;
}
printf("count=%d\n",count);
(5)
}
第10题:
以下fun函数的功能是:找出具有N个元素的一维数组中的最小值,并作为函数值返回,请填空。(设N己定义)
int fun(int x[N])
{int i,k=0
for(i=0;i<N;i++)
if(x[i]<x[k])k=_____;
return x[k];
}