如何将冒泡排序函数添加到当前随机数生成器

How to add bubblesort function to this current random number generator?

本文关键字:随机数生成器 添加 函数 冒泡排序      更新时间:2023-10-16

需要对100个从-100到100的随机数从高到低进行冒泡排序,同时保持当前的小数点精度。我有一个冒泡排序函数,但我不确定如何从另一个函数内调用它。

 #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

您需要做几件事:

添加排序函数调用,如下所示:

  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. 返回一个数组,存储这些随机数到main或
  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;
            }
        }
    }
}