This week's book giveaway is in the Mac OS forum. We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line! See this thread for details.

In this program, I am calculating the value of pi by simulating throwing darts at a dartboard. I have correctly coded the formula that calculates pi, however, as seen in the code, the # of trials is based on user input. If the user enters 10, I need to calculate pi 10 times. However, I am not sure how I am going to calculate pi ten times. Can somebody help with this? Thanks

You just need a big old "for" loop enclosing the existing code; it would start right after the declaration of "numberOfTrials" and continue all the way to the end of the method.

Thanks for the tip. However, I have a couple of for loops in the code now, and everything is fine except the fact that there is still only one value for pi. I thought the following code would work, however, apparently something is still not right.

Ernest Friedman-Hill
author and iconoclast
Marshal

Well, there are a couple of things that need polishing. For example, make sure that all the numbers that need to start from a known value at the beginning of a trial, do so. The variables numberOfX and numberOfY should start at zero for each time you calculate pi, yes? So they should be initialized inside the top of that for loop, not before the loop (as an aside, you really only need one of those variables, right?)

Another issue is with the array piValues. Since you're creating it each time you calculate pi, it can never hold more than one value, so the expression "piValues[trialNumbers]" is going to give you ArrayIndexOutOfBoundsExceptions. I woud just print the simple variable "pi" and do away with the piValues array altogether.

David Barry
Ranch Hand

Joined: Jan 13, 2009
Posts: 83

posted

0

Ok. Thanks for the tip. I definitely see where you are going with this (and how I should be going). I see some improvement in the output, however, based on the following code, I get this outputt:
How many times should the darts be thrown in a trial?
123456

Enter the number of trials.
10

Trial 0: pi = 3.140747
Trial 1: pi = 6.280262
Trial 2: pi = 9.422013
Trial 3: pi = 12.563828
Trial 4: pi = 15.706875
Trial 5: pi = 18.848075
Trial 6: pi = 21.988757
Trial 7: pi = 25.134461
Trial 8: pi = 28.282400
Trial 9: pi = 31.424184

This shows some improvement, however, this is definately NOT what I want, and I really don't see what is going on here.

David Barry
Ranch Hand

Joined: Jan 13, 2009
Posts: 83

posted

0

Do you see what I'm saying concerning this problem? I don't understand why this is happening

you probably need to reset your 'numberOfHits' each time through the loop.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

David Barry
Ranch Hand

Joined: Jan 13, 2009
Posts: 83

posted

0

Ok, I now have all of the correct values for pi. However, I want to calculate the average of all the pi values and print it below the pi values. However, I get this output: How many times should the darts be thrown in a trial?
123456

Enter the number of trials.
10

Trial 0: pi = 3.140260
Estimate of pi = 0.31402604976671855
Trial 1: pi = 3.137474
Estimate of pi = 0.3137474079834111
Trial 2: pi = 3.135303
Estimate of pi = 0.3135303265940902
Trial 3: pi = 3.134169
Estimate of pi = 0.3134169258683256
Trial 4: pi = 3.139483
Estimate of pi = 0.31394828926905133
Trial 5: pi = 3.143339
Estimate of pi = 0.31433385173665107
Trial 6: pi = 3.136632
Estimate of pi = 0.31366316744427164
Trial 7: pi = 3.150564
Estimate of pi = 0.3150563763608087
Trial 8: pi = 3.140131
Estimate of pi = 0.31401308968377395
Trial 9: pi = 3.140909
Estimate of pi = 0.3140908501814411

David Barry
Ranch Hand

Joined: Jan 13, 2009
Posts: 83

posted

0

In other words, I need to get the sum of all of the values of pi and divide them by the number of trials. However, as seen from the output I posted above, the code is only generating the previous value of pi for the average.

you have answered your own question. you need to get the sum of all the values of pi, and divide that by the number of trials.

Each time through your loop, you calculate pi for that trial then immediately try and calculate the average:this doesn't make any sense. You can't calculate the average until you have all 10 values.

you need to keep a running total of the values of pi, then when you're done with all your trials, divide that total by the number of trials.

alternately, you could keep a running total inside the loop, and divide that by the trial you're on (not the total number) to see your accuracy improve on each iteration.

David Barry
Ranch Hand

Joined: Jan 13, 2009
Posts: 83

posted

0

Thanks, Fred. That's just what I was thinking: keeping a running total. However, I am sort of at a lost as to how to do that. Do I need an array? I don't think I can do: pi = 4 * (numberOfHit / drops);
pi++;

I am just trying to figure out what is the right way to keep a running total so I can divide that by the number of trials (which is variable: numberOfTrials).

An array is used when you need to keep many individual values. for example. you could have an array of hiTemps that keeps all the hi temperatures for 30 days. you can access them all individually by using the array index.

You don't need that (although you could write your program to work with one). All you need is a total - so your last suggestion is correct. Note that you will need to declare it outside of the trials loop, similar to where you declared your pi variable.