填空题
请补充函数fun(),该函数的功能是:把数给aa中元素下标为奇数的元素按从大到小的顺序重新保存在原数组中,其它元素位置不变。
例如,输入“33,67,42,58,25,76,85,16,41, 56”,则输出“33,76,42,67,25,58,85,56,41,16”。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun()的横线上填入所编写的若干表达式或语句。
试题程序:
#include <stdio.h>
#define N 10
void fun(int aa[])
int i, j, t;
for (
【1】
; i<N; i=i+2)
for(j=i; j<N; j=j+2)
if (aa [i]<aa[j])
【2】
;
aa [j]=aa[i];
【3】
;
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] i=1 [2] t=aa[j] [3] aa[i]=t
点击查看答案&解析
<上一题
目录
下一题>
热门
试题
问答题
下列程序定义了N×N的二维数组,并在主函数中自动赋值。请编写函数fun(int a[][N],int n),该函数的功能是:使数字右上半三角元素中的值乘以m。 例如,若m的值为2,a数组中的值为 a=1 9 2 7 则返回主程序后a数组的值应为 2 18 2 14 注意:部分源程序给出如下. 请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。 试题程序: #include<conio.h> #include<stdio.h> #include<stdlib.h> #define N 5 int fun(int a[] [N], int m) main() int a[N] [N],m, i, j; clrscr (); printf *****The array***** n ); for (i=O; i<N; i++) *输出一个随机的5×5矩阵* for(j=0; j<N; j++) a[i] [j]=rand()%20; printf( %4d , a[i] [j]); printf ( n ); do m=rand () %10; while (m>=3); *产生一个上于3的随机数* printf ( m=%4d n , m); fun (a,m); printf ( THE RESULT n ); for (i=0; i<N; i++) for(j=0;j<N;j++) printf ( %4d ,a[i] [j]); printf ( n );
点击查看答案&解析
填空题
下列给定程序中,函数fun()的功能是:从N个字符串中找出最长的那个中,并将其地址作为函数值返回。各字符串在主函数中输入,并放入一个字符串数组中。 请改正程序中的错误,使它能得出正确的结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。 试题程序: #include <string.h> #include <stdio.h> #define N 5 #define M 81 *************found************** fun(char (*sq) [N]) int i; char *sp; sp=sq[0]; fori=0;i<N;i++) if(strlen(sp)<strlen(sq[i])) sp=sq[i]; *************found************** return sq; main () char str[N][M], *longest; int i; printf( Enter %d lines: n ,N); for(i=0;i<N;i++) gets(str[i]); printf( nThe %d string : n ,N); for(i=0;i<N;i++) puts(str[i]); longest=fun(str); printf( nThe longest string : n ); puts(longest);
点击查看答案&解析
相关试题
下列程序定义了N×N的二维数组,并在主函数...
下列给定程序中,函数fun()的功能是:从N...