《C语言及程序设计》程序阅读——查找和排序
返回:贺老师课程教学链接
1、下面的程序是输出数组中最大元素的下标(p表示最大元素的下标),请将程序补充完整。
#include<stdio.h>
int main()
{
______(1)_______;
int s[]= {1,-3,0,-9,8,5,-20,3};
for(i=0,p=0; i<8; i++)
if(s[i]>s[p])
_____(2)_____;
_____(3)_____;
return 0;
}
2、下面程序用“顺序查找法”查找数组a中是否存在某一关键字,请将程序补充完整。
#include<stdio.h>
int main()
{
int a[10]= {25,57,48,371,123,291,18,22,44,21,56};
int i, x ;
scanf("%d",&x);
_____(1)_____;
while (i<10&&a[i]!=x)
i++;
if (_____(2)_____)
printf("found=%d\n", i);
else
printf("can\'t found!\n");
return 0;
}
3、有些场合,数组中下标为0的元素经常“空闲”或留作他用。例如下面程序中用于保存学生成绩的数组,有用的数据共有n=10个,但数组大小定义为n+1,而a[0]的值,并不代表任何同学的成绩。现在要将a[1]~a[10]共10个元素用冒泡法排序,请将程序中的空补充完整。
#include <stdio.h>
#define n 10
int main( )
{
int i,j,t,a[n+1]= {-1,98,75,69,81,53,68,92,76,100,93};
for (i=1; _____(1)_____; i++)
for(j=1; _____(2)_____; j++)
if (a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
for(i=1;_____(3)_____; i++) //输出排序后结果
printf("%d ", a[i]);
return 0;
}
参考解答:侧(zuo)脖(wan)子(zai)看(kan)