0% found this document useful (0 votes)
8 views

Assignment Questions C

The document describes a programming assignment to write a C program that estimates the probability of an explorer escaping from a dinosaur and volcano-infested island by taking random walks. The program must calculate escape probabilities, mean path lengths, and standard deviations for starting from each cell of a 9x9 input map, performing 1000 walks from each.

Uploaded by

Nipun Vindula
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Assignment Questions C

The document describes a programming assignment to write a C program that estimates the probability of an explorer escaping from a dinosaur and volcano-infested island by taking random walks. The program must calculate escape probabilities, mean path lengths, and standard deviations for starting from each cell of a 9x9 input map, performing 1000 walks from each.

Uploaded by

Nipun Vindula
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Objective

The objective of this assignment is to write a C program which estimates


the probability that an explorer will safely escape from a dinosaur- and
volcano-infested island ("Jurassic Island") by taking random walks
across it. As well as calculating the probability of escape, the mean and
standard deviation of the path lengths should be determined for each
starting cell.

Island map
The island map is represented as a 9x9 array:
B W W B B W B W W
B L L V L L L L B
W L L L L D L L B
B L L D L L L L W
B L D L L L L L W
W L L L L L V L B
W V L L L L L L W
W L L L D L L L W
B B W B W B B W B
where each cell is labelled as follows:
• L: Land, the explorer can safely step on this cell.
• B: Bridge, the explorer can safely step on this cell and use it to
get off the island.
• W: Water, the explorer will die if this cell is stepped on.
• D: Dinosaur, the explorer will die if this cell is stepped on.
• V: Volcano, the explorer will die if this cell is stepped on.
Note that there is a space between each letter in the island map and no
space at the end of the line. Also, this is not the same map as that used
in the task itself.

The only way to escape from the island is for the explorer to step onto a
Bridge (B) cell.
Stepping onto a Land (L) cell is safe, however if the explorer steps on a
Dinosaur (D), Volcano (V), or Water (W) cell, the walk is terminated and
the next random walk should be attempted.

Task
Each cell in the map is to be used as a starting point for walks,
beginning, for example, with the B cell at the top-left corner (i.e. [0][0] in
a 2D array representing the island map).
For each cell on the map, the program should perform 1000 random
walks where the explorer attempts to get off the island.

On each attempt, the explorer can take up to, and including, 10 steps
before they run out of energy and the walk terminates unsuccessfully,
moving on to the next attempt.

Each step on a walk is randomly chosen from one of eight directions, i.e.
North (N), Northeast (NE), East (E), Southeast (SE), South (S),
Southwest (SW), West (W), or Northwest (NW), with equal probability.

After each step, the program should check to see if the walk:
• Is successful by stepping onto a Bridge cell.
• Has failed by stepping onto a Dinosaur, Volcano or Water cell.
• Continues by stepping onto a Land cell.
• Has failed due to the maximum number of steps being taken.

For example, assuming that the explorer is near the top-left, at location
[1][1] (ignoring the spaces) which is a Land (L) cell. If the next step is to
the NW or E they will have stepped on a Bridge cell (B) and so will have
escaped from the island successfully. If the next step is to E, SE, or S,
then they have moved to another Land (L) cell and the walk should
continue for another step. However, if the next step is to N, NE, or SW
then the explorer lands on a Water (W) cell and the walk terminates
unsuccessfully.
REQUIREMENTS

Inputs
Your program should read a 9x9 island map from a text file, stored in the
same directory as your program, called "island_map.txt".

Input validation
We will compile and execute your code, using unseen maps to check the
behaviour. This will include passing invalid inputs to your code. You
should add input validation checks to your code, anticipating the different
ways in which a user may provide an incorrect input.

If your code detects an error with the input it should display:


Error!
and exit, with an exit status of 1. No other output should be displayed,
and the text above must be exact. You can use:
printf("Error!");
to display the required text.

Starting conditions
You should analyse walk paths starting from each of the 81 possible
starting cells in a 9x9 island map. If the starting cell is a Bridge (B), no
steps are needed and so the path length should be recorded as 0. If the
starting cell is Water (W), Dinosaur (D), or Volcano (V), the explore will
die immediately and so the path length should be recorded as 0.

Coding scheme
To perform the random movement you must use the rand() function (see
below). You should map each move to an integer value as follows:
• 0 represents move N.
• 1 represents move NE.
• 2 represents move E.
• 3 represents move SE.
• 4 represents move S.
• 5 represents move SW.
• 6 represents move W.
• 7 represents move NW.
This coding scheme is shown on the diagram below.
Generating random movements
You are required to use the rand() function to randomly move in one of
eight possible directions.

rand(), which is part of the C standard library stdlib.h, returns a pseudo-


random integer uniformly distributed within the interval given by [0,
RAND_MAX], where RAND_MAX typically has a value of 32767.

You should constrain the range of random numbers that rand()


generates by using the modulus (remainder) operator introduced in
Topic 3.

Note that rand() must be seeded with a value before it is first called. For
this purpose you must use srand() as:
srand(123456);
You should only call srand() once, near the start of your program in int
main(). (Aside: Random numbers.)

If on an edge cell, and a random move would take the explorer outside
the edge of the map, then the explorer should stay put in that direction.
For example:
• If they are on the bottom row, and try and move South, they will
stay on the same cell. (As a move South would take them out of
the map.)
• If they are on the bottom row and try and move South East they
will move East, but stay on the same row.
• If they are on the bottom row, in the bottom right corner, and
move South East they will stay on the same cell. (As a move
South would take them out of the map, and a move to the East
would take them out of map.)
All of these cases still count as a step, and so the step counter should
be incremented.

Note that the explorer can take up to 10 steps on each try. 0 and 10 are
thus valid numbers of steps to take.
.

If checking your standard deviation function in another programming


language, note that the above equation is implemented by (for an
example array containing 1 to 10):
• Matlab: std([1, 2, 3, 4, 5, 6, 7, 8, 9, 10],1)
• Python with numpy: np.std(a,ddof=0)
Just say, just:
• Matlab: std([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
implements a different definition of the standard deviation (where the
denominator is N-1.)

Other standard libraries


You can use other built-in C libraries as you wish, provided that all of
your code is contained in the one coursework.c file. In particular, you
may wish to use the sqrt() function included in math.h.

You are not allowed to include your own custom headers. These will not
be used by the marking system.

Outputs
For every cell on the island map your program should display (to two
decimal places when suitable):
• A copy of the input map.
• The probability of successfully escaping from the island,
displayed as a percentage. That is, how many of the random
walks made it to a Bridge (B) cell.
• The mean path length (i.e. number of steps)
of successful walks.
• The standard deviation of path lengths of successful walks.
Invalid starting cells have 0.00 for the above numerical items. The
values should be displayed to the terminal.

An example output is shown on the following page. The output displayed


must be formatted correctly:
• Use the headings (e.g. "Map:") exactly as shown on the
example.
• Numbers on the same row should be separated by spaces.
• Suitable format specifiers should be used to set the precision
and field width, to keep information in each table aligned.
• Include a blank line in between each of the output blocks (the
map display, probability display, and so on). There is no blank
line between rows in a table.

Use of functions
Although you can complete a suitable program without them, we suggest
that you use your own custom functions to keep your code compact and
avoid repetitive code for common actions. You might like to use the
following (incomplete) function prototypes:
• void random_step(…);
• int calculate_status(…);
where:
• random_step calculates the next random step.
• status returns the status of the next step:
o 0 Failure
o 1 Successfully made it to a bridge cell
o 2 Walk continuing

Planning and testing


We suggest that you plan your code, before you start typing it in. Think
about the main steps your code has to go through - getting data,
processing the data, and outputting the results.

You should also think about your test strategy. How will you check that
your code does what you want it to do? How will you check that any
functions you write in your code do what you want them to do? Feel free
to make your own maps and use these to test your code.

Coding style and quality


We will check your code for common programming issues, including the
number of comments used. You will lose marks if, for example, warnings
are generated by the compiler, or there are issues with overflowing
arrays.

EXAMPLE OUTPUT

The below shows the formatting of the output required from your
program. Note that this is not the output for the island_map.txt included
in the GitHub site. The actual coursework uses a different map to that
below. In particular, note that:
• The numbers below are separated by spaces, not by tabs.
• There is no space on the end of a row.
Map:
B W W B B W B W W
B L L V L L L L B
W L L L L D L L B
B L L D L L L L W
B L D L L L L L W
W L L L L L V L B
W V L L L L L L W
W L L L D L L L W
B B W B W B B W B

Probability of escape:
100.00 0.00 0.00 100.00 100.00 0.00
100.00 0.00 0.00
100.00 35.60 28.10 0.00 32.10 40.30 34.10 49.40
100.00 0.00 40.80 20.50 13.60 13.00 0.00 31.10 43.
60 100.00
100.00 41.40 19.70 0.00 9.00 14.60 20.70 27.60
0.00
100.00 39.70 0.00 8.50 9.20 11.50 17.00 25.80
0.00
0.00 20.90 13.20 11.40 14.50 12.70 0.00 23.80
100.00
0.00 0.00 19.00 19.30 16.70 16.60 24.40 28.50
0.00
0.00 31.20 37.20 23.80 0.00 39.20 41.90 35.60
0.00
100.00 100.00 0.00 100.00 0.00 100.00 100.00 0.00
100.00

Mean path length:


0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
0.00 1.72 2.12 0.00 1.50 1.77 2.28 1.63 0.00
0.00 2.01 3.12 3.60 3.72 0.00 3.41 2.16 0.00
0.00 2.02 3.31 0.00 5.66 5.21 3.82 2.87 0.00
0.00 1.79 0.00 5.52 6.34 5.75 4.64 2.36 0.00
0.00 1.99 4.52 5.23 5.48 5.22 0.00 2.42 0.00
0.00 0.00 3.41 4.62 4.31 4.13 3.30 2.29 0.00
0.00 1.64 1.81 2.27 0.00 2.04 1.92 1.83 0.00
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00

Standard deviation of path length:


0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
0.00 1.37 1.53 0.00 1.19 1.38 1.58 1.43 0.00
0.00 1.59 1.60 1.68 2.17 0.00 1.82 1.80 0.00
0.00 1.52 1.69 0.00 1.96 2.22 1.98 2.21 0.00
0.00 1.46 0.00 1.88 1.76 1.98 2.28 2.13 0.00
0.00 1.73 2.31 2.18 2.01 2.05 0.00 2.01 0.00
0.00 0.00 2.00 2.52 2.26 2.26 1.74 1.71 0.00
0.00 1.56 1.54 1.89 0.00 1.95 1.60 1.64 0.00
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00

You might also like