填空题 C语言
题号 #2257216

请补充函数proc(),该函数的功能是:把数组aa中元素
下标为奇数的元素按从大到小的顺序重新保存在原数组中,其他元素位置不变。
例如,若数组aa中的元素为:“45 65 40 32 13 57 88 90 67 99”,则输出结果为:“45 99 40 90 13 65 88 57 67 32”。
注意:部分源程序中的给出如下。
请勿改动main()函数和其他函数中的任何内容,仅在函数proc()的横线上填入所编写的若干表达式或语句。
试题程序:
#include<stdlib.h> #include<stdio.h> #define M 10
void proc(int aa[])
{
int i,j,t;
for(【1】 ;i<M;i=i+2)
{
for(j=i;j<M;j=j+2)
if(aa[i]<aa[j])
{
【2】 ;
aa[j]=aa[i];
【3】 ;
}
}
}
void main()
{
int i;
int aa[M]={45,65,40,32,13,57,88,90,67,99);
system("CLS");
printf("\n***original list***\n");
for(i=0;i<M;i++)
printf("%4d",aa[i]);
proc(aa);
printf("\n***new list***\n");
for(i=0;i<M;i++)
printf("%4d",aa[i]);
}

进入在线模考

参考答案

暂无

答案解析

【1】i=1【2】t=aa[j]【3】aa[i]=t
【解析】按题目中要求将数组中元素下标为奇数的元素按从大到小的顺序排列,所以变量i的初始值为1,因此,【1】处填“i=1”;将顺序不符合要求的两元素互换,因此,【2】处填“t=aa[j]”.【3】处填“aa[i]=t”。

你可能感兴趣的试题

3 请补充main()函数,该函数的功能是:把文本文件text2中的内容追加到文本文件text1的内容之后。
例如,文件text2的内容为“I’m very happy!”,文件textl的内容为“I’m a girl,”,追加之后文件textl的内容为“I’m a girl,I’m very happy”。
注意:部分源程序给出如下。
请勿改动main()函数和其他函数中的任何内容,仅在函数main()的横线上填入所编写的若干表达式或语句。
试题程序:
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#define M 80
void main()
{
FILE*fp,*fp1,*fp2;
int i;
char c[M],ch;
system("CLS");
if((fp=fopen("text1.dat","r"))==NULL)
{
printf("file text1 cannot be opened\n");
exit(0);
}
printf("\n textl contents are:\n\n");
for(i=0;(ch=fgetc(fp))!=EOF;i++)
{
c[i]=ch:
putchar(c[i]);
}
fclose(fp);
if((fp=fopen("text2.dat","r"))==NULL)
{
printf("file text2 cannot be opened\n");
exit(0);
}
printf("\n\n\nB contents are:\n\n");
for(i=0;(ch=fgetc(fp))!=EOF;i++)
{
c[i]=ch;
putchar(c[i]):
}
fclose(fp);
if((fp1=fopen("text1.dat","a")) 【1】 (fp2=
fopen("text2.dat","r")))
{
while((ch=fgetc(fp2))!=EOF)
【2】 ;
}
else
{
printf("Can not open textl text2 1\n");
}
fclose(fp2);
fclose(fp1);
printf("\n***"lew text1 contents***\n\n”);
if((fp=fopen("text1.dat","r"))==NULL)
{
printf("file text1 cannot be opened\n");
exit(0);
}
for(i=0;(ch=fgetc(fp))!=EOF;i++)
{
c[i]=ch;
putchar(c[i]):
}
【3】 ;
}
找试题 · 找答案 · 找资料 在线模考