Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. Introduction This assignment, which introduces the use of loops, solves the following problem: given a starting location in a city, how long does it

1. Introduction

This assignment, which introduces the use of loops, solves the following problem: given a starting location in a city, how long does it take a drunken sailor who randomly chooses his direction at each intersection to reach the citys border? You will read input values to set up the problem parameters, run several trials to determine an average number of steps for the sailor to reach the border, and output the results.

This problem is an example of a random walk, a succession of random steps that can model real world problems like stock price fluctuation or molecules traveling through liquid. The approach is a simple approximation of a Monte Carlo experiment, in which repeated random samples are run to find a numerical result.

The old Program 4 spec from Spring 2018 can be found at this link. Note that your solution will not require you to prompt the user for a seed for the random number generator. Your program should always use seed value 1, as given in the template file below.

The figures document contains detailed test cases on pages 3-4 that show full program runs given two different sets of input.

2. Specification

Problem Description

The city is organized as a set of M x N blocks. The sailors position, which must always be an intersection or a point on the border, can be represented as a pair of coordinates (X, Y), where 0 X M, and 0 Y N. See Figure 1 in the figures document for an example of a 4 x 3 city with the sailor at position (3, 2).

At each step of a given trial, the sailor will randomly choose a direction and walk until he reaches the next intersection. A trial ends when the sailor reaches one of the four city borders--X == 0, Y == 0, X == M, or Y == N. Note that each new trial always uses the same starting point.

Input Specification

Your input must be entered in the order listed below. All inputs are integers. Note that your program should prompt the user to enter each value and check that each input fits within the bounds described below, as well as ensure there are no formatting errors:

M and N, the number of blocks in the X (2 M 10) and Y planes (2 N 10), respectively. This pair of values will be entered on the same line.

A starting position for the sailor, input as a pair of integers (X,Y).

These values should be entered on the same line, separated by a space (e.g., 3 5).

You should NOT format your input as an (X,Y) pair using parentheses and a comma (e.g., (3,5)).

The sailor must always start within the city, so the starting coordinates are subject to the following bounds:

1 X (M-1)

1 Y (N-1)

T, The number of trials to execute (1 T 10).

A sample set of input prompts and valid inputs to those prompts are shown below:

City size in X, Y (# blocks >= 2 and <= 10): 2 2 Starting position (X Y): 1 1 Number of trials: 3 

As noted above, detailed test cases showing appropriate input prompts can be found on pages 3-4 of the figures document.

Output Specification

As noted above, the program should print a prompt for each input--acceptable prompts are shown in the example above. If an input does not fit within the bounds described, or the user enters an improperly formatted value, the program should display an error message and then prompt the user again to enter that value. See the section below for a detailed description of possible errors, and see Section 3 for hints on bounds checking and error messages.

Once the user has successfully input all values, the program executes each trial, starting at the point specified. At each step of a trial, your program generates a random number representing the direction the sailor moves and changes his position accordingly. NOTE: To ensure your output matches the test case output, I've heavily constrained the way you deal with random numbers. Follow the directions in the template file, which are reproduced below:

Only call srand() once, using seed value 1

Modify the statement dir = rand() to ensure the random value is always between 0 and 3

Assume your sailor moves as follows--if you do not follow this specification, your outputs will not match the test cases:

If dir == 0, move west

If dir == 1, move east

If dir == 2, move north

If dir == 3, move south

Your program should print messages at the following points:

At the start of each trial, the program should print a message indicating the start of a new trial and the starting point being used.

Example: Trial #1 Start: 3 1

For each step of each trial, the program should print the direction the sailor moves and his new position.

Example: North: 3 2

At the end of each trial, the program should print the total number of steps taken during that trial.

Example: Trial #1 total steps: 8

Once all trials are complete, the program should calculate the average number of steps taken per trial and output that value.

Example: Average # of steps over 5 trials: 3.2

See the built-in program test cases for more sample program runs, or look at pages 3-4 of the figures document to see two full program runs.

Error Checking

Your program should print a descriptive error message under any of the conditions below. For examples of valid error messages, see the built-in program test cases.

Any of the inputs are incorrectly formatted and therefore cannot be read correctly using scanf()

Your error message should simply read Could not read input in all cases.

Don't forget to clear the line if there is a formatting error, as described in Lecture 11!

The values of M and/or N (the number of X and Y blocks) do not fit in the ranges 2 M 10 and 2 N 10.

Your error message(s) should print # X blocks must be >= 2 and <= 10 or # Y blocks must be >= 2 and <= 10, depending on which input(s) caused the error.

It's possible for both values to be out of bounds, so your program may print two error messages for this pair of inputs.

The values of X and/or Y (the sailor's starting position) do not fit in the ranges 1 X (M-1) and 1 Y (N-1).

Your error message(s) should print Starting X position must satisfy (1 <= X <= M-1) or Starting X position must satisfy (1 <= Y <= N-1), depending on which input(s) caused the error.

Each message should show the actual value of M-1 or N-1 (for example, 1 <= X <= 9)

The number of trials does not fit in the range 1 T 10.

Your error message should print Number of trials must be > 0 and <= 10

3. Hints

Bounds checking and error messages

See Lectures 11-12 (F 9/28 and M 10/1) for one example of how to handle input validationrepeatedly prompting your user to enter input values until the inputs are error-free.

Random number generation

Section 4.11 of the text, which follows this lab, does an excellent job of describing the use of srand() and rand() for random number generation. If you have further questions, look at the hints section of the old Program 4 spec, or view the example program dice_example.con the course website. The program models a pair of dice being rolled until they match a user input value. This program also contains an example of an input validation loop.

Basic flow chart

While the template code below does provide a program outline you can follow, Figure 2 in the figures document also shows the basic flowchart I will cover in class on Thursday, 10/11 or Friday, 10/12.

4. Grading Rubric

For this assignment, points are assigned as follows:

40 points: Your code uses appropriate coding style such as including appropriate comments, indenting the main() function body, and appropriate variable declarations.

60 points: Your code compiles and output matches the desired test outputs shown below. Each test case has a different number of points assigned to it, as shown in submit mode. This section will be auto-graded, while I will assign the other 40 points after inspecting your program.

CAN YOU PLEASE FOLLOW THE INSRUCTIONS BELOW

/*

* REPLACE WITH YOUR OWN HEADER COMMENT

*/

#include

#include // Necessary for random number functions

int main() {

unsigned dir; // Determines direction to move (N, S, E, W)

/*** ADD ADDITIONAL VARIABLES HERE, THEN REMOVE COMMENT ***/

// Seed random number generator--DO NOT CHANGE THIS LINE

srand(1);

/* ADD CODE TO READ INPUTS AND CHECK FOR ERRORS

EACH INPUT PROMPT SHOULD BE REPEATED IF THERE IS

ANY ERROR IN THAT SET OF INPUT VALUES

** REMOVE THIS COMMENT ONCE YOU'RE DONE** */

/* AS DESCRIBED IN CLASS ON 6/7, YOU NEED A FOR LOOP

TO CONTROL THE NUMBER OF TRIALS

** REMOVE THIS COMMENT ONCE YOU'RE DONE** */

for ( /* REPLACE THIS COMMENT WITH VALID CODE */ ) {

/* WITHIN YOUR FOR LOOP, USE A WHILE OR DO-WHILE

LOOP TO CONTROL EACH TRIAL--REPEAT THE BODY

OF THE LOOP UNTIL YOUR "SAILOR" REACHES THE

BORDER

**NOTE: THERE ARE SOME VARIABLES YOU NEED TO

INITIALIZE OR RE-INITIALIZE BEFORE STARTING

EACH TRIAL--DO THAT INSIDE THE FOR LOOP BUT

OUTSIDE THE WHILE LOOP!**

** REMOVE THIS COMMENT ONCE YOU'RE DONE** */

while ( /* SAILOR HASN'T REACHED BORDER */ ) {

/* Generate a random number to choose your direction */

dir = rand(); /*** MODIFY THIS STATEMENT TO GIVE YOU A RANDOM VALUE BETWEEN 0 AND 3 ***/

/* WRITE CODE TO MOVE YOUR "SAILOR" ONE BLOCK IN THE

APPROPRIATE DIRECTION.

TO GET YOUR OUTPUT TO MATCH THE TEST CASES,

YOU ***MUST*** ASSUME THAT:

IF DIR IS 0 --> MOVE WEST

IF DIR IS 1 --> MOVE EAST

IF DIR IS 2 --> MOVE NORTH

IF DIR IS 3 --> MOVE SOUTH

** REMOVE THIS COMMENT WHEN YOU ARE DONE ** */

/* COUNT EACH STEP, AND **REMOVE THIS COMMENT WHEN YOU ARE DONE** */

}

}

return 0;

}

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

Essential Data Protection For Estate Agencies In Singapore 2024

Authors: Yang Yen Thaw Yt

1st Edition

B0CQK79WD3, 979-8872095392

More Books

Students also viewed these Databases questions

Question

What is the purpose of a variable? in java

Answered: 1 week ago