Using programming language C. Dear Chegg Person, I tried this program several ti
ID: 3891688 • Letter: U
Question
Using programming language C.
Dear Chegg Person, I tried this program several times and I have not been able to make this program run. Thanks for your help.
Program: 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.
Ycoord (moving down, moves downward)
0 0 0 0
S
0 0 0 0
0 0 0 0
X coord (moving across ->)
Note the above 0's are suppose to represent the city blocks and the S is to represent the Sailor,
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 city borders. Note that each new trial always uses the same starting point.
Input: 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: • A seed value for the random number generator (RNG). If the user enters -1, use the system time to seed the RNG; otherwise, the user input is used. See Section 4, “Hints and Tips,” for more details. • 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 simply be 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).
Output: As noted above, the program should print a prompt for each input. See Section 5, “Test Cases,” for examples of acceptable prompts. 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 Section 4 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. 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
Again, see Section 5: Test Cases for detailed examples of how your output should look.
4. Hints and Tips: Bounds checking and error messages: You may repeatedly prompt User to enter input values until inputs are error-free.
Random number generation: The library function rand() generates pseudo-random numbers between 0 and RAND_MAX (a large, constant value defined in the header <stdlib.h> , which you must include to use this function). To get a random value between 0 and N, use the modulus operator (%) as follows: X = rand() % (N+1);
For example, to generate one of six different values between 0 and 5, you can use: rand() % 6.
Note that rand() is not truly random—if you do not provide a different starting point, or “seed”, each time you run your program, rand() produces the same values. To specify a seed, use the function: srand (unsigned int seed).
While your test cases may not match mine, if you repeatedly use the same seed value, your program should produce the same set of random values each time. A common way to get (close to) true randomness is to use the system time as the seed:
srand(time(0));
srand() should only be called once per program, before any calls to rand(). To use the time() function, you must include the header. The course website contains a short program, dice_example.c, that demonstrates the proper use of the rand() and srand() functions. It also contains examples of an input validation loop like the one described above.
5. TEST CASES: Note that, because this program uses random numbers, your outputs for each trial likely will not match the test cases below. (The 2x2 city case is an exception—your sailor may move in different directions, but every trial should take only 1 step to finish.) What should match is the order in which you read input values and the cases in which you print error messages. Note that these test cases do not cover all possible program outcomes. You should create your own tests to help debug your code and ensure proper operation for all possible inputs. I’ve copied and pasted the output from each test case below, rather than showing a screenshot of the output window. User input is underlined in each test case, but it won’t be when you run the program. Test Case 1:
Enter seed (-1 to use system time): 1
City size in X, Y (# blocks >= 2 and <= 10): 2 2
Starting position (X Y): 1 1 Number of trials: 3
Trial # 1 Start: 1 1
South: 1 0
Trial # 1 total steps = 1 -------------------------------------
Trial # 2
Start: 1 1
East: 2 1
Trial # 2 total steps = 1 -------------------------------
Trial # 3 Start: 1 1
East: 2 1
Trial # 3 total steps = 1
Average # of steps over 3 trials: 1.00
Test Case 2: Enter seed (-1 to use system time): 1
City size in X, Y (# blocks >= 2 and <= 10): 1 1
# X blocks must be >= 2 and <= 10
# Y blocks must be >= 2 and <= 10
City size in X, Y (# blocks >= 2 and <= 10): 12 3
# X blocks must be >= 2 and <= 10
City size in X, Y (# blocks >= 2 and <= 10): 10 10
Starting position (X Y): 0 10 Starting X position must satisfy (1 <= X <= 9)
Starting Y position must satisfy (1 <= Y <= 9)
Starting position (X Y): 2 8
Number of trials: 0
Number of trials must be > 0 and <= 10
Number of trials: 1
Trial # 1 Start: 2 8
South: 2 7
East: 3 7
East: 4 7
North: 4 8
North: 4 9
West: 3 9
West: 2 9
North: 2 10
Trial # 1 total steps = 8
Average # of steps over 1 trial: 8.0
Thanks for your help.
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(){
int s,m,n,s1,s2,x,y,nt,i;
printf("Enter seed (-1 to use system time):");
scanf("%d",&s);
if (s == -1)
srand(time(NULL));
else
srand(time((time_t *)&s));
while(1){
printf("City Size in X and Y (# blocks >=2 and <= 10):");
scanf("%d%d",&m,&n);
if (m < 2 || m > 10 || n < 2 || n > 10){
printf("# X blocks must be >=2 and <=10 ");
printf("# Y blocks must be >=2 and <=10 ");
}
else
break;
}
while(1){
printf("Starting position (X Y):");
scanf("%d%d",&s1,&s2);
if (s1 < 1 || s1 > 9 || s2 < 1 || s2 > 9){
printf("# Starting X position must satisfy (1 <= X <= 9) ");
printf("# Starting Y position must satisfy (1 <= X <= 9) ");
}
else
break;
}
while (1){
printf("Number of trials:");
scanf("%d",&nt);
if (nt <= 0){
printf("Number of trials must be > 0 and <= 10 ");
}
else {
break;
}
}
float sum = 0;
for (i = 0; i<nt; i++){
printf("Trial # %d Start: %d %d ",i+1,s1,s2);
x = s1;
y = s2;
int num_steps = 0;
while (x > 0 && x < m && y >0 && y < n){
int r = rand()%4;
if (r == 0){
y = y+1;
printf("North: %d %d ",x, y);
num_steps++;
}
if (r == 1){
x = x+1;
printf("East: %d %d ",x, y);
num_steps++;
}
if (r == 2){
y = y-1;
printf("South: %d %d ",x, y);
num_steps++;
}
if (r == 3){
x = x-1;
printf("West: %d %d ",x, y);
num_steps++;
}
}
printf("Trial # %d total steps = %d ",i+1, num_steps);
sum = sum + num_steps;
}
printf("Average # of steps over 1 trial: %.1f ",sum/nt);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.