0
我正在生成從-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
已經有詳細解釋[這裏](http://stackoverflow.com/questions/201101/how-to-initialize-an-array-in-c) – alexrider 2013-04-20 22:57:28
排序和輸出雙打,而不是整數。整數本質上沒有小數部分。 – alexrider 2013-04-20 23:13:05
感謝您的評論 – Mac 2013-04-21 00:34:29