Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Monte Carlo Assignment: Once again, we are going to use the Monte Carlo Meth

ID: 662533 • Letter: C

Question

C++ Monte Carlo Assignment:

Once again, we are going to use the Monte Carlo Method, this time to determine the different

frequencies of landing on each spot of a board in a single circumnavigation of the game board.

The Board

You should now be learning to make your programs as general as possible. For this assignment, you

have to be able to make your calculations work for any board size and configuration: all you know

about it is that all the "cells" (the squares on which a player can land) are evenly distributed around

the edge of an n?sided polygon, k cells per side. For our purposes, we shall number the cells 1

through (n * k), with cell (n * k) also being the original starting position.

The Simulation

A player rolls a pair of 6?sided dice, and moves that many cells from the start spot; this process is

repeated as many times as required until the roll places the "piece" on or past the starting space.

Since we are modeling only a single player, there are no turns: each simulation is of a single player

going around the entire board once. The starting space is also the last possible spot a player can

land in a single rotation, we record landing on this space but then end the simulation (no more rolls and

we do not record anything if we land beyond this last space [beyond n*k]).

Your Task

You will model the board as a vector of size (n * k). Note that although you will be working with

indices 0 through (n * k ? 1), you will report the cells as numbers 1 through (n * k), as this is how a

player would think about the board.

As your player traverses the board, you will record each cell he/she lands on. You will perform

multiple simulations of a single board rotation, accumulating in each vector cell the number of times

the player lands on the corresponding board space.

You will implement several functions for this program. As you have learned, you must understand

how to test each function with a "test harness". We do not submit the harnesses to R'Sub, R'Sub

already has several to test with.

Functions

We list two functions that are required, we provide an idea for third optional function and you can

implement other additional functions that you find useful. You should write function comments for

each of the functions you define, containing an @brief, @param and @return similar to those we

have previously provided for you. R'Sub will test each function individually.

The first function will simulate the dice roll. Once again we want the function to be as general as

possible, so we will parameterize both the number of dice and the number of sides on a dice.

The second function returns the cell with most landings within a closed interval of board spots.

(The closed interval [5,10] means any of the values 5, 6, 7, 8, 9, or 10.)

rollNDice

? simulates rolling N dice, each of which has the specified number of sides

? parameters: two integers by value:

? first integer: the number of dice to roll, by value

? second integer ? the number of sides on the dice, by value

? return type: integer, the summation of all N dice rolls

mostLandings

? returns the cell on the board with the most "landings", within an inclusive interval

? parameters: a vector<int>; two integers

? first parameter: the game board vector, by const reference

? second parameter: the start of the interval to be tested, by value

? third parameter: the end of the interval to be tested, by value

? return type: integer, the index of the first cell in the interval with the most landings

Optionally, you may want (in addition to other functions you think of):

printBoard

? procedure, prints out game board, showing number of landings on each cell

? parameters: a vector<int>

? return type: void

Random Seed Requirement

We want you to submit to R'Sub with srand(time(0)); as the seed. However, to reproduce the

results displayed in the examples you you have to seed rand with 333: srand(333);

A note to those not working within C9. The random numbers generator on different systems

(Windows, Linux, OS X) may produce different results even when using the specified seed value.

? board ? the game board vector, by const reference

When debugging your program using srand(333), the first 24 rolls of a die on c9 (linux) are:

544334523652415533644342

Input Requirements

? Prompt the user for the board configuration: number of sides, number of cells per side

? Prompt the user for the number of simulations to run.

Output Requirements

Use the mostLandings function to help you find and report the cell that has the most landings on

each side of the board. As shown in the examples, each side of the board is reported. You will

need to write a loop to print out this information and the loop should use simple mathematics to

calculate the beginning and ending spots based on the side number.

Explicit Example

For our explicit example we will utilize the board game Monopoly. The board for Monopoly has 4

sides of 10 spaces, so our vector needs 40 cells. The start space in Monopoly is "Go", but "Go" will be

the last spot represented by the last cell in the vector, as the first board space is the spot

immediately after "Go".

Note that we are not incorporating the

Explanation / Answer

#include <vector>
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int rollNDice(int num, int sides){
   int sum = 0;
   for(int i = 0; i < num; ++i){
       sum += rand() % sides + 1;
   }
   return sum;
}

int mostLandings(const vector<int> &board, int s, int e){
   int ind = s;
   for(int i = s; i <= e; ++i){
       if(board[i] > board[ind]){
           ind = i;
       }
   }
   return ind;
}

void print(const vector<int> &board){
   for(int i = 0; i < board.size(); ++i){
       cout << "Number of landings on" << (i + 1) << " is " << board[i] << endl;
   }
}

int main(){
   srand(time(NULL));
   int sides, spots;
   cout << "How many sides of the board are there? ";
   cin >> sides;
   cout << "How many spots are on each side? ";
   cin >> spots;
   vector<int> v(sides * spots);
   for(int i = 0; i < v.size(); ++i){
       v[i] = 0;
   }
   long s;
   cout << "How many simulations? ";
   cin >> s;
   int spot = 0;
   for(int i = 0; i < s; ++i){
       spot = 0;
       while(true){
           spot += rollNDice(2, 6);
           if(spot >= (sides * spots)){
               break;
           }
           else{
               v[spot]++;
           }
       }
   }
   for(int i = 0; i < sides; ++i){
       int ind = mostLandings(v, i * spots, ((i * spots) + spots - 1));
       cout << "On side " << (i + 1) << ", spot " << (ind + 1) << " has the most landings: " << v[ind] << endl;
   }
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote