CS 136 - Winter 2017 - Assignment 5

Due Date: Friday, February 10, 2017 at 11:59am (morning / noon).



LANGUAGE FEATURES

You MAY NOT use recursion for this assignment. Use iteration (loops) instead. You may use any of the loop control flow statements discussed in the notes (while, do while or for).


BLACK QUESTION (moderate collaboration allowed)

Question 1: Print an ASCII table

[5 marks correctness]

For this question you write a program that uses a loop to print an ASCII table of printable characters similar to the one that appears on Slide 19 of Section 5 in the course notes. However, your version should be identical to the following table (note that the rows and columns are different from that in the course notes):

  32 space   33 !       34 "       35 #       36 $       37 %
  38 &       39 '       40 (       41 )       42 *       43 +
  44 ,       45 -       46 .       47 /       48 0       49 1
  50 2       51 3       52 4       53 5       54 6       55 7
  56 8       57 9       58 :       59 ;       60 <       61 =
  62 >       63 ?       64 @       65 A       66 B       67 C
  68 D       69 E       70 F       71 G       72 H       73 I
  74 J       75 K       76 L       77 M       78 N       79 O
  80 P       81 Q       82 R       83 S       84 T       85 U
  86 V       87 W       88 X       89 Y       90 Z       91 [
  92 \       93 ]       94 ^       95 _       96 `       97 a
  98 b       99 c      100 d      101 e      102 f      103 g
 104 h      105 i      106 j      107 k      108 l      109 m
 110 n      111 o      112 p      113 q      114 r      115 s
 116 t      117 u      118 v      119 w      120 x      121 y
 122 z      123 {      124 |      125 }      126 ~
Each line should contain a maximum of 6 pairs of ASCII values and their corresponding characters and end in a newline character. Each pair should be printed as "%4d %c". After each pair either 5 spaces are printed or (if it is the last pair on the line) a newline (\n). In the above, there is a newline (and no additional spaces) printed after % and ~. Note that the the ASCII value 32 should be handled as a special case.

There is only one I/O test case for this question (provided).


BLACK QUESTION (moderate collaboration allowed)

Question 2: Reading and Processing Input with Iteration

[15 marks correctness]

Do Assignment 2, Q4 parts a, b, and c. However, instead of using recursion your solution must use iteration.

As in A2, we have provided an example to show how readnat can be used inside of a loop.


BLACK QUESTION (moderate collaboration allowed)

Question 3: Drawing some Shapes

[20 marks correctness]

For this question, you must implement the rectangle, triangle and squares functions described in the module interface shapes.h. The implementations must be added to the file shapes.c.

We have also provided you with an I/O testing client (io-test-shapes.c) that will allow you to interact with and test your module using .in and .expect files.

Pay careful attention to the test client and the sample.in and sample.expect files we have provided to learn how to properly draw the shapes.

For this question, the Marmoset basic tests ("public" tests) will be identical to the simple test file provided.


GOLD QUESTION (no collaboration allowed)

Question 4: ASCII Art

[30 marks correctness]

For this question you must implement two programs.

  1. This program reads instructions from input to produce ASCII art as output. The input consists of pairs of natural numbers:
    C A
    where :
    • C represents a count (>= 1)
    • A represents a printable ASCII character

    Your program should read the first number (C) and then print C copies of the second number (A) in ASCII form using "%c". This should be repeated until there is no more input in the file (readnat() returns -1). Given the proper instructions you will see a picture (art :-) produced using ASCII characters. For example, the following input:


    1 43
    7 45
    1 43
    1 10
    3 65
    3 97
    1 10
    

    would produce this output:
    +-------+
    AAAaaa
    

    Note that each line of the output above ends with a newline character. This is part of the instructions (1 10 means print 1 newline character, because the ASCII value of '\n' is 10). You should not print any newline characters unless they are specified in the input.

    To read natural numbers as input, we have provided the function readnat() in the module input (see input.h).

  2. This program reads ASCII art as input and produces output that is a set of instructions which could be read by the program completed in part a. The input:
    +-------+
    AAAaaa
    

    Would produce the output:


    1 43
    7 45
    1 43
    1 10
    3 65
    3 97
    1 10
    

    Note that in the above example, the following would be considered incorrect:
    1 43
    1 45
    6 45        <--- not allowed
    1 43
    1 10
    3 65
    3 97
    1 10
    
    Each subsequent pair must have different ASCII values. In addition, each pair must be printed as "%d %d\n" to facilitate our testing. To read characters as input, we have provided the function readchar() in the module input (see input.h). Your program should read in characters until readchar() returns -1.

For this question, the Marmoset basic tests ("public" tests) will be identical to the simple test files provided.

Fun Fact: This is closely related to Run-Length Encoding.

BONUS: [2 marks]

For q4a provide a bonus.in file that represents a sequence that displays a fun, interesting or beautiful picture (hint: use q4b to generate the sequence). The "best" 25 pictures (as determined by the ISAs) will receive 2 bonus marks. Note: simply using the first ASCII art you find on google may not be sufficient. Do NOT ask any questions on piazza regarding the bonus.

After the assignment is complete, you may freely share your bonus.in files on Piazza (there will be a dedicated post).


GOLD QUESTION (no collaboration allowed)

Question 5: Gold Mining

[15 marks correctness][15 marks style]

For this question you must implement a single program that searches multiple mines for gold and prints information about what you find (as specified below).

You are to use the functions provided in the module given to you called mines. The file mines.h describes the functions provided by this module.

First, use the function mines_read, which reads the input (from .in files) that describes the mines. It also returns the number of mines you need to search.

Then, for each mine, your program is to search all of the locations in that mine (in 3D space) looking for gold. Locations are specified with three coordinates (x,y,z). You can think of the z coordinate as the depth of the mine. All locations from (0,0,0) to ((mines_max_x(mine), (mines_max_y(mine), (mines_max_z(mine)) must be checked in each mine. You can find out if a mine contains gold at a specific location by using the function mines_check_for_gold(mine, x, y, z). The order in which you search the mines and the locations is important in order to match our expected output. If you imagine the mine and coordinates as a 4-digit number: mxyz, you must search in ascending order. For example:

0000  mine: 0, (x,y,z): (0,0,0)
0001  mine: 0, (x,y,z): (0,0,1)
0002
0003
...
0010
0011
0012  mine: 0, (x,y,z): (0,1,2)
...
9999  mine: 9, (x,y,z): (9,9,9)
In other words, that is the order that must be used when calling mines_check_for_gold, for the case where there are 9 mines and the maximum dimensions of each mine is (9,9,9). Each mine can have different maximum dimensions but mines_max_x(mine), mines_max_y(mine), mines_max_z(mine) will always be <= 9 and our tests will use a maximum of 10 mines (numbered 0..9).

Output

When gold is found at a particular location you must call the function mines_print_found with the appropriate parameters (this will print information about the mine, the location and the amount of gold).

If no gold is found after searching all of a mine (and only if no gold is found anywhere in the mine) you must call the function mines_print_no_gold (this will indicate that no gold has been found).

After searching the entire mine and before starting to search the next mine you must call mines_print_done with the appropriate parameters to indicate that the search of that mine has been completed (this will print information about what was found and how many locations where checked).

After searching the all of the mines you must call mines_print_summary which prints a summary about all of the mines.

If any gold was found in any mine, you must first call mines_print_best_mine which prints the information about the mine with the "best" gold (most total gold in the entire mine). In case of a tie, the first mine encountered with the best gold should be printed. Next, you must then call mines_print_best_deposit which prints the information about the single location (deposit) that has the "best" gold (most gold at a single location). In case of a tie, the first location encountered with the best gold should be printed.

Input

You need to understand the format of input files in order to create your own test input and to understand the tests that are provided.

The input file specification is described in input-readme.txt

For this question, the Marmoset basic tests ("public" tests) will be identical to the simple test files provided. Be sure to have a look at the provided .in and .expect files.