填空题
请补充函数fun(),该函数的功能是;交换数组aa中最大和最小两个元素的位置,结果依然保存在原数组中,其它元素位置不变。注意数组aa中没有相同元素。
例如,输入“33,67,42,58,25,76,85,16,41, 56”,则输出“33,67,42,58,25,76,16,85,41,56”。
注意;部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun()的横线上填入所编写的若干表达式或语句。
试题程序:
#include <stdio. h>
#define N 10
void fun(int aa[])
int i, j, t;
int max=0, min=0;
for(i=0; i<N; i++)
if(
【1】
)
max=i;
if(
【2】
)
min=i;
t=aa [max];
【3】
;
aa [min] =t;
main()
int i;
int aa [N] =33, 67, 42,58,25, 76, 85,16, 41, 56;
clrscr ();
printf("\n*** original list ***\n");
for(i=0; i<N; i++)
printf ("%4d", aa [i] );
fun (aa);
printf ("\n*** new list ***\n");
for(i=0; i<N; i++)
printf ("%4d", aa [i]);
【参考答案】
[1] aa[max]<aa[i] [2]aa[min]>aa[i] [3]aa[max]=aa[min]
点击查看答案&解析
<上一题
目录
下一题>
热门
试题
问答题
请编写函数fun(),该函数的功能是:移动一维数组中的内容,若数组中有n个整数,要求把下标从0到p(p≤n-1)的数组元素平移到数组的最后。 例如,一维数组中的原始内容为1,2,3,4,5,6,7, 8,9,10,11,12,13,14,15,p的值为3。移动后,一维数组中的内容应为5,6,7,8,9,10,11,12,13,14,15, 1, 2, 3, 4。 注意:部分源程序给出如下。 请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。 试题程序: #include <stdio.h> #define N 80 void fun(int *w, int p, int n) main () int a[N]=1,2,3,4,5,6,7,8,9,10,11,12,13,14,15; int i, p, n=15; printf( The original data: n ); for(i=0;i<n;i++) printf( %3d ,a[i]); printf( n nEnter p: ); scanf( %d ,&p); fun(a,p,n); printf( nThe data after moving: n ); for(i=0;i<n;i++) printf( %3d ,a[i]); printf( n n );
点击查看答案&解析
填空题
N个有序整数数列已放在一维数组中,给定下列程序中,函数fun()的功能是:利用折半查找算法查找整数m在数组中的位置。若找到,则返回其下标值:反之,则返回-1。 折半查找的基本算法是:每次查找前先确定数组中待查的范围:low和high(low<high),然后把m与中间位置(mid)中元素的值进行比较。如果m的值大于中间位置元素中的值,则下一次的查找范围放在中间位置之后的元素中;反之,下次查找范围落在中间位置之前的元素中。直到low>high,查找结束。 请改正程序中的错误,使它能得出正确的结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。 试题程序: #include <stdio.h> #define N 10 *************found********************* void fun(int a[],int m) int low--0,high=N-l,mid; while (low<=high) mid=(low+high) 2; if(m<a[mid]) high=mid-1; *************found********************* else if(m>=a [mid]) low=mid+1; else return(mid); return(-1); main () int i,a[N]=-3,4,7,9,13,24,67,89,100,180,k,m; printf ( a数组中的数据如下: ); for(i=0;i<N;i++) printf( %d ,a[i]); printf ( Enter m: ); scanf ( %d , &m); k=fun (a,m); if (k>=0) printf ( m=%d, index=%d n ,m, k); else printf( Not be found! n );
点击查看答案&解析
相关试题
请编写函数fun(),该函数的功能是:移动...
N个有序整数数列已放在一维数组中,给定下...