在具有两个'For Loops'的函数中使用默认参数



我正在尝试按降序和升序对数组进行排序。程序的输出将打印未排序的数组,按降序排序的数组和按升序排序的数组。问题是,我必须声明一个函数并使用默认参数。我不能使用函数重载。这就是我的问题所在。我做了两个for loops,一个按降序,一个按照升序。如果我是函数重载,那么很容易打印出每个函数,我可以只让一个函数是descendingOrder(),另一个是ascendingOrder()。但由于我使用的是一个函数selectionSort(),我不知道如何使一个for loopselectionSort(arr, SIZE)相对应,另一个与selectionSort(arr, SIZE, 1)相对应。这可能是一个很容易回答的问题,但我只是一个初学者,我真的很困惑。谢谢你的帮助。

我的代码运行,但它没有打印我想要的输出。它打印降序两次,而不是先打印降序,然后打印升序。

#include <iostream>
using namespace std;
const int SIZE = 10;
void printArr(int* arr, int len){
for(int i; i < len; i++)
cout << arr[i] << " ";
cout << endl;
}
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void selectionSort(int* arr, int n=0, int var = 0)
{
int i, j, smallest;
//loop for descending order
for (i = 0; i < n-1; i++){
smallest = i;
for (j = i+1; j < n; j ++)
if (arr[j] > arr[smallest])
smallest=j;

swap(&arr[smallest], &arr[i]);
}
//loop for ascending order
for (i=0; i<n-1; i++){
smallest = i;
for(j = i+1; j<n; j++)
if(arr[j] < arr[smallest])
smallest=j;

swap(&arr[smallest], &arr[i]);
}
}
// Declare function "selectionSort" here.
// (using std::swap)
int main()
{
int arr[SIZE] = {5, 13, 6, 1, 32, 65, 87, 23, 14, 88};
cout << "Before sort: " << endl;
printArr(arr, SIZE);
cout << "Sorted (descending order): " << endl;
selectionSort(arr, SIZE);
printArr(arr, 10);
cout << "Sorted (ascending order): " << endl;
selectionSort(arr, SIZE, 1);
printArr(arr, 10);
return 0;
}

请注意;函数";是以下行:

if (arr[j] > arr[smallest])

你可以简单地使这个条件依赖于var,如下所示:

if (var ? arr[j] < arr[smallest] : arr[j] > arr[smallest])

相当于:

if ((var && arr[j] < arr[smallest]) || (!var && arr[j] > arr[smallest]))

只写一个";函数";。

我为循环做了两个,一个用于降序,另一个用于升序。[…]由于我使用的是一个函数selectionSort(),我不知道如何使一个for loop对应于selectionSort(arr, SIZE),另一个对应于selectionSort(arr, SIZE, 1)

;"容易";方法可以是使用if

if ( val == 0 ) {
//loop for descending order
for (i = 0; i < n-1; i++) {
// ...
}
}
else {
//loop for ascending order
for (i=0; i<n-1; i++) {
// ...     
}
}

但这是很多容易出错的代码重复。

另一种不同的方法,我认为对于可能对语言功能了解有限的初学者来说是可以理解的(阅读:有更好的方法,但你稍后会学习(,包括使用比较函数。

#include <iostream>
using namespace std;
const int SIZE = 10;
void printArr(int* arr, int len){
for(int i = 0; i < len; i++)
//      ^^^^^                  Note the initialization.
cout << arr[i] << " ";
cout << endl;
}
// It returns 1 if a < b, -1 if b < a or 0 if a == b
int compare_int(int a, int b)
{
return (a < b) - (b < a);
}
// Declare function "selectionSort" here.
// ^^^^^^^
void selectionSort( int* arr
, int n
, int var = -1 );
int main()
{
int arr[SIZE] = {5, 13, 6, 1, 32, 65, 87, 23, 14, 88};
cout << "Before sort: " << endl;
printArr(arr, SIZE);
cout << "Sorted (descending order): " << endl;
selectionSort(arr, SIZE);
printArr(arr, SIZE);
cout << "Sorted (ascending order): " << endl;
selectionSort(arr, SIZE, 1);
printArr(arr, SIZE);
return 0;
}
// (using std::swap)
void selectionSort( int* arr
, int n
, int var )
{
for ( int i = 0; i < n - 1; ++i ) {
int smallest = i;
for ( int j = i + 1; j < n; ++j ) {
if ( compare_int(arr[j], arr[smallest]) == var ) {
//   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                 
smallest = j;
}
}   
std::swap(arr[smallest], arr[i]);
}
}

最新更新