无法修复蒙特卡洛程序中的错误

Can't fix errors in monte carlo program

本文关键字:错误 程序 蒙特卡洛      更新时间:2023-10-16

我正在尝试编写一个程序,模拟在标准曲线上投掷飞镖。每当我接近调试整个东西时,就会弹出其他东西。到目前为止,我收到了很多错误,比如:

错误:变量未在此范围中声明

还有一个错误,我不知道如何修复,它与C++比较指针和整数有关

我是C++的新手,所以任何指针都将不胜感激。

这是我目前为止得到的。

注意:错误出现在第67、70、72和75行。

#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
double seed(int darts, int x);
int main ()
{
    int darts, x_max;
    double area;
    char again = 'y';
    char giveDarts;
    while (again == 'y' || again == 'Y');
        cout << "Run program (y/n)?";
        cin >> giveDarts;
        switch (giveDarts) {
            case 'y':
            case 'Y':
                cout << "Enter the ammount of darts to be thrown: "; //since we are simulating DARTS I will use the varible darts instead of "NumberOfSamples"
                cin >> darts;
                srand(darts);
            default:
                break;
        }
    cout << "Enter maximum value of x: ";
    cin >> x_max;
    while (x_max < 0);
        cout << "Please enter a positive value of x: ";
        cin >> x_max;
        cout << endl;
    srand(time(NULL));
    area = seed(darts, x_max);
    cout << "Estimate of area under curve is: " << area << endl;
    cout << "Go again? ";
    cin >> again;
    return 0;
}
double seed(int darts, int x_max)
{
    int i, num_darts=0; //num_darts instead of SamplesInsideArea.
    double area;
    for(i=1; i<=darts; i++) // for loop
    {    
        double x, y; 
        double pi = 3.14;
        double n (double t);
        return 1/sqrt(2*pi)*exp(-pow(t,2)/2); //error:'t' was not declared in this scope
              x = rand() / static_cast<double>(RAND_MAX);
              y = rand() / static_cast<double>(RAND_MAX);
        n(0) = (x*x_max + y*y_max); //error: y_max was not declared in this scope
        if(num_darts <= n) //error: ISO C++ forbids comparison between pointer and integer
            num_darts++;
            area*n(0)+0.5 = static_cast<double>(num_darts)/darts; //error: invalid Ivalue in assignment.
     }
     return area;
 }
  1. 此行:

    double n (double t);
    

    正在对采用一个参数CCD_ 2的函数CCD_。这导致了两个错误:

    • error: 't' was not declared in this scope(因为函数原型不声明变量(
    • error: ISO C++ forbids comparison between pointer and integer(因为n是指向函数的指针(

    你是说这是一个函数原型吗?如果不是,你是什么意思?

  2. 错误error: y_max was not declared in this scope是直接的。y_max没有在任何地方声明。

  3. 此行:

    area*n(0)+0.5 = static_cast<double>(num_darts)/darts; //error: invalid Ivalue in assignment.
    

    错误error: invalid Ivalue in assignment是因为无法为表达式赋值。你打算在这里做什么?


此外,还有一些其他问题:

  1. 此行:

    while (again == 'y' || again == 'Y');
    

    将导致您的程序进入无限循环,因为您在它之前设置了again = 'y',并且分号告诉编译器这一点:

    while (again == 'y' || again == 'Y')
    {
        // do nothing
    }
    

    要解决此问题,请删除分号,并在while循环中需要包含的代码周围加大括号。稍后也存在相同的问题(n0(。

  2. 还有人指出了这一点:

    return 1/sqrt(2*pi)*exp(-pow(t,2)/2);
    

    这发生在函数的中间。这将导致该函数立即结束并返回计算值。这是你想要的吗?这一行之后的代码将永远不会运行。


更多问题:

  1. 此代码不处理N/N情况。当您键入"n"时,程序不会停止,并且可能会崩溃。

    switch (giveDarts) {
    case 'y':
    case 'Y':
        cout << "Enter the ammount of darts to be thrown: "; //since we are simulating DARTS I will use the varible darts instead of "NumberOfSamples"
        cin >> darts;
        srand(darts);
    default:
        break;
    }
    cout << "Enter maximum value of x: ";
    
  2. 使用大括号来控制循环,而不是空白。取而代之的是:

    while (x_max < 0);
    cout << "Please enter a positive value of x: ";
    cin >> x_max;
    cout << endl;
    

    你想要这个:

    while (x_max < 0)
    {
        cout << "Please enter a positive value of x: ";
        cin >> x_max;
        cout << endl;
    }
    
  3. 此行:

    area*n(0)+0.5 = static_cast<double>(num_darts)/darts;
    

    如果您试图设置area,则需要采用以下形式:

    area = static_cast<double>(num_darts)/darts; // n(0)+0.5 goes where??
    

当您第一次学习编程C++时,我建议您在全局级别声明和定义所有函数。这意味着像double n (double t);这样的行永远不应该出现在任何大括号内。因此,要解决代码的部分问题,请移动以下两行代码:

double n (double t);
return 1/sqrt(2*pi)*exp(-pow(t,2)/2);

seed()函数之外,并进行一些小的修改,使其看起来像这样:

double n (double t) {
    return 1/sqrt(2*pi)*exp(-pow(t,2)/2)
}

这应该会帮助你朝着正确的方向前进。(只需确保pi被声明为全局常量即可。(