CS 136 - Fall 2016 - Assignment 5

Due Date: Friday, October 28, 2016 at 8:59pm (evening).

NOTE: there will be no second chances on this assignment.



BLACK QUESTION (moderate collaboration allowed)

Question 1: Reimplementing readnat and readchar

[10 marks correctness]

For this question, you must implement the readnat and readchar modules that you have been using on previous assignments.

For this question, the marmoset basic tests ("public" tests) will use the same simple test file provided.


GOLD QUESTION (no collaboration allowed)

Question 2: An interactive calculator

[20 marks correctness]

For this question, you must implement an interactive calculator that reads in commands and arguments from the input. Commands are single characters and described below. Arguments are integers. Both commands and arguments may be separated by arbitrary whitespace. If an invalid command or argument is provided, or EOF is encountered, the program prints "INVALID\n" and exits the program.

The initial value of the calculator and the memory are zero.

The following are a list of commands. Some commands are followed by arguments:

If the ARG provided to / or % is zero the program prints "DIV0\n" and exits the program.

For this question, the marmoset basic tests ("public" tests) will use the same simple test file provided.


GOLD QUESTION (no collaboration allowed)

Question 3: Finding a Root with Bisection

[15 marks correctness]
LANGUAGE FEATURES: For this question, you may use the double data type
COLLABORATION: For this question, you may freely discuss with others how the "bisection method" works, but not how to implement it.

For this question, you must implement a bisection function that uses the "bisection method" to find the root of an arbitrary function f (the root of a function f is the value x such that f(x) is zero).

The function f is passed a double and returns a double.

The bisection function is passed a lo and hi value such that f(lo) and f(hi) are both non-zero and have opposite signs. As a result, (assuming f is continuous) a root must exist strictly between lo and hi.

The bisection function is passed a positive tolerance to determine when the found root is "close enough".

The "bisection method" iterates as follows:

  1. The midpoint (mid) is found between lo and hi.
  2. If |f(mid)| is less than or equal to tolerance, the solution has been reached and mid is returned.
  3. Otherwise, the sign of f(mid) is observed, and either lo or hi is changed to the value of mid so that the root is still contained within the range.

For this question, the marmoset basic tests ("public" tests) do not test your module at all and simply determine if your code can be "run".

A simple test file has been provided to illustrate how bisection can be used.


GOLD QUESTION (no collaboration allowed)

Question 4: Pointers & Robots

[20 marks correctness]

For this question, you must implement the robot module and complete a test program for the robot module.

A robot keeps track of its position in a Cartesian (x-y) plane, and when initialized it begins at the origin (0,0).

A robot can also report the farthest distance it has travelled from the origin in each of the four orthogonal directions. For example, if the robot is initialized and then travels "down" 10, its position would be (0,-10). If it travels "down" again another 5, its position would be (0,-15). The maximum distance it has travelled in the "down" direction from the origin would be 15.

For this question, you must implement an interactive test program that reads in commands and arguments from the input. Commands are single characters and described below. Arguments are positive integers. Both commands and arguments may be separated by arbitrary whitespace. If an invalid command or argument is provided, or EOF is encountered, no message is displayed and the program simply exits.

The following are a list of commands that your test client must support. Some commands are followed by arguments:

The test program should automatically initialize the robot before it starts reading in commands.

For this question, the marmoset basic tests ("public" tests) will use the same simple test file provided.


GOLD QUESTION (no collaboration allowed)

Question 5: Advanced Reading of Ints

[25 marks correctness] [10 marks style]

For this question, you must implement the saferead module and complete a test program for the saferead module.

For this question, you may not read in integers with scanf("%d").

Instead, you should be reading in one character at a time and constructing an integer from the characters. For example, if saferead reads in a character '1' followed by a '9' and then a space ' ', saferead returns the integer 19.

saferead is capable of detecting when a sequence of characters represents an integer that exists outside of the C int range, and returns a constant to indicate that overflow has occurred.

Similarly, saferead is capable of detecting when a sequence of characters is not an integer because it has invalid characters.

For this question, the marmoset basic tests ("public" tests) will use the same simple test file provided.