2013-04-20 31 views
0

需要對100個隨機數進行冒泡排序,範圍從-100到100,從高到低保持當前的小數位精度。我有一個冒泡排序功能,但不知道如何從另一個函數中調用它。如何將bubblesort函數添加到當前的隨機數生成器?

#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; 

// Functions 
void number_Generator(); 

void bubbleSort (double *array, double length) 
{ 
    int i,j; 
    for (i=0;i<100;i++) 
    { 
     for (j=0;j<i;j++) 
     { 
      if(array[i]>array[j]) 
      { 
       int temp = array[i]; 
       array[i]=array[j]; 
       array[j]=temp; 
      } 
     } 
    } 
} 
int _tmain(int argc, _TCHAR* argv[]) 
{ 
    system("pause"); 
    cout.precision (6); 
    number_Generator(); 
} 

// Number Generator Function 
void number_Generator() 

{ 
    double Final_Avg = 0; 

    double Random_Cap = 100; 
    double Samples_To_Create = 100; 
    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++; 

cout.precision (6); 
cout << fixed << " " << rndDbl << endl; 

} 
cout << " " << endl 
    << "The average = " << fixed << rndAvg/counter << endl 
    << " " << endl 
    << "The Min = " << fixed << rndMin << endl 
    << " " << endl 
    << "The Max = " << fixed << rndMax << endl 
    << " " << endl; 

} // end of number generator function 
+0

您的排序代碼給出參數'length'但是卻忽略了它;你應該可以使用它。你不要在'number_Generator()'的末尾調用'bubbleSort(array,num_entries)'(當然變量名取決於你實現'number_Generator()'的方式) – 2013-04-20 22:47:54

回答

1

你需要做的幾件事情:

添加排序函數調用,如下圖所示:

int _tmain(int argc, _TCHAR* argv[]) 
    { 
     system("pause"); 
     cout.precision (6); 

     int dblArray[100] = {0.0}; //these change are explained below 
     number_Generator(dblArray, 100); //100 means generate 100 random numbers 
        //remove double dblRanAry[100] inside the generator; 
     bubbleSort (dblArray, 100) ; 
    } 

變化number_Generator的原型

void number_Generator(double dblArray[], int length); 

number_Generator應要麼

  1. 返回存儲那些隨機數到主要或
  2. 存儲這些號碼在一個全局數組或
  3. 陣列傳遞到它,並存儲數字作爲我上面那樣的陣列。

您也可以更改您的number_Generator以符合原型更改。

此外:

void bubbleSort (double *array, int length) 
{        //^^array length is int, not double 
    for (int i = 0; i < length; i++) 
    {    //^^use length, not hardcoded 100 
     for (int j = 0; j < i; j++) 
     { 
      if(array[i] > array[j]) 
      { 
       double temp = array[i]; 
       //since array elements are double, not int 
       array[i] = array[j]; 
       array[j] = temp; 
      } 
     } 
    } 
} 
+0

@JonathanLeffler同意。更新帖子,謝謝! – taocp 2013-04-20 22:49:31

+0

你將如何去返回一個數組,將這些隨機數存儲到主 – Mac 2013-04-20 23:41:00

+0

@Mac看到這篇文章?http://stackoverflow.com/questions/3473438/c-return-array-in-a函數 – taocp 2013-04-20 23:42:30