Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The original question: Consider a rectangle that has base [a,b] and height m, wh

ID: 3614416 • Letter: T

Question

The original question:
Consider a rectangle that has base [a,b] and height m, wherem>=f(x) for all x in [a,b].

Imagine throwing q darts at rectangle ABCD and counting the totalnumber p that hit the region uder the curve. For a large number ofthrows we would expect:
(p/q)~= (area under curve/area of rectangle ABCD)

Write a function called Monte() to calculate areas using this MonteCarlo method. Complete the following steps:
a. pass the function the total number of darts to be thrown(q)/

b. For each dart thrown generate two random numbers, X from [a,b]and Y from [0,m], and consider the point (X,Y) as being where thedart hits.

c. Determin if the point lies under the curve or not. If it doesincrement p.

d. After all the darts have been thrown, compute the area using theequation above.

Now write a main() driver function that uses Monte() to calculatethe integral of f(x) =2x2 +3X -2 from x=2.0 tox=8.0 Hint: you can evaluate the value of f(x) usingstatements within Monet() or by writing a fuction to calculate andreturn the vlaue of a polynomial at x and calling that functionfrom Monte().

Have the main () function read the number of darts to be thrown,call the function and write the area value to the terminal.

My question
:
I get the following error that I don't know how to fix:
lab2d.cpp: In function ‘double monte(int)’:
lab2d.cpp:47: error: invalid operands of types ‘double’and ‘double’ to binary ‘operator%’

Also I'm not 100% sure my code will find the area under to curve soI'd appreciate it if you'd look it over and/or run it.

PLEASE highlight all changes so I can see where I went wrong.

My Code:
//Random numbers and the Monte Carlo method

#include <iostream>
using namespace std;
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <ctime>

//global declarations
const int a=2, b=8, m=150;

//function declarations / prototype
double monte(int);
double function(double);

int main()
{
//variable declarations
int q; //q is the number of darts to be thrown
double area;

//input
cout << "Welcome to the Monte Carlo Method program." <<endl;
cout << "Input number of darts to be thrown";
cin >> q;

//call
srand(time(NULL));
area= monte(q);

//output
cout << "For "<< q <<" darts thrown the area isequal to: " << area << endl;

return 0;
}

//functions
double monte(int q)
{
int i, p=0; //p is the number of darts that hit under the curve
double x, y, fx;

for (i=1; i<=q; i++)
{
x= static_cast <double> (a + rand()%(b-a));
y= static_cast <double> ( rand() )%(m - function(x));
    if (y <= fx)
    p++;
}

return ( (x*y)*(p/q) );
}

double function(double x)
{
return ( (2.0*(x*x)) + (3.0*x) - 2.0 );
}

Explanation / Answer

//Random numbers and theMonte Carlo method

#include<iostream>
using namespace std;
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <ctime>

//global declarations
const int a=2, b=8, m=150;

//function declarations /prototype
double monte(int);
double function(double);

int main()
{
//variable declarations
int q; //q is the number of darts to be thrown
double area;

//input
cout << "Welcome to the Monte Carlo Method program." <<endl;
cout << "Input number of darts to be thrown";
cin >> q;

//call
srand(time(NULL));
area= monte(q);

//output
cout << "For "<< q <<" darts thrown the area isequal to: " << area << endl;
system("pause");
return 0;
}

//functions
double monte(int q)
{
int i, p=0; //p is the number of darts that hit under the curve
double x, y, fx;

for (i=1; i<=q;i++)
{
x= static_cast <double> (a + rand()%(b-a));
y= rand()%static_cast <int> (m -function(x));         //all % operands must be integer
    if (y <= fx)
    p++;
}

return ( (x*y)*(p/q));
}

double function(doublex)
{
return ( (2.0*(x*x)) + (3.0*x) - 2.0 );
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote