帮忙做一下C++数组方面的编程题

来源:百度知道 编辑:UC知道 时间:2024/04/29 06:30:35
完成一个程序,该程序要求用户输入整数,并按照输入顺序记录进一个数组。当用户输入-1时标识结束输入结束。程序将记录的数组的顺序进行反序,即原数组第一个数与最后一个数进行交换,依此类推。反序后的数组进行输出在屏幕上。最后对数组进行从小到大排序,并输出排序结果。程序的一次运行结果如下。
Enter numbers, one per line, ending with the
sentinel value -1 The program will then
display those values in reverse order and sorted order.
? 9
? 3
? 7
? 9
9 is already in the array. Please input again
? 3
3 is already in the array. Please input again
? 1
? 2
? 6
? 19
? -1
The reverse array is as follows:
19 6 2 1 7 3 9
The sorted array is as follows:
1 2 3 6 7 9 19
输入的数如果和先前的重复,就会出现 is already in the array. Please input again。

#include <iostream>
using namespace std;
int main()
{
int a[32374],i=0,j,n,k,biaozhi=0;
cout<<"Enter numbers, one per line, ending with the\n"<<
"sentinel value -1 The program will then \n"<<
"display those values in reverse order and sorted order. "<<endl;
while(n!=-1)
{
biaozhi=0;
cout<<"?";
cin>>n;
for(int x=0;x<=i;x++)
{
if(n==a[x])
{
biaozhi=1;
cout<<n<<" is already in the array. Please input again."<<endl;
break;
}

}
if(n!=-1&&biaozhi==0)
{
a[i]=n;

i++;
}

}
for(j=0,k=i-1;j<=k,k>=j;j++,k--)
{
int temp=a[j];
a[j]=a[k];
a[k]=temp;
}
cout<<"The reverse array is as follows: ";
for(j=0;j<i;j++)
{
cout<