C++——用指向指针的指针的方法对n个整数排序并输出。要求将排序单独写成一个团数,整数和n在主函数中输人,最后在主函数中输出。
没注释的源代码
#include <iostream>
using namespace std;
void sortNumbers(int** arr, int n);
int main()
{
int n;
cout<<"enter the number of integers:";
cin>>n;
int** arr=new int*[n];
for (int i=0; i<n;i++)
{
arr[i] = new int;
cout<<"enter the integers "<<i+1<<": ";
cin>>*(arr[i]);
}
sortNumbers(arr, n);
cout<<"Sorted integers:"<<endl;
for (int i=0;i<n;i++)
{
cout<<*(arr[i])<< " ";
}
cout<<endl;
for (int i=0;i<n;i++)
{
delete arr[i];
}
delete[] arr;
return 0;
}
void sortNumbers(int** arr, int n)
{
for (int i=0; i<n-1;i++)
{
for (int j=0; j<n-i-1;j++)
{
if (**(arr+j)>**(arr+j+1))
{
int* temp = *(arr+j);
*(arr+j)=*(arr+j+1);
*(arr+j+1)=temp;
}
}
}
}