问题 问答题

请编写一个函数resort,该函数的功能是:能在一个数列中,对从指定位置开始的几个数,按相反顺序重新排列,并在主函数中输出新的数列。 注意:部分源程序已存在文件PROC4.cpp中。 请勿修改主函数和其他函数中的任何内容,仅在函数reson()的花括号中填写若干语句。 文件PROC4.cpp的内容如下: //PROC4.cpp #include<iostream> using namespace std; void resort(int array[],int where,int arrount); int main() { int number[20],where, arrount,i; cout<<"input 20 numbers\n"; for(i=0;i<20;i++)cin>>number[i]; cout<<"how many do you want to sort:"; cin>>arrount; cout<<"where do you want to start:"; cin>>where; resort(number,where,arrount); cout<<"\n resorted array as follow:\n"; for(i=0;i<20;i++)cout<<number[i]; return 0; } void resort(int array[ ],int where,int amount) { /************/ }

答案

参考答案:

解析:下面是函数resort的函数体实现: void resort(int array[ ],int where,int amount) { int *p1,*p2,temp; p1=&array[where-1]; p2=&array[where-2+amount]; for(;p1<&array[where-1+amount/2];p1++,p2--) { temp=*p1; *p1=*p2; *p2=temp; } } 函数resort的主要功能是将指定的几个数据按原顺序相反的顺序重新排列。 可以采用循环加数组的方式实现。

口语交际,情景问答题
判断题