This is the archived website of SI 486H from the Spring 2016 semester. Feel free to browse around; you may also find more recent offerings at my teaching page.

Problem 12

Mystery program

Due: January 19
Points: 3-4

Here is a pseudocode-level description of a randomized function. This function takes as input an integer \(n\), which can be any number greater than or equal to 1, and returns a floating-point value.

  1. Choose two numbers, \(x\) and \(y\), each being randomly-chosen floating point numbers between -1.0 and 1.0.
  2. Compute \(d = \sqrt{x^2 + y^2}\).
  3. Repeat steps 1 and 2 \(n\) times, and count the number of times that \(d \le 1\). Call this number \(c\). (Note \(c\) will be an integer between 0 and \(n\).)
  4. Return the value \(4c/n\).

Implement this program in the programming language of your choice, and run it with \(n=10\), \(n=100\), \(n=1000\), \(n=10000\), \(n=100000\), and \(n=1000000\). Turn in a printout of your code as well as the results of these runs, printed in a nice table.

Then answer the question: What does this program do?

For 1 bonus point, explain why it works.