请编写一个函数resort,该函数的功能是:能在一个数列中,对从指定位置开始的几个数,按相反顺序重新排列,并在主函数中输出新的数列。 注意:部分源程序已存在文件PROC4.cpp中。 请勿修改主函数和其他函数中的任何内容,仅在函数reson()

admin2009-01-15  94

问题 请编写一个函数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;
      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;
      return 0;
   }
   void resort(int array[  ],int where,int amount)
   {
     /************/
   }

选项

答案下面是函数reson的函数体实现: 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; } }

解析 函数reson的主要功能是将指定的几个数据按原顺序相反的顺序重新排列。
   可以采用循环加数组的方式实现。
转载请注明原文地址:https://jikaoti.com/ti/qYkiFFFM
0

最新回复(0)