Question

In: Computer Science

c programing language This assignment, which introduces the use of loops, solves the following problem: given...

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).

    • 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--notice that these bounds are based on the values of M and N, the first pair of inputs:
      • 1 ≤ X ≤ (M-1)
      • 1 ≤ Y ≤ (N-1)
  • 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.

  1. 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!

  2. 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.

  3. 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)
  4. 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 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.

Solutions

Expert Solution

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):


Related Solutions

design a program that solves matrices by the method of gauss-seidel use the object-oriented language c...
design a program that solves matrices by the method of gauss-seidel use the object-oriented language c ++
C programming problem I have to design an efficient algorithm which solves this problem. Also, it...
C programming problem I have to design an efficient algorithm which solves this problem. Also, it needs to include time and space complexities of this algorithm. There is a matrix (N times N) of integers which rows and columns are sorted in non-decreasing order. A sorted matrix and integer M will be given and we need to find the position(row,column) of M in this matrix. it's okay to report only one position if there are more than one answers. when...
in C++ programing language Write a program that prompts the user for an integer, then prints...
in C++ programing language Write a program that prompts the user for an integer, then prints all of the numbers from one to that integer, separated by spaces. Use a loop to print the numbers. But for multiples of three, print "Fizz" instead of the number, and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz". Drop to a new line after printing each 20 numbers. If the user typed...
C++ Please Do not use loops!! Assignment : Dice Roll Redux In "Dungeons & Dragons" and...
C++ Please Do not use loops!! Assignment : Dice Roll Redux In "Dungeons & Dragons" and similar role-playing games, dice with different numbers of sides are used to determine the outcomes of events throughout the game. There are different dice for different occasions: 4-sided, 6-, 8-, 10-, 12-, and 20-sided dice are common. A required roll is typically designated <n>d<s> where <n> is the number of dice to throw and <s> is the number of sides on those dice. For...
Use MATLAB to create a script which solves for problem 5.9 in the book (5.11 in...
Use MATLAB to create a script which solves for problem 5.9 in the book (5.11 in the 4th edition). Given are the equations for the moment, as a function of x, starting from the leftmost side of the beam with x=0 and ending at the other end of the beam with x=12. This piecewise function together makes up the moment equation for the beam given. 0 ≤ ? ≤ 3 ?(?) = 265? − 5.56?3 , 3 < ? ≤...
THE STRING MATCH PROBLEM C++ only. Must use loops. Please do not use sequence of if...
THE STRING MATCH PROBLEM C++ only. Must use loops. Please do not use sequence of if statements. . Given 2 strings, a and b, set result to the number of the positions where they contain the same length 2 substring. So "xxcaazz" and "xxbaaz" yields 3, since the "xx", "aa", and "az" substrings appear in the same place in both strings. • for input of "xxcaazz", "xxbaaz" → 3 • for input of "abc", "abc" → 2 • for input...
c programing language When a new program is executed, a new process is created with the...
c programing language When a new program is executed, a new process is created with the next available process ID. However, there are a few special processes that always have the same process ID, which are usually given the ID value less than 5 these are called system processes. Can you identify which of the two system processes have the process ID of 0 and 1 respectively?
**CODED IN C LANGUAGE** Case 1: The given snapshot in the assignment instructions checks for the...
**CODED IN C LANGUAGE** Case 1: The given snapshot in the assignment instructions checks for the following: P to be switched with Q (Once done will remain as it is in all the rows) If the user enters the same P again, the program must not make any changes For instance, given elements are 0123456789 3 4          0              0124456789 2 5          1              0154456789 (Keeping the change in Row 0 (input for row 1); 2 is switched to 5) 1 6          2              0654456789 (Keeping...
Language: C++ NEEDS TO WORK IN VISUAL BASIC The code is broken and loops in a...
Language: C++ NEEDS TO WORK IN VISUAL BASIC The code is broken and loops in a few places please fix it #include<iostream> using namespace std; //function declaration void EnterRents(int*, int); void displayRents(int*, int); void selectionSort(int*, int); int sumRents(int* temp, int size) {    int sum = 0;    for (int i = 0; i < size; i++)    {        sum += *(temp + i);    }    return sum; } void Displaymemory(int* temp, int size) {    /*int...
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing...
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing chess himself to practice his abilities. The chess that Jojo played was N × N. When Jojo was practicing, Jojo suddenly saw a position on his chessboard that was so interesting that Jojo tried to put the pieces of Rook, Bishop and Knight in that position. Every time he put a piece, Jojo counts how many other pieces on the chessboard can be captured...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT