因此,我被告知要创建一个数组,该数组将接受来自用户的10个整数,并将其存储到一个数组中,然后使用指针冒泡排序按升序对这些值进行排序。
我相信我已经成功地完成了这一部分,但我在第二部分遇到了麻烦。
动态分配另一个包含10个整数的数组。将元素从第一个复制到第二个,但顺序相反(即降序)。按顺序显示第一个和第二个数组中的元素,并释放动态分配的数组。
我能够按顺序显示第一个数组,并且我知道要释放该数组,必须使用delete函数,但我不太确定如何构造动态数组。
*我没有包含这些函数,因为我不认为它们对于这一部分是必要的,但如果我包含了,那么我也会发布它们。
提前感谢您的任何建议和澄清。
#include <iostream>
using namespace std;
void sortArray(int * , int);
void showArray(const int * , int);
int binarySearch(const int *, int, int);
int main(void)
{
int const MAX_NUM = 10;
int numbers [MAX_NUM];
int counter;
int findval;
int index;
char again;
cout<< "Please enter 10 integer values."<< endl;
for(counter=0; counter< MAX_NUM ; counter++)
{
cout << "Enter a value for "<< counter+1 << ": ";
cin >> *(numbers+counter);
}
sortArray(numbers, 10);
cout << endl << "The values in ascending order are: " << endl;
showArray(numbers, 10);
do
{
cout<< endl << "Enter the value you are searching for: ";
cin >> findval;
cout << endl;
index = binarySearch(numbers , MAX_NUM , findval);
// Display the results of the search.
if (index == -1)
cout << "Number was not found." << endl << endl;
else
cout << "Number "<< findval<<" found in position " << index + 1 << endl << endl;
// Does the user want to do this again?
do
{
cout << "Would you like to look up another number? (y/n) ";
cin >> again;
}
while(again != 'y' && again != 'Y' && again != 'n' && again != 'N');
}
while (again == 'Y' || again == 'y');
cout<< endl << "Thank You. Press the return key to continue...";
cin.get();
cin.ignore();
return 0;
}
https://stackoverflow.com/questions/33903026
复制相似问题