问答题

使用VC6打开考生文件夹下的工程RevProj8。此工程包含一个源程序文件 RevMain8.cpp。在该文件中,函数resort的功能是:能在一个数列中,对从指定位置开始的几位数,按相反顺序重新排列,并在主函数中输出新的序列。
请改正程序中的错误,使它能得到正确结果。
注意,不要改动main函数,不得删行或增行,也不得更改程序的结构。
源程序文件RevMain8.cpp中的程序清单如下:
//RevMain8.cpp
#include <instream>
using namespace std;
void resort(int arr[],int where,int amount);
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<<"\n where do you want to start: ";
cin>>where;
cout<<"old array as follow:\n";
for (i=0; i<20; i++)
cout<<nmuber [i] <<" ";
resort (number,where, arrount);
cout<<"\n resorted array as follow:\n";
for (i=0; i<20; i++)
cout<<number [i] <<" ";
cout<<end1;
return 0;

void resort(int array[],int where, int amount)

int *pi, *p2, temp;
p1=&array [where-1];
p2=&array [where-2+amount];
/* * * * *FOUND * * * * */
for (;p1<&array [where-1+amount/2]; )

/* * * * *FOUND * * * * */
*p1=*p2;
*p2=*p1;

return;

【参考答案】


正确的resort()函数如下:
void resort(int array[],int where,......

(↓↓↓ 点击下方‘点击查看答案’看完整答案、解析 ↓↓↓)
热门 试题

问答题
使用VC6打开考生文件夹下的工程MyProj8。此工程包含一个源程序文件MyMain8.cpp,该程序实现栈的入栈和出栈的操作。其中有两个类:一个是节点类node,它包含节点值和指向上一个节点的指针prey;另一个类是栈类stack,它包含栈的头指针top。但类的定义并不完整。 请按要求完成下列操作,将类Sample的定义补充完成: ①定义私有节点值data,它是血型的数据,以及定义一个指向上一个节点的指针prev。请在注释“ * *1* *”之后添加适当的语句。 ②完成构造函数node(int d,node*n)的定义,使得私有成员data和prev分别初始化为d和n。请在注释“ * *2* *”之后添加适当的语句。 ③完成类stack的成员函数push(int i)的类体内的定义。函数push()实现入栈这个操作,即把形参i压入栈中,那么此时应该创建一个新的节点,并让这个节点的prev指针指向栈顶。请在注释“ * *3 * *”之后添加适当的语句。 注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。 源程序文件MyMain8.cpp清单如下: MyMain 8.cpp #include <iostream> using namespace std; class stack; class node private: * * 1 * * public: node(int d, node *n) * * 2 * * friend class stack; ; class stack node *top; 栈头 public: stack() top=0; void push(int i) * * 3 * * int pop() node*t=top; if(top) top=top->prev; int c=t->data; delete t; return c; return 0; ; int main() stack s; s.push(6); s.push(3); s.push(1); return 0;