Я пытаюсь написать программу, которая имитирует дротики, бросаемые на стандартную кривую. Всякий раз, когда я приближаюсь к отладке всего, что-то появляется. До сих пор я получаю много ошибок, как:Невозможно исправить ошибки в программе monte carlo
Error: variable is not declared in this scope
Также есть ошибка, я понятия не имею, как исправить, которая имеет отношение к 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;
}
Кроме того, мой инструктор попросил настроить максимальное значение x и y на x_max и y_max, но я не знаю, что делать с y_max. –
Не могли бы вы пояснить свой исходный код строками, которые дают ошибки? Это избавляет нас от необходимости подсчитывать их или вставлять их в другой редактор. – MatthewD
Вы, кажется, возвращаетесь из петли. Вы уверены, что хотите этого? – emsr