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

Hi. I need to make a program that asks the user to numerically approximate the i

ID: 3539727 • Letter: H

Question

Hi. I need to make a program that asks the user to numerically approximate the integral of one of three functions, specify a range of integration, the number of iterations for the approximation, and then approximate the integral using random numbers.

I can always get the right answer in the first trail. But when I run the second trial, the answer will become double. For example, the first time to integrate |sin(x^2)| I got 0.55. Then I run it the second time with same bounds and iterations, the result became 1.1. Here is my code. Could anyone help me to figure out what went wrong? Thank you so much!



#include <iostream>

#include <string>

#include <cstdlib>

#include <cmath>

#include <ctime>

using namespace std;


int main(){

srand((unsigned int)time(0));

cout<<"Welcome! ";

int choice=0, iters=0,hits=0;

double lower_bound=0, upper_bound=0;

string;

string two= "e^(-x^2)";

string three= "|arctan(x)|";

double u=0,x=0,y=0,area=0;

string input;

const double PI_OVER_TWO=1.571;

const double>

do{

do{

cout<<"Please select a function from the option below. ";//This statement prints out the text.

cout<<" ";//It prints a empty line.

cout<<"1. |sin(x^2)| ";

cout<<"2. e^(-x^2) ";

cout<<"3. |arctan(x)| ";

cout<<" ";

cout<<"Integrate function number: ";

cin>>choice;

cout<<" ";


  

if(choice==1){

cout<<"Lower bound: ";

cin>>lower_bound;

cout<<"Upper bound: ";

cin>>upper_bound;

cout<<"Number of iterations: ";

cin>>iters;

for(int i=0; i<=iters; ++i){

u = static_cast<double>(rand()) / RAND_MAX;

x = u * ( upper_bound - lower_bound ) + lower_bound;


u = static_cast<double>(rand()) / RAND_MAX;

y = u * ONE;

if(y<=fabs(sin(x*x)))

++hits;

}

area = ( upper_bound - lower_bound ) * ( ONE * hits ) / iters;

cout<<"The estimate for the area of "<<one<<" from "<<lower_bound<<" to "<<upper_bound <<" is "<<area<<endl;

}

else if(choice==2){

cout<<"Lower bound: ";

cin>>lower_bound;

cout<<"Upper bound: ";

cin>>upper_bound;

cout<<"Number of iterations: ";

cin>>iters;

for(int i=0; i<=iters; ++i){

u = static_cast<double>(rand()) / RAND_MAX;

x = u * ( upper_bound - lower_bound ) + lower_bound;


u = static_cast<double>(rand()) / RAND_MAX;

y = u * ONE;

if(y<=exp(-x*x))

++hits;

}

area = ( upper_bound - lower_bound ) * ( ONE * hits ) / iters;

cout<<"The estimate for the area of "<<two<<" from "<<lower_bound<<" to "<<upper_bound <<" is "<<area<<endl;

}


else if(choice==3){

cout<<"Lower bound: ";

cin>>lower_bound;

cout<<"Upper bound: ";

cin>>upper_bound;

cout<<"Number of iterations: ";

cin>>iters;

for(int i=0; i<=iters; ++i){

u = static_cast<double>(rand()) / RAND_MAX;

x = u * ( upper_bound - lower_bound) + lower_bound;


u = static_cast<double>(rand()) / RAND_MAX;

y = u * PI_OVER_TWO;

if(y<=fabs(atan(x)))

++hits;

}

area = PI_OVER_TWO * ( upper_bound - lower_bound ) * ( ONE * hits) / iters;

cout<<"The estimate for the area of "<<three<<" from "<<lower_bound<<" to "<<upper_bound <<" is "<<area<<endl;

}

else{

cout<<"This is not a valid selection. ";

cout<<" ";

}

}while(choice!=1 && choice!=2 && choice!=3);

  

cout<<" ";

cout<<"Would you like to integrate again? Enter 'yes' or 'no': ";

cin>>input;//It allows to enter yes or no.

cout<<" ";

}while(input=="yes");



system ("PAUSE");

return 0;

Explanation / Answer

I would like to give a try by adding this line

hits=0;

after cout<<"Please select a function from the option below. ";//

This would re initialize hits to ZERO every time .

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