Other 2018 FR Questions FR other years Be Prepared Home
A-1
Part (a)
  public boolean simulate()
  {
    int pos = 0;

    for (int hops = 0; hops < maxHops; hops++) 1
    {
      pos += hopDistance();
      if (pos >= goalDistance)
        return true;
      else if (pos < 0)
        return false;
    }
    return false;
  }
Notes:
  1. Or, if you prefer,
    for (int hops = 1; hops <= maxHops; hops++)


Part (b)
  public double runSimulations(int num)
  {
    int count = 0; 1
    for (int n = 0; n < num; n++)
      if (simulate())
        count++;
    return (double)count / num;
  }
Notes:
  1. Some people prefer double count = 0; to avoid the cast in the final ratio.


Other 2018 FR Questions | Back to Contents

Copyright © 2018 by Skylight Publishing
support@skylit.com