我正在生成从 -100 到 100 的随机数,我试图按升序和降序对 # 进行排序,但是当显示结果时,只显示第一个和最后一个数字?
// sorting using function pointers.
#include "stdafx.h"
#include <fstream> // writing data to disk
#include <cstdlib> // standard general utilities library "# generator"
#include <ctime> // convert time value to string
#include <iostream>
#include <iomanip> // set precision
using namespace std;
// prototypes
void selectionSort( int [], const int, bool (*)( int, int ) );
void swap( int * const, int * const );
bool ascending( int, int ); // implements ascending order
bool descending( int, int ); // implements descending order
int _tmain(int argc, _TCHAR* argv[])
{
// Number Generator
{
double Final_Avg = 0;
double Random_Cap = 100;
double Samples_To_Create = 99;
srand((unsigned)time(0));
double rndDbl;
int rndInt;
double rndAvg = 0, rndMin = 0, rndMax = 0;
int counter = 0;
double temp = 0;
double dblRanAry[100];
Final_Avg = rndAvg / counter; // final average to display
double lDbl=0, hDbl=Random_Cap;
int lInt = 0, hInt=1;
double dblRange=(hDbl-lDbl)+1;
int intRange=(hInt-lInt)+1;
for(int index=0; index<Samples_To_Create; index++)
{
rndInt = lInt+int(intRange*rand()/(RAND_MAX + 1.0));
rndDbl = lDbl+double(dblRange*rand()/(RAND_MAX + 1.0));
// random number if statement
if (rndInt == 0){
rndDbl = -(rndDbl);
} //start of Min/Max if statements
if (rndMin == 0){
rndMin = rndDbl;
}
else if (rndDbl < rndMin){
rndMin = rndDbl;
}
if (rndMax == 0){
rndMax = rndDbl;
}
else if (rndDbl > rndMax){
rndMax = rndDbl;
} //end of Min Max if statements
temp = rndDbl;
rndAvg += temp;
dblRanAry[counter] = temp;
counter++;
}
const int arraySize = 100;
int order; // 1 = ascending, 2 = descending
int counter1; // array index
// ** 我认为问题出在这里 **
int a[ arraySize ] = { rndDbl };
cout << "Enter 1 to sort in ascending order,\n"
<< "Enter 2 to sort in descending order: ";
cin >> order;
cout << "\nData items in original order\n";
// output original array
for ( counter1 = 0; counter1 < arraySize; ++counter1 )
cout << setw( 4 ) << a[ counter1 ];
// sort array in ascending order; pass function ascending
// as an argument to specify ascending sorting order
if ( order == 1 )
{
selectionSort( a, arraySize, ascending );
cout << "\nData items in ascending order\n";
} // end if
// sort array in descending order; pass function descending
// as an argument to specify descending sorting order
else
{
selectionSort( a, arraySize, descending );
cout << "\nData items in descending order\n";
} // end else part of if...else
// output sorted array
for ( counter1 = 0; counter1 < arraySize; ++counter1 )
cout << setw( 4 ) << a[ counter1 ];
cout << endl;
system("pause");
}
} // end main
// multipurpose selection sort; the parameter compare is a pointer to
// the comparison function that determines the sorting order
void selectionSort( int work[], const int size,
bool (*compare)( int, int ) )
{
int smallestOrLargest; // index of smallest (or largest) element
// loop over size - 1 elements
for ( int i = 0; i < size - 1; ++i )
{
smallestOrLargest = i; // first index of remaining vector
// loop to find index of smallest (or largest) element
for ( int index = i + 1; index < size; ++index )
if ( !(*compare)( work[ smallestOrLargest ], work[ index ] ) )
smallestOrLargest = index;
swap( &work[ smallestOrLargest ], &work[ i ] );
} // end if
} // end function selectionSort
// swap values at memory locations to which
// element1Ptr and element2Ptr point
void swap( int * const element1Ptr, int * const element2Ptr )
{
int hold = *element1Ptr;
*element1Ptr = *element2Ptr;
*element2Ptr = hold;
} // end function swap
// determine whether element a is less than
// element b for an ascending order sort
bool ascending( int a, int b )
{
return a < b; // returns true if a is less than b
} // end function ascending
// determine whether element a is greater than
// element b for a descending order sort
bool descending( int a, int b )
{
return a > b; // returns true if a is greater than b
} // end function descending