Question

1. Consider a dartboard consisting of a circle of radius r inscribed in a square of side length 2r. Assume that there is some(c) On Canvas, we have provided a C++ program called approx_pi.cpp that computes the value of a using a random number generat

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//The solution of the above problem is attached below:

//CPP CODE-

#include <iostream>

#include <cmath>

#include <cstdlib>

#include <ctime>

#include <iomanip>

#include <string>

using namespace std;

int main()

{

const double PI = 3.141592653589793;

static double x_cor = 0;

static double y_cor = 0;

static double x_sum = 0;

static double y_sum = 0;

double x_y_total = 0;

long int num_darts = 0;

long int num_hits = 0;

long int num_misses = 0;

long double pi_approx = 0;

srand(time(0));

while ( num_darts != 0 ) // Start of while loop

{

cout << " How many times do you want to throw the darts: ";

cin >> num_darts;

cout << endl;

if ( num_darts != 0 )

{

for ( int i = 0; i < num_darts; i++) // Start of for loopa inside the while loop

{

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

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

x_sum = (x_cor * x_cor);

y_sum = (y_cor * y_cor);

x_y_total = x_sum + y_sum;

if (x_y_total < 1)

{

num_hits++;

}

else

{

num_misses++;

}

}

} // end of if loop

pi_approx = (num_hits / num_darts);

} // end of while loop

cout << " The value is " << pi_approx << endl;

return 0;

}

Add a comment
Know the answer?
Add Answer to:
1. Consider a dartboard consisting of a circle of radius r inscribed in a square of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • CS0007 Good day, I am in an entry-level Java class and need assistance completing an assignment....

    CS0007 Good day, I am in an entry-level Java class and need assistance completing an assignment. Below is the assignment criteria and the source code from the previous assignment to build from.  Any help would be appreciated, thank you in advance. Source Code from the previous assignment to build from shown below: Here we go! Let's start with me giving you some startup code: import java.io.*; import java.util.*; public class Project1 { // Random number generator. We will be using this...

  • 1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program!...

    1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program! 3     public static void main(String[] args) { 4         System.out.println("Hello, World!"); 5     } 6 } What is on line 1? a. a variable declaration b. a statement c. a method (subroutine) definition d. a comment e. a class definition 2) Which one of the following does NOT describe an array? a. It can be used in a for-each loop. b. It has a numbered sequence...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT