2

だから私はプログラミングを始めたばかりで、これは私がこれまでにやった中で最もやりがいがありながらイライラすることだと言わざるを得ません. 私は難易度の高いプロジェクトに取り組んでいます。最近のプロジェクトでは、モンテカルロ法と多くのループが使用されています。これまでに完成したコードは次のとおりです。

#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 回実行する必要があります。ランダム値のクエリからの許容可能な結果の範囲の乱数を考え出す内部 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 が、reservoir_capacity に達するまで実行し続けるという考え方です。何年 (1 年を表す内部 for ループの各反復) をシミュレートするプロセスは、fill_level シミュレーション for ループの親 for ループによって 10 回繰り返されます。

ここに表示されているようにプログラムを実行しようとすると、「シミュレーションの実行」までずっと進み、それ以上進みません。私よりも経験豊富な人は、私が言っていることを理解し、何が起こっているのかを知っていますか?

4

1 に答える 1

1
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このループでは決して増加しません。無限ループです。

于 2012-10-05T03:29:39.330 に答える