C++ 数组,数组转换不起作用

C++ Arrays, Array Conversion not working

今天我被一个问题困扰了很长一段时间,尽管我在网上搜索,但我不确定我应该做什么。这个程序(我将 post 源代码)应该基于生日悖论,并帮助我们证明它是正确的。

想法是我们有多个数组,其中一个通过计算没有匹配生日对的次数来模拟悖论,另一个采用该数组并创建一个比率(数组 value/over 总计迭代)和另一个创建理论阵列比率值的方法。

现在,这就是我卡住的地方。我可以让前两个功能完美运行,birthdayFrequencyratioArray,但下一个 idealArray 我无法获得才能正常工作。ratioArrayidealArray 都应该是 double 数据类型,ratioArray 将其正确存储为 double。

然而,idealArray 没有。它将数组位置中的数据存储为整数。我想知道是否遗漏了什么,可能导致我不小心将数组设为整数。

*我要道歉了,代码真的很长。此外,我无法将其全部放入代码 window。对不起。

using namespace std;

//Arraytype declarations

typedef int Birthday[365];

typedef bool Counter[365];
typedef double Ratio[365];

void ratioArray(int, Birthday, Ratio);
void idealArray(Ratio);
void outputTable();
int randomInt(int);



//Main function

int main()


{

Birthday array = {0};
Ratio vector = {0};
Ratio matrix = {0};


int seed;
int runs;

//Prompt the user for the number of times the simulation is to be run.

cout << "Hello and welcome to the Birthday Paradox Simulator. " << endl;
cout << "This program uses simulated runs to calculate and verify the paradox." << endl << endl;
cout << "Please enter the number of runs you want done. IMPORTANT, must be a positive integer." << endl;
cin >> runs;

while (runs <= 0)
    {
        cout << "That's an invalid value. Please enter a positive number." << endl;
        cin >> runs;
    }

//Prompt the user for a non-negative integer seed value

cout << "Please input a seed to be used for randomly generating numbers. It must be an integer, and non-negative." << endl;
cin >> seed; 

while (seed < 0)
    {
        cout << "I'm sorry, that is an invalid value. Please enter a non-negative number)" << endl;
        cin >> seed;
    }

//Seed the srand function

srand(seed);

//Call birthdayFrequency function

birthdayFrequency(runs, array);

//Call ratioArray function

ratioArray(runs, array, vector);

//Call idealRatioArray function

idealArray(matrix);

//Testing values

cout << array[1] << endl;
cout << array[2] << endl;
cout << array[3] << endl;

cout << vector[1] << endl;
cout << vector[2] << endl;
cout << vector[3] << endl;

cout << matrix[1] << endl;
cout << matrix[2] << endl;
cout << matrix[3] << endl;

//Call outputTable function

outputTable();


return 0;

}

void birthdayFrequency(int n, Birthday number)


{

int iteration = 0;
int value;
int boundary = 364;

//For loop for n iterations

for ( int k =0 ; k < n ; k++)
    {
Counter boolean = {0};
iteration = 0;
//Randomly mark birthdays until there's a duplicate using a for loop
    for ( int i = 0; i < 366; i ++)
        {
            value = randomInt(boundary);
            if (boolean[value] == 1)
                break;
            else
                boolean[value] = 1;

            number[iteration]++;       //Increment the frequency array for every non-match
            iteration++;

        }
    }
}

void ratioArray(int n, Birthday number, Ratio vectors)

{
double ratio;

//For loop for the number of runs, storing the value of ratios to the total number of runs.

for ( int i = 0 ; i < 364 ; i++)
    {
    ratio = (double)number[i] / n;
    vectors[i] = ratio;
    }

}
void idealArray(Ratio number)


{

number[0]= 1.0;
number[1] = 1.0;

//Create an ideal, theoretical probability array

for ( int n = 2; n < 364; n++)
    {
     number[n] = (number[n - 1]*(1- (n-1)/365));
    }
}

void outputTable()

{


//Not completed yet.


}

int randomInt(int bound)

{
return static_cast<int>( rand() / (RAND_MAX + 1.0) * bound );
}

在此代码中:

for ( int n = 2; n < 364; n++)
{
    number[n] = (number[n - 1]*(1- (n-1)/365));
}

n 是一个整数,因此 (1-(n-1)/365)) 将计算为一个整数值,因为表达式中的所有值都是整数。此外,一个整数乘以一个整数将产生一个整数。由于您开始将 number[1] 设置为 1.0,并且每个元素都是通过将前一个元素乘以一个整数来计算的,因此所有后续值都将是整数(尽管存储为双精度)。

更改您的代码以使用双精度数进行计算:

for ( int n = 2; n < 364; n++)
{
    number[n] = (number[n - 1]*(1.0-((double)n-1.0)/365.0));
}