El *_*ffy 0 c++ arrays sorting dynamic-allocation
所以我被告知要创建一个数组,该数组将接受来自用户的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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
652 次 |
| 最近记录: |