Question: I need Help with checking my C program not C++ 4.10 Program 4: The Drunken Sailor Problem 1. Introduction This assignment, which introduces the use

I need Help with checking my C program not C++

4.10 Program 4: The "Drunken Sailor" Problem

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.

Remember, in addition to submitting your code, you MUST complete the Blackboard assignment "Program 4 style assessment" to get all of the points for this program. You can complete this "assignment" by typing a short message indicating you submitted your code through the textbook IDE.

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.

MY CODE IS

/* */

#define _CRT_SECURE_NO_WARNINGS #include #include #include

void main() { unsigned citysize, citysize2, pointx, startingx, startingy, pointy, trials, E, o; int scanfscanned, seed, run, P; char junk; double average, totalsteps, stepspertrial; P = 0; totalsteps = 0; stepspertrial = 0.00;

do { printf("Enter seed (-1 to use system time): "); scanfscanned = scanf("%d", &seed); if (scanfscanned < 1) { //checking if it needs to clear the line, junk

do { scanf("%c", &junk); } while (junk != ' ');//clear the line } } while (scanfscanned < 1);

if (seed == -1) srand(time(0)); else srand(seed);

do { printf("City size in X,Y (#blockes >=2 and <=10): "); scanfscanned = scanf("%u %u", &citysize, &citysize2); if (scanfscanned != 2) { do { scanf("%c", &junk); } while (junk != ' '); }

else if ((citysize<2) || (citysize>10)) printf("# X blocks must be >= 2 and <= 10 "); if ((citysize2 < 2) || (citysize2 > 10)) printf("# Y blocks must be >= 2 and <= 10 ");

} while (scanfscanned != 2 || citysize2 >10 || citysize2 < 2 || citysize >10 || citysize < 2);

do { printf("Starting position (X Y): "); scanfscanned = scanf("%u %u", &startingx, &startingy); E = citysize - 1;//create boundary o = citysize2 - 1;

if (scanfscanned != 2) { do { scanf("%c", &junk); } while (junk != ' '); }

else if ((startingx < 1) || (startingx > E))

printf("Starting X position must satisfy (1 <= X <= %u) ", E); if ((startingy < 1) || (startingy > o))//verifying if the point is valid

printf("Starting Y position must satisfy (1 <= Y <= %u) ", o);

} while (scanfscanned != 2 || startingx < 1 || startingx > E || startingy < 1 || startingy > o);

do { printf("Number of trials: "); scanfscanned = scanf("%u", &trials); if (scanfscanned != 1) { do { scanf("%c", junk);

} while (junk != ' '); } else if ((trials <= 0) || (trials >= 10)) printf("Number of trials must be > 0 and <= 10 "); } while (scanfscanned != 1 || trials <= 0 || trials >= 10);

while (P < trials) { P = P + 1; pointx = startingx; pointy = startingy; printf("Trial # %d Start: %d %d ", P, startingx, startingy); do { stepspertrial = stepspertrial + 1; run = rand() % 4 + 1; switch (run) { case 1: pointx = pointx + 1; printf("East: %u %u ", pointx, pointy); totalsteps = totalsteps + 1; break; case 2: pointx = pointx - 1; printf("West: %u %u ", pointx, pointy); totalsteps = totalsteps + 1; break; case 3: pointy = pointy + 1; printf("North: %u %u ", pointx, pointy); totalsteps = totalsteps + 1; break; case 4: pointy = pointy - 1; printf("South: %u %u ", pointx, pointy); totalsteps = totalsteps + 1; break; } } while (pointx > 0 && pointx < citysize && pointy > 0 && pointy < citysize2); printf("Trial # %d total steps = %.0lf ", P, stepspertrial); stepspertrial = 0; } average = totalsteps / trials; printf("Average # of steps over %d trials: %.2lf ", P, average); }

It give erro of

Program generated too much output. Output restricted to 50000 characters. Check program for any unterminated loops generating output. or

Program end never reached (commonly due to an infinite loop or infinite recursion).

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!