[英]C++ 2D array and boost random number generator
我在我的程序中放入了兩個循環 - 一個用一個值 N0 填充二維數組,下一個循環生成隨機數。 當我有數組循環時,我的程序不起作用。 我得到“未處理的異常...(參數:0x00000003)”。 但是沒有第一個循環它可以正常工作。 感謝幫助。
#include <iostream>
#include <vector>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int_distribution.hpp>
using namespace std;
const double czas = 1E9;
int main()
{
//Declaration of variables
const int k = 20;
const int L = 30;
double N0 = 7.9E9;
int t,i,j, WalkerAmount;
double excitation, ExcitationAmount;
double slab[30][600];
//Random number generator
boost::random::mt19937 gen;
boost::random::uniform_int_distribution<> numberGenerator(1, 4);
//Filling slab with excitation
for (int i = 0; i <= L; i++)
{
for (int j = 0; j <= k*L; j++) { slab[i][j] = N0; }
}
//Time loop
for (t = 0; t < czas; t++) {
WalkerAmount = 0;
ExcitationAmount = 0;
for (int i = 0; i <= L; i++)
{
for (int j = 0; j <= k*L; j++)
{
int r = numberGenerator(gen);
cout << r << endl;
}
}
}
system("pause");
return 0;
}
C++ 中的數組索引從0
到n-1
,其中n
是數組的容量。 然后,代碼后面的代碼是錯誤的。
int main()
{
//Declaration of variables
const int k = 20;
const int L = 30;
double N0 = 7.9E9;
double slab[30][600];
// [...]
for (int i = 0; i <= L; i++)
{
for (int j = 0; j <= k*L; j++) { slab[i][j] = N0; }
}
}
當你初始化你的數組時,你總是走得太遠。 當您考慮i == L
和j == k*L
您會到達內存中超出數組的區域。
您要執行的循環是
for (int i = 0; i < L; i++)
for (int j = 0; j < k*L; j++)
// Initialize
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.