填空题 函数fun()的功能是:逆置数组元素中的值。形参n给出数组中数据的个数。
例如:若a所指数组中的数据依次为:1、2、3、4、5、6、7、8、9,则逆置后依次为:9、8、7、6、5、4、3、2、1。
注意:部分源程序给出如下。
请勿改动main()函数和其他函数中的任何内容,仅在fun()函数的横线上填入所编写的若干表达式或语句。
试题程序:
#include<stdio.h>
void fun(int a[], int n)
{
int i, t;
for(i=0; i< 1; i++)
{
t=a[i];
a[i]=a[n-1- 2];
3=t;
}
}
void main()
{
int b[9]={1, 2, 3, 4, 5, 6, 7, 8, 9}, i;
printf("/nThe original data: /n");
for(i=0; i<9; i++)
printf("%4d", b[i]);
printf("/n");
fun(b, 9);
printf("/nThe data after invert: /n");
for(i=0; i<9; i++)
printf("%4d", b[i]);
printf("/n");
}
【正确答案】
【答案解析】n/2 i a[n-i-1][解析] fun()函数实现的功能是实现数组的逆置。具体步骤通过参数传递,得到数组指针(即数组名)和数组记录大小的变量n,通过这两个参数配置for循环,遍历数组的中间的元素位置,每次循环都将i位置和对应位置结点(i距离0位置等于其距离尾位置的结点)的元素交换,由此实现数组的逆置。