无法修复蒙特卡洛程序中的错误
Can't fix errors in monte carlo program
我正在尝试编写一个程序,模拟在标准曲线上投掷飞镖。每当我接近调试整个东西时,就会弹出其他东西。到目前为止,我收到了很多错误,比如:
错误:变量未在此范围中声明
还有一个错误,我不知道如何修复,它与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;
}
-
此行:
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
是指向函数的指针(
你是说这是一个函数原型吗?如果不是,你是什么意思?
-
错误
error: y_max was not declared in this scope
是直接的。y_max
没有在任何地方声明。 -
此行:
area*n(0)+0.5 = static_cast<double>(num_darts)/darts; //error: invalid Ivalue in assignment.
错误
error: invalid Ivalue in assignment
是因为无法为表达式赋值。你打算在这里做什么?
此外,还有一些其他问题:
-
此行:
while (again == 'y' || again == 'Y');
将导致您的程序进入无限循环,因为您在它之前设置了
again = 'y'
,并且分号告诉编译器这一点:while (again == 'y' || again == 'Y') { // do nothing }
要解决此问题,请删除分号,并在while循环中需要包含的代码周围加大括号。稍后也存在相同的问题(
n
0(。 -
还有人指出了这一点:
return 1/sqrt(2*pi)*exp(-pow(t,2)/2);
这发生在函数的中间。这将导致该函数立即结束并返回计算值。这是你想要的吗?这一行之后的代码将永远不会运行。
更多问题:
-
此代码不处理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: ";
-
使用大括号来控制循环,而不是空白。取而代之的是:
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; }
-
此行:
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
被声明为全局常量即可。(
- 程序崩溃并显示"std::out_of_range"错误
- xmake总是报告:错误:无法获取cxx的程序,为什么
- VSCode-有一个红色下划线,但程序构建和运行正确,并且出现配音错误
- 分段错误当我试图运行程序时出错
- 显示错误输出的简单数组排序程序
- 示例外壳应用程序显示的 V8 "segmentation fault (core dumped)"错误
- C++应用程序 MySQL odbc 数据库连接错误:在引发"otl_tmpl_exception<>"实例后终止调用
- 为什么程序在 c++ 中迭代 emtpy 向量时会抛出运行时错误
- 在气泡排序程序中未声明错误功能
- 我在c ++矩阵计算器程序中找不到错误
- 叮当格式化程序多行格式配置错误
- 全局向量导致 C++ 程序结束时出现段错误
- 运行时间错误:程序跳过提示,以获取第二名和第三个名称
- Glibc 2.7 配置错误程序太旧
- 运行时错误:程序已停止响应
- OpenCV for Android:在 PATH 中找不到错误程序"sh"
- C++中的 scanf 错误 - 程序没有响应
- c++语法错误:程序以非零状态退出
- 运行时错误C++程序:检测到glibc,munmap_chunk(),指针无效
- libxml2错误程序c++