因此,我只是对编程进行介绍,我不得不说这是我做过的最有意义但又令人沮丧的事情。我正在进行的项目难度越来越大,最近的项目涉及蒙特卡洛方法的使用和大量循环。以下是到目前为止已完成的代码:
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <string>
#include <ctime>
using namespace std;
int main ()
{
srand (time(0));
string operation;
cout << "Using the letters 'o', or 'q', please indicate if you would like to simulate once, or quit the program: " << endl;
cin >> operation;
string reservoir_name; // Creating variables for reservoir
double reservoir_capacity;
double outflow;
double inflow_min;
double inflow_max;
if (operation == "q")
{
cout << "Exiting program." << endl;
system ("pause");
return 0;
}
while (operation == "o") // Choose one or multiple simulations.
{
string reservoir_name; // Creating variables for reservoir function
double reservoir_capacity;
double inflow_min = 0;
double inflow_max = 0;
double inflow_range = inflow_min + inflow_max;
double inflow_difference = inflow_max - inflow_min;
double inflow_threshold = .9 * inflow_range/2; // Math for acceptable flow threshold.
cout << "What is the name of the reservoir?" << endl;
cin.ignore ();
getline (cin,reservoir_name); // Grab whole string for reservoir name.
cout << "What is the capacity of the reservoir in MAF (Millions of Acre Feet)?" << endl;
cin >> reservoir_capacity;
cout << "What is the minimum inflow?" << endl;
cin >> inflow_min;
cout << "What is the maximum inflow?" << endl;
cin >> inflow_max;
cout << "What is the required outflow?" << endl;
cin >> outflow;
inflow_range = inflow_min + inflow_max;
inflow_threshold = .9 * inflow_range/2;
cin.ignore ();
if (outflow > inflow_threshold) // Check for unacceptable outflow levels.
{
cout << "Warning! The outflow is over 90% of the average inflow. Simulation aborted. Returning to main menu." << endl;
}
else
{
const int number_simulations = 10;
double fill_level = 0;
int years = 1;
cout << "Running simulation." << endl;
for (int i = 1; i < number_simulations; i++) // Each year
{
for (years; fill_level < reservoir_capacity; years++ )
{
double r = rand() * 1.0 / RAND_MAX;
double x = inflow_min + inflow_range * r;// SHOULD be between minimum inflow and maximum inflow.
if (fill_level < 0)
{
fill_level = 0;
}
} // Simulate the change of water level.
cout << years << endl;
}
}
cout << "What would you like to do now?" << endl; // Saving for later. The menu re-prompt message and code.
cout << "Using the letters 'o', or 'q', please indicate if you would like to simulate once, or quit the program: " << endl;
cin >> operation;
}
system ("pause");
return 0;
}
因此,我想我的主要问题是我遇到了有关在“运行模拟”下设置for循环的问题,在这里我需要设置第一个for循环以运行内部for循环10次,并进行10次迭代内部for循环的结果,其中包含来自随机值查询的可接受结果范围内的随机数。有人告诉我,我的想法是使用蒙特卡洛方法,即
double r = rand() * 1.0 / RAND_MAX;
double x = inflow_min + inflow_range * r;// SHOULD be between minimum inflow and maximum inflow.
因此程序将为流入量创建一个随机值。这个想法是内部的for循环将继续运行,直到从0开始的容器的fill_level达到容器容量为止。父级for循环的fill_level模拟for循环将模拟多少年的过程(内部for循环的每次迭代代表一年)。
当我尝试运行该程序时(如您在此处看到的那样),它将一直进行到“正在运行的仿真”为止,然后将不再进行任何操作。比我自己更有经验的人能理解我的意思并知道发生了什么吗?
最佳答案
for (years; fill_level < reservoir_capacity; years++ )
{
double r = rand() * 1.0 / RAND_MAX;
double x = inflow_min + inflow_range * r;// SHOULD be between minimum inflow and maximum inflow.
if (fill_level < 0)
{
fill_level = 0;
}
} // Simulate the change of water level.
您永远不会在此循环中增加
fill_level
。这是一个无限循环。