In: Computer Science
c programing language
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 city’s 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.
This assignment has been split into three parts to help teach you how to build a large program in smaller pieces. Doing so makes the program easier to develop and test--you can make sure each smaller piece works and then integrate the pieces together. In this lab, Part 1, you will handle the input processing--prompting the user for inputs, reading those values, and testing for all possible input errors.
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.
In your Blackboard submission, please indicate which of the three parts of the program you have completed. If you plan to complete all three parts, do not submit to Blackboard until all parts are done.
2. Specification
Problem Description
The city is organized as a set of M x N blocks. The sailor’s 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).
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).
T, The number of trials to execute (1 ≤ T ≤ 10). As we'll see in later parts of the assignment, each trial consists of placing the sailor at the starting point and randomly choosing movements until he reaches the border.
A sample set of input prompts and valid inputs to those prompts are shown below:
Output Specification
For this part of the program, after reading all inputs, your program should simply reprint the input values that were entered. So, given the sample inputs shown above, your program should print:
As noted above, detailed test cases showing appropriate input prompts can be found starting on page 3 of the figures document.
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.
The number of trials does not fit in the range 1 ≤ T ≤ 10.
3. Hints
Bounds checking and error messages
See Lecture 12 (M 9/30) for one example of how to handle input validation—repeatedly prompting your user to enter input values until the inputs are error-free.
Program development
Although the program has already been broken into smaller pieces, you can test your solution to this part piece by piece as well. I suggest starting with a single input validation loop--prompt for and read the number of X and Y blocks and test them for errors. If your program does all of those things successfully, it will pass the test case "Output test 2a". Then, move on to your input validation loop for the sailor's starting position, which, if it works correctly, will pass "Output test 2b." Adding a successful input validation loop for the number of trials will enable your program to pass the remaining test cases.
Program:
#include<stdio.h>
int main(){
int m,n,x,y,t;
//USED TO CHECK IF INPUTS SATISFY REQUIRED
CONSTRAINTS
int errorFree = 0;
//UNLESS USER ENTERS VALID M AND N VALUES, PROMPT
USER TO ENTER M AND N VALUES
while(errorFree == 0){
printf("Enter the values of m and
n(the number of planes in the X and Y planes): \n");
scanf("%d%d",&m,&n);
if(m>=2 && m<=10
&& n>=2 && n<=10){
//VALID M AND N
VALUES
errorFree =
1;
}else{
printf("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.\n");
if(m<2 ||
m>10){
printf("The number of X blocks must be >=2
and <= 10\n");
}
if(n<2 ||
n>10){
printf("The number of Y blocks must be >=2
and <= 10\n");
}
}
}
errorFree = 0;
//UNLESS USER ENTERS VALID X AND Y VALUES, PROMPT USER
TO ENTER X AND Y VALUES
while(errorFree == 0){
printf("Enter the starting postion
of the sailor: \n");
scanf("%d %d",&x,&y);
if(x>=1 && x<=m-1
&& y>=1 && y<=n-1){
//VALID X AND Y
VALUES
errorFree
=1;
}else{
if(x<1 ||
x>m-1){
printf("Starting X position must satisfy (1
<= X <= %d)\n",m-1);
}
if(y<1 ||
y>n-1){
printf("Starting Y position must satisfy (1
<= Y <= %d)\n",n-1);
}
}
}
errorFree = 0;
while(errorFree == 0){
printf("Enter the number of trials:
\n");
scanf("%d",&t);
if(t>=1 &&
t<=10){
//VALID T
VALUE
errorFree =
1;
}else{
printf("Number
of trials must be >0 and <=10\n");
}
}
printf("M: %d,N: %d\n",m,n);
printf("X: %d,Y: %d\n",x,y);
printf("T: %d\n",t);
return 0;
}
Console interaction/Output:
Screenshots(for better understanding):