Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Part 3 : Determine primality Take a look at primes. h - you will be implementing the isPrime function. You can assume that the parameter

Part 3: Determine primality
Take a look at primes. h- you will be implementing the isPrime function. You can assume that the parameter x is always 2.
Now, implement the function in primes.cpp. Your book has some pseudocode in Section 9.4 that you may find helpful.
Once you're finished (or you want to check your work), I've implemented several. tests in testPrimes.cpp. Compile with make testPrimes and run with ./testPrimes. These are the same tests that you will be graded against.
Part 4: Calcuate pi by throwing darts
Look at pi.h. You will be using the Point class (that l've already implemented for you) to implement the approximatePi function at the bottom.
You are going to randomly throw numDarts onto a square with an inscribed circle centered at (0,0) :
Several of your darts will land inside the circle, but some will miss Surprisingly, this game tells us something about !
The darts approximate area, and the ratio of the area of the circle to the area of the square is 4. So, if you divide the number of darts that landed inside the circle by the total number of darts Area of square: 22=4 thrown, you get an approximation of 4! All that's left is to turn that into an approximation of .
We're going to cheat and work only in the first quadrant (where the x and y coordinates are always nonnegative), but the ratio remains the same.
The Point class represents the coordinates where the dart landed. Do you see what method you might use to determine if the dart landed inside the circle?
Implement the approximatePi function in pi.cpp. You can then run the tests in testPi.cpp using make testPi to compile and ./testPi to run.
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microsoft SQL Server 2012 Unleashed

Authors: Ray Rankins, Paul Bertucci

1st Edition

0133408507, 9780133408508

More Books

Students also viewed these Databases questions